I am trying to make to display cells in a database that are not empty, i have URL links with images inside the table and i want to display only the cells that are not empty.

For example if there is only 2 pictures added in the database i want to display only those 2, and not the rest 3 with no images

I have this code but its going on forever and its crashing my browser.

<?php
$sql = "SELECT pic1, pic2, pic3, pic4, pic5 FROM modelprofile WHERE name='$name'";
$result = $conn->query($sql);

if($result === false) {
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
    while($row = $result->fetch_assoc()) {
        $pic1 = $row["pic1"];
        $pic2 = $row["pic2"];
        $pic3 = $row["pic3"];
        $pic4 = $row["pic4"];
        $pic5 = $row["pic5"];
        while(!empty($row)) {


        ?>
        <img class="card-img-top" src="<?= $pic1 ?>" alt="">
        <br><br>
        <img class="card-img-top" src="<?= $pic2 ?>" alt="">
        <br><br>
        <img class="card-img-top" src="<?= $pic3 ?>" alt="">
        <br><br>
        <img class="card-img-top" src="<?= $pic4 ?>" alt="">
        <br><br>
        <img class="card-img-top" src="<?= $pic5 ?>" alt="">
        <br><br><br>

        <?php
        }
    }
}
?>
Muyi_1 commented: Good day +0

Recommended Answers

All 7 Replies

OFF TOPIC
Through the years i asked so many questions here but never considered donating. Now, I promise if i succeed with this method, i am gonna pay a whole year hosting + donate;
Since i am seeing guys making crazy money per day with this method i though ill give it a try.

commented: Did you donate yet? ;-) +1

Here you go:

<?php
$sql = "SELECT pic1, pic2, pic3, pic4, pic5 FROM modelprofile WHERE name='$name'";
$result = $conn->query($sql);

if($result === false) {
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
    while($row = $result->fetch_assoc()) {
        while(!empty($row)) {
            for ($pic_counter = 1; $pic_counter <= 5; $pic_counter++) {
               $pic = $row['pic' . $pic_counter];
                if (!empty($pic)) {
                    echo '<img class="card-img-top" src="' . $pic . '" alt=""><br><br>';
                }
            }
        }
        echo '<br>';
    }
}
?>

Hope this helps!

Thank you for your answer but this doesnt solve. It again crashing my browser

Oh sorry i removed the 2nd while loop and it works. BIG thanks

That's on me; my bad. In my hurry to help you succeed and donate, I did not test the code. I also overlooked the fact that your query had a WHERE clause. That means that, ideally, only one row will be fetched.

So because you are only fetching a single row from your table, you only need to parse through the pictures in that row. The while loops are therefore unnecessary.

<?php
$sql = "SELECT pic1, pic2, pic3, pic4, pic5 FROM modelprofile WHERE name='$name'";
$result = $conn->query($sql);

if($result === false) {
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
    $row = $result->fetch_assoc();

    for ($pic_counter = 1; $pic_counter <= 5; $pic_counter++) {
        $pic = $row['pic' . $pic_counter];

        if (!empty($pic)) {
            echo '<img class="card-img-top" src="' . $pic . '" alt=""><br><br>';
        }
    }

    echo '<br>';
}
?>

Note that the structure of your table allows for different girls to have the same name because the name column of your modelprofile table is not unique. The code snippet above will not help you with that because it will stop after the first match. So if you want to show the pictures for all girls with the same (or similar) name, you could use the snippet below. Now you do get to use the WHILE loop because you must traverse all the items in $result->fetch_assoc()

<?php
// Fetches girls with names that match $name
$sql = "SELECT pic1, pic2, pic3, pic4, pic5 FROM modelprofile WHERE name LIKE '%$name%'";

$result = $conn->query($sql);

if($result === false) {
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
    while ($row = $result->fetch_assoc()) {
        for ($pic_counter = 1; $pic_counter <= 5; $pic_counter++) {
            $pic = $row['pic' . $pic_counter];
            if (!empty($pic)) {
                echo $pic . ":not empty";
                echo '<img class="card-img-top" src="' . $pic . '" alt=""><br><br>';
            }
        }
        echo '<br>';
    }
}
?>

I tested this, btw:

/show.php:12:
array (size=6)
  'name' => string 'girl1' (length=5)
  'pic1' => string 'pic1' (length=4)
  'pic2' => string 'pic2' (length=4)
  'pic3' => null
  'pic4' => string 'pic4' (length=4)
  'pic5' => string 'pic5' (length=4)

pic1:not empty

pic2:not empty

pic4:not empty

pic5:not empty


/show.php:12:
array (size=6)
  'name' => string 'girl2' (length=5)
  'pic1' => null
  'pic2' => string 'pic2' (length=4)
  'pic3' => string 'pic3' (length=4)
  'pic4' => null
  'pic5' => null

pic2:not empty

pic3:not empty


/show.php:12:
array (size=6)
  'name' => string 'girl3' (length=5)
  'pic1' => null
  'pic2' => null
  'pic3' => string 'pic3' (length=4)
  'pic4' => null
  'pic5' => string 'pic5' (length=4)

pic3:not empty

pic5:not empty


/show.php:12:
array (size=6)
  'name' => string 'girl3' (length=5)
  'pic1' => string '2' (length=1)
  'pic2' => string '3' (length=1)
  'pic3' => string '4' (length=1)
  'pic4' => null
  'pic5' => null

2:not empty

3:not empty

4:not empty

Wow very detailed answer thank you

How do you test this stuff to get the info you provided above?

To test, first I create a test database and then use your core.php create table query. Then I insert dummy rows. Finally, I use var_dump() and echo to make sure I get the expected results. During all this, I have error reporting on, so I can see if something is not working as expected.

For example, to see the contents of $row, I used var_dump($row); right after Line 10 (while). And, of course Line 14, which just shows that the current $pic is not empty, which I left behind but should not be there in the final snippet.

This is just one way of testing (debugging) code. There are other, more complex, ways of doing so. Some PHP frameworks will have a development mode that shows you extremely detailed debug information when your code (PHP, MySQL queries, etc.) fails to execute properly.

Finally, whether you realize it or not, asking the question: "why is this not working?" is the first step in debugging. Hopefully the way we walk you through these solutions should help you take additional debugging steps before you reach out in frustration over your code not working ;-)

Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.