r/learnphp • • Jun 21 '20

ElasticSearch Example with Laravel 7 - Phpflow.com

Thumbnail
phpflow.com
3 Upvotes

r/learnphp • • Jun 09 '20

Can someone help me with adding current date to database?

3 Upvotes

At my job I created something for managing where customers prints are located. I work at a print and design shop. Here is a shot of what it looks like. I would like to add a column at the far right that would be the date that customers job was finished and put at that location.This will be good info because sometimes we have customers order prints and they leave them there for what seems like months. When it's busy, we don't have much extra space. If the date is up there, we can tell a customer that their prints have to be picked up in 30 days or 90 days etc or theyll be charged a fee.

Anyway, I guess it would be the current date that needs to get inserted in to the database (the date the other info is entered). I could do it manually where I just type the date in and pull that info from the database just like the other info but I want to learn.

Here is what it looks like. https://imgur.com/a/XswtHt3

I think what I am talking about will make more sense when you see it. The top is the basic form where myself or a coworker types in the customer name, job name, and where it is located.


r/learnphp • • Jun 05 '20

Simple Php/json question

3 Upvotes

Since i posted into the wrong subreddit to begin with ill try here aswell;

I have a Simple Question, Because i can't seem to get it to work myself.

I have a program, that spits out Data in a JSON file. Theres some of the data in this JSON file i want to insert into my SQL database.

however, what ever i try, it doesnt seem to post into the Database itself.. i cant seem to figure out what im doing wrong.

Screendump of my current code; https://imgur.com/a/71sRajb


r/learnphp • • May 29 '20

How to rename files across servers in php?

3 Upvotes

Hey guys, i just recently started learning php and i am wondering how would you ....rename files on different servers in php? like one file stored in AWS S3 bucket and not stored locally. Would this still work? I am trying to a way to do in php. Open to any suggestions!


r/learnphp • • May 29 '20

How do I do a POST request in PHP?

3 Upvotes

I'm looking at wordpress woocommerce plugins and on this one is this a POST or a GET request? How do I tell?

protected function generate_vm_request($order_id){
        $order  = wc_get_order( $order_id );

        try {
            $post_data = array();
            $post_data['x_account_id'] = $this->test_api_key;
            $post_data['x_amount'] = $order-> get_total() ;
            $post_data['x_currency'] = version_compare( WC_VERSION, '3.0.0', '<' ) ?
            $query1 = implode('', array_map(
                function ($v, $k) {
                    return $k.$v;
                },
                $post_data,
                array_keys($post_data)
            ));

            $vm_query  =  http_build_query($post_data, '', '&');

            $vm_url = $this->VMURL.$vm_query;
            header('Location:'.$vm_url );
        }
        catch ( Exception $e ) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }

r/learnphp • • May 23 '20

Illegal length modifier specified

3 Upvotes

I started getting this error in my Drupal 8 site when I try to load a few different admin pages after clearing the site cache.

Fatal error: Illegal length modifier specified 'f' in s[np]printf call in /public_html/mysite.com/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php on line 88

Line 88 is a call to the php rename function.

$result = rename($temporary_path, $full_path);

Here are examples of inputs that throw the error.

$temporary_path = sites/default/files/php/twig/.Lmc1W8Ah3a0Ti25OSi6EWRhRrak
$full_path = sites/default/files/php/twig/5eb6d88f951ea_input.html.twig_BDyoxtSNo6EAtdqLkk7vWqrEZ/Y3dUT4hvb70S6IlToElkUoP3liser_VNlYRsxFliuEg.php

The rename() function in the MTimeProtectedFastFileStorage.php file is part of Drupal's caching system. When the site cache is cleared Drupal rebuilds these php twig cache files on page loads as needed. The error seems to occur while rebuilding only certain twig files. After some testing I do not believe the issue has anything to do with file/directory name character length, illegal characters, or file permissions. The rename() function actually successfully renames the file, but then the script is apparently stopped by the spprintf error.

