Home > Net >  Filter PHP array ghost values?
Filter PHP array ghost values?

Time:10-07

So I've got some PHP code that I can't for the life of me seem to figure out. Basically, I have two arrays: images, and videos. Both of these arrays are exploded values of strings retrieved from the database (for example: column imgArray contains value: "img1.png, img2.png, img3.png" and then once the data is retrieved from the database, I'm using the explode() function to combine these values into an array so I get each individual value: img1.png - img3.png and can use them accordingly.)

Anyway, the row/col value in MySQL will show exactly what I want. That's fine. If there is nothing in the array, the value in the table defaults to MySQL's "NULL" value. However, when I iterate over the values in the array inside my PHP code, some weird results occur. I get ghost values, that don't even seem to show up when I use var_dump(). This causes my echo statement in my iteration/loop to display blank images (because there obviously is no "null.png" or "null.mp4" files on my server). I've tried filtering the values strictly in an IF () statement inside each loop, so it simply ignores any ghost/null values, but it does nothing. I have never seen an issue like this before.

Exploding values and creating each array:

$images = $row["imgArray"]; // table row
$images = explode(", ", $images);
$videos = $row["videoArray"]; // table row
$videos = explode(", ", $videos);

Loops:

$num_vids = count($videos) - 1; // Starts at 1 so -1 to start at 0 for loop/array
echo var_dump($images); // Diag
for ($i = 0; $i <= $num_vids; $i  ) {
    if ($videos[$i] !== null || $videos[$i] !== NULL || $videos[$i] !== "" || $videos[$i] !== "null" || $videos[$i] !== "NULL") { 
        $vid = $videos[$i];
        echo
        "
         <video class='media_content video' onclick=\"expandVideo('$vid');\">
             <source src='/videos/001/$vid' type='video/mp4'/>
         </video>
        ";
    }
}

$num_imgs = count($images) - 1; // Starts at 1 so -1 to start at 0 for loop/array
echo var_dump($images); // Diag
for ($i = 0; $i <= $num_imgs; $i  ) {
    if ($images[$i] !== null || $images[$i] !== NULL || $images[$i] !== "" || $images[$i] !== "null" || $images[$i] !== "NULL") {
        $img = $images[$i];
            echo "<a href='/images/001/$img'><img class='media_content' src='/images/001/$img' decoding='async'/></a>";
    }
}

These are the values that are in the SQL table that I am using as placeholders:

SQL ROW 1:
col "imgArray": *NULL*
col "videoArray": `_user_1_submitted_44297_video_.mp4, _user_1_submitted_55522_video_.mp4, _user_1_submitted_39893_video_.mp4`

SQL ROW 2:
col "imgArray": `_user_1_submitted_64424_image_.jpg, _user_1_submitted_17426_image_.jpg, _user_1_submitted_93363_image_.PNG`
col "videoArray": *NULL*

This is the var_dump() information for each post:

Post 1 (for images):

array(1) { [0]=> string(0) "" }

Post 2 (for videos):

array(3) { [0]=> string(34) "_user_1_submitted_64424_image_.jpg" [1]=> string(34) "_user_1_submitted_17426_image_.jpg" [2]=> string(34) "_user_1_submitted_93363_image_.PNG" }

Here are some images to visualize the issue (Don't mind the images used, they're just placeholders I'm using lol):

enter image description here enter image description here enter image description here

With all this in mind, what exactly could be causing the ghost values that cause my echo statements to try to display HTML code with blank/null values? That don't even exist as values in my SQL table?

CodePudding user response:

The comparison statements are having logical problem for this case.

Hence, change this line

  if ($images[$i] !== null || $images[$i] !== NULL || $images[$i] !== "" || $images[$i] !== "null" || $images[$i] !== "NULL") {

to

  if ($images[$i] !== null && $images[$i] !== NULL && $images[$i] !== "" && $images[$i] !== "null" && $images[$i] !== "NULL") {

and

Change this line

 if ($videos[$i] !== null || $videos[$i] !== NULL || $videos[$i] !== "" || $videos[$i] !== "null" || $videos[$i] !== "NULL") { 

to

if ($videos[$i] !== null && $videos[$i] !== NULL && $videos[$i] !== "" && $videos[$i] !== "null" && $videos[$i] !== "NULL") { 

CodePudding user response:

It's a shot in the dark, but try doing something like this instead:

<?php
    // Ensure the returned value from the database is ONLY of type string AND
    // the string isn't empty.
    if(is_string($videos) && trim($videos) !== ""){
        // Split the file names into their own array.
        $videoFiles = explode(", ", $videos);

        // Iterate over each file name.
        foreach($videoFiles as $videoFile){
?>
    // You can use <?= to echo out values from PHP
    <video  onclick="expandVideo('<?= $videoFile ?>')">
        <source src="/videos/001/<?= $videoFile ?>" type="video/mp4" />
    </video>
<?php   
        }
    }

// Same thing for images...

CodePudding user response:

Your sql data screenshot makes it clear that of the two records shown, one has no value for imgArray.

Now let's look at how you're handling this:

    if ($images[$i] !== null || $images[$i] !== NULL || $images[$i] !== "" || $images[$i] !== "null" || $images[$i] !== "NULL") {

Or? Since $images[$i] can only match one of them, that statement will always evaluate to true.

You want all of those checks to be true, not just any of them. You want &&, not ||.

  • Related