0

I have this script that displays a max of 5 images for each row, but for some reason my <ul> tag won't close correctly if the number of items isn't an exact multiple of 5. How can I correct this problem so the <ul> tag will close even if the number of listed images is less then 5?

Here is my PHP code.

if (!$dbc) {
    print mysqli_error($mysqli);
} else {
    $row_count = 0;
    while($row = mysqli_fetch_array($dbc)){ 
        if($row_count % 5 == 0){
         echo "<ul>";
        }
       echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
        echo "<img src='".$row['src']."'></a></li>";
       if($row_count % 5 == 4) {
         echo "</ul>";
       }
       $row_count++;
    }

}
7
  • 4
    (tipp) printf can make your code a lot more readable Commented Jun 30, 2010 at 8:11
  • Or alternatively, do this: echo "<li><a href='{$row['url']}'</a></li>"; Commented Jun 30, 2010 at 8:12
  • @Gordon how will print_f help me with this problem? Commented Jun 30, 2010 at 8:16
  • nope, the printf is just kind of equivalent for echo, while your problem is about logics. Commented Jun 30, 2010 at 8:18
  • 1
    @sIK It's just a general tipp to improve your code legibility; a sidenote. It wasn't meant to help with your actual problem. Commented Jun 30, 2010 at 8:21

4 Answers 4

3

below the loop, check if

if (!$dbc) {
    print mysqli_error($mysqli);
} else {
    $row_count = 0;
    while($row = mysqli_fetch_array($dbc)){ 
        if($row_count % 5 == 0){
         echo "<ul>";
        }
       echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
        echo "<img src='".$row['src']."'></a></li>";
       if($row_count % 5 == 4) {
         echo "</ul>";
       }
       $row_count++;
    }
    if ( (($row_count % 5) > 0) && (($row_count % 5) < 4))
        echo "</ul>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

exactly where below the loop?
below the closing "}" of "while", but of course inside "else" statement code. I've edited the answer to show you where exactly ;)
2
$multiple = false;

if (!$dbc) {
    print mysqli_error($mysqli);
} else {
    $row_count = 0;
    while($row = mysqli_fetch_array($dbc)){ 
        if($row_count % 5 == 0){
         echo "<ul>";
        }
       echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
        echo "<img src='".$row['src']."'></a></li>";
       if($row_count % 5 == 4) {
         $multiple = true;
         echo "</ul>";
       } else {
        $multiple = false;
       }
       $row_count++;
    }
    if($multiple == false) {
        echo "</ul>";
    }

}

Comments

0
if (!$dbc) {
print mysqli_error($mysqli);} else {
$row_count = 0;
//tank start
$total_rows = mysqli_num_rows($dbc);
//tank end
while($row = mysqli_fetch_array($dbc)){ 
    if($row_count % 5 == 0){
     echo "<ul>";
    }
   echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
    echo "<img src='".$row['src']."'></a></li>";
   //tank start
   if($row_count % 5 == 4 || $row_count==$total_rows) {
   //tank end
     echo "</ul>";
   }
   $row_count++;
}

Comments

0

Here's my updated solution. I think it looks a little clearer. I do setup a few variables to get rid of some IF statements, and replace them with FOR loops. Mainly for readability and it's the way i thought of doing it.

$itemsperrow = 5;
$items = mysqli_num_rows($dbc);
$rows = $items / $itemsperrow;
$itemcount = 0;

if (!$dbc)
{
    echo(mysqli_error($mysqli));
}
else
{
    for ($int = 0; $int < $rows; $int++)
    {
        echo "<ul>";

        for ($item = 0; $item < $itemsperrow; $item++)
        {
            if ($itemcount >= $items)
            {
                echo "</ul>";
                exit;
            }
            else
            {
                $row = mysqli_fetch_array($dbc);
                echo "<li><a href='". $row["url"] . "'>";
                echo "<img src='" . $row["src"] . "' title='" . $row["title"] . "'/></a></li>";
                $itemcount++;
            }
        }
        echo "</ul>";        
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.