The error does not even seems to not be a normal php error but instead an "internal" php error thrown by the spprintf function as seen on line 741 in this github repository.

php_error(E_ERROR, "Illegal length modifier specified '%c' in s[np]printf call", *fmt);

https://github.com/php/php-src/blob/master/main/spprintf.c

I am not sure why the rename() function throws the error after the task is performed correctly, and I am not sure what the spprintf() functions has to do with the rename() function. I'm not even sure how to troubleshoot this issue because the error doesn't seem to be a normal php error but something that happens "internally". Any insight into this matter would be greatly appreciated. I don't have any idea how to move forward or go about fixing or even testing this issue.


r/learnphp • • May 11 '20

Is there php equivalent of pythons executemany() method for database query?

3 Upvotes

As the title say, if anybody has experience in Python


r/learnphp • • Apr 26 '20

Notice: Trying to get property 'num_rows' of non-object in search bar

3 Upvotes

For some reason I'm getting: Notice: Trying to get property 'num_rows' of non-object but, This works when I search in in a different column but to changed it from trackname to songname and now it doesn't work. Any seggustions?

//Preform full text search
$query = "SELECT * FROM song WHERE MATCH(songname) AGAINST('".$keyword."'IN NATURAL LANGUAGE MODE)";
$statement = $connection->prepare($query);
$statement->execute();
$results = $statement->get_result();

if($results->num_rows > 0) {
  while ($row = $results->fetch_assoc()) {
    echo '<br />'.$row['songname'];
   }
 }

}

r/learnphp • • Apr 16 '20

Admin page coming along, but still looking.

3 Upvotes

Hey everyone,

I am creating a WordPress theme and want show what I have done so far. I already have the design mock of the theme so I decided to work on the backend portion. I am working on the Advanced admin page for personal information that will work in a sidebar and shows a preview in the admin section.

Check it out

Have if work ahead of me with this theme, trying to find a job, school work, and overall family matters.

On that note,

If anyone has any remote job suggestions for an intermediate Frontend, UI Dev, Web Developer/Designer with a small amount of experience let me know. Even internships are cool. Just trying to get more experience and not feel like an impostor.

Merci beaucoup!


r/learnphp • • Apr 06 '20

WordPress - Backward Compatible

3 Upvotes

Hey all.

So I am learning WordPress Theme Development and came across something on WordPress.org about backward compatibility. I went to look at the functions that go into creating partial templates like: [wp_body_open()](https://developer.wordpress.org/reference/functions/wp_body_open/). On this page, they have a comment that talks about backward compatibility. They have a function like this:

<?php 
        if ( function_exists( 'wp_body_open' ) ) {
            wp_body_open();
        } else {
            do_action( 'wp_body_open' );
        }
    ?>

I kind of understand what it does but, I want to know how and why this is considered backward compatible?

Thanks in advance.


r/learnphp • • Mar 31 '20

How to include http request library in simple script?

3 Upvotes

SOLVED!! thanks. Hey guys, JS dev here who inherited some PHP code. The php code is simple single file php scripts that sit in a ubuntu's www/ folder.

I need to modify some code and want to use http library (Guzzle) to make some http requests but am unsure how to include the library in my script. All google searches point me to composer to "add it to project" but I only have the single file.

Your help is very appreciated.


r/learnphp • • Mar 26 '20

Checkboxes and Select-fields. How do I make the Select-form work on top of Checkboxes?

3 Upvotes

Right now, the user can fill in all the checkboxes they're interested in, and also choose an option in the select-field. However, whenever I use checkboxes, it's as if the Select-option doesn't matter any more.

Now matter what I choose as an Option in the Select when I've got a Checkbox filled in, the query still fetches all records, as long as they are = what checkboxes has been set.

Why is this? how can I make the Select more important, and stand "on top" of the checkboxes?


r/learnphp • • Mar 24 '20

How can I use LIKE in prepares statement?

3 Upvotes

Here's my code: https://pastebin.com/tfTzti5N

I have 5 columns called s1-s5, and I'm trying to search each one to see if they have a partial match against the $show variable.

I keep getting error: Number of variables doesn't match number of parameters in prepared statement on line 9.

Everything works fine if I use regular ? instead of '%?%'


r/learnphp • • Mar 22 '20

Can I get a variable out of a checkbox value?

3 Upvotes

Hi!

I've currently got a list containing 5 checkboxes that users can check, and my purpose for this is to return different rows from the database, depending on which of the checkboxes the users have checked.

I was thinking if it works to get a checkbox value, make it into a variable, and then use that variable to compare with the database, and if so, how do I do this?

If not, how would you suggest I do?

I have other lists of checkboxes that the users can check too. There are 6 options in total, and each of these options have their own unique checkboxes that are then checked against the database.

Can anyone help me out there? :)


r/learnphp • • Mar 04 '20

How can I enter an image in MySQL and check it?

3 Upvotes

The only thng i have is a my table on the database with the following fields: id, username and userimage(userimage as a blob)


r/learnphp • • Feb 24 '20

Is there a roadmap to learn?

3 Upvotes

Is there some kind of roadmap that says learn variables then after that flow then oop then design patterns, something in that kind of fashion?


r/learnphp • • Feb 20 '20

Trying to hire a NEW programmer to replace OLD one... issue with hiring

3 Upvotes

So I had one programmer who wrote the programmer but he is in school final right now and caught up in too much work. He is awesome.

I want to hire another programmer. Basically my plan was to hire maybe 3 or 4 of them and test them on a piece of work.

Basically get them to do something on my website. Get all of them to do the same exact thing. And who ever does it the best (understands me, quickly, and well).

However, I do not want them to break my website. Is there a way I can give them access to my site. Let them work on it. And I can view their edition?

And see if they can actually do the work?

Is there a way I can give them the site offline or something? Sorry I am NOT a programmer.


r/learnphp • • Jan 20 '20

PHPspreadsheet and JS: How to support arabic?

3 Upvotes

I'm getting data from the database, some strings are English, some are Arabic, some are half Arabic half English.

I'm generating the spreadsheet .xlsx like this

function writeToFile($spreadsheet, $filename)
{
    header("Content-Encoding: UTF-8");
    header('Content-Type: application/vnd.ms-excel; charset=UTF-8"');
    header('Content-Disposition: attachment; filename=' . $filename);
    $writer = new Xlsx($spreadsheet);

    $writer->save("php://output");
}

But I'm not downloading it directly, this generates a blob which gets downloaded using javascript.

fetch(url)
    .then(resp => resp.blob())
    .then(file => {
        if (file.size > 0) {
            const url = window.URL.createObjectURL(file);
            const a = document.createElement('a');
            a.style.display = 'none';
            a.href = url;
            a.download = filename;
            document.body.appendChild(a);
            a.click();
            window.URL.revokeObjectURL(url);
        } else {
             console.log('error');
        }
    })
    .catch(() => console.log('error'));

The problem is with PHPspreadsheet, I don't know if I fix that, I'll get another problem related to JS, but I do know that if I generate the file without using JS, I'll still have the Arabic issue.


r/learnphp • • Nov 28 '19

Something interesting with the splat operator that I found today

3 Upvotes

So I saw this on a presentation recently and thought I would share:

<?php

class Fruit
{
}

class Basket
{
    public $fruits = [];

    public function __construct(array $fruits)
    {
        $this->fruits = (function (Fruit ...$fruits) {
            return $fruits;
        })(...$fruits);
    }
}

$fruits = [new Fruit(), new Fruit(), new Fruit(), new Fruit(), 'test'];

new Basket($fruits);

So basically function (Fruit ...$fruits) checks the type of every single value even though the param is usable as an array inside the function.


r/learnphp • • Jul 07 '19

Help with getting form to work

3 Upvotes

I'm learning PHP and practicing some projects. I'm trying to get my form to send emails, but it's not working. In case it's relevant, I'm pasting the files to a fresh sub domain on my server.

I'm using Bootstrap for styling, but here's my code:

index.php <?php include('header.php') ?>

<div class="container">
  <h1>Hello, world!</h1>
  <form action="contactform.php" method="post">
    <div class="form-row">
      <div class="form-group col-md-6">
        <label for="inputName">Name</label>
        <input type="text" class="form-control" id="inputEmail4" name="name" placeholder="Name">
      </div>

      <div class="form-group col-md-6">
        <label for="inputPassword4">Subject</label>
        <input type="text" class="form-control" id="inputPassword4" name="subject" placeholder="Subject">
      </div>
      <div class="form-group col-md-6">
        <label for="inputEmail4">Email</label>
        <input type="email" class="form-control" id="inputEmail4" name="mail" placeholder="Email">
      </div>
      <div class="form-group">
        <label for="exampleFormControlTextarea1">Message</label>
        <textarea class="form-control" id="exampleFormControlTextarea1" name="message" rows="3"></textarea>
      </div>
    </div>
    <button type="submit" name="submit" class="btn btn-primary">Send</button>
  </form>
</div>

<?php include('footer.php') ?>

contactform.php <?php

    if (isset($_POST['submit'])) {
      $name = $_POST['name'];
      $subject = $_POST['subject'];
      $mailFrom = $_POST['mail'];
      $message = $_POST['message'];

      $mailTo = "[myemail]@yahoo.com";
      $headers = "From: " . $mailFrom;
      $txt = "You have received an e-mail from " . $name . ".\n\n" . $message;

      mail($mailTo, $subject, $txt, $headers);
      header("Location: index.php?mailsend");
    }
     ?>

Any help is really appreciated!


r/learnphp • • Apr 18 '19

Does echo know when to include '' on its own?

3 Upvotes

I notice echoing a variable into a CSS value works. Which I think means "" is omitted.

I wrote some php in html earlier today and I think it was possible to pass the string variable into an HTML attribute space directly, or inside a string template.

Is this accurate? Does PHP decide on its own when to include quotes to make a string work with CSS, HTML, and I assume also Javascript?


r/learnphp • • Mar 30 '19

Do composer dependencies have a build step?

3 Upvotes

Something equivalent to 'npm run build'?

If not what steps are needed to make the dependencies work anywhere?

I assume it would just work as is, if there is no built step?

And the "build" directory is just the same as the project root?


r/learnphp • • Mar 23 '19

PhpUnit: How to bring a function into a Test Class?

3 Upvotes

This doesn't work:

function indieFunction () {
    return 40;
  }
  
  class CalculatorTest extends \PHPUnit\Framework\TestCase {
    public function testIndieFunction () {
      $result = indieFunction();
      $this.assertEquals(38, $result);
    }    
  }
There was 1 error:

1) CalculatorTest::testIndieFunction
Error: Call to undefined function assertEquals()

/var/www/html/wptest2/tests/CalculatorTest.php:37

Any way to bring it into the test class? If not, how would you restructure this for PHPUnit tests? My target code is a wordpress plugin (the above are unrelated and just for learning the pattern). WP plugins are written procedurally for the most part, I could change that if required though as I only ever wrote decent Javascript when Jest was supporting me.


r/learnphp • • Mar 22 '19

Can one pass a javascript variable's value directly into PHP?

3 Upvotes

Is there a way to run a script inside some script tags and have it pass into PHP? I suspect not and that using localstorage or a database entry is the way to go but thought I'd ask.


r/learnphp • • Feb 05 '19

Looking for a good tutorial to create an oop black jack game specifically in php?

3 Upvotes

Im looking for a good oop tutorial blackjack or other card game that creates the game using php and oop design principles.

I know it is fairly similar in other languages but would really prefer if I can find a php tutorial .

Thanks