tags:

views:

104

answers:

4

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++;
    }

}
+3  A: 

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>";
}
migajek
exactly where below the loop?
sIK
below the closing "}" of "while", but of course inside "else" statement code.I've edited the answer to show you where exactly ;)
migajek
This didn't work :(
sIK
sIK, I'm afraid you have to learn PHP basics first.
migajek
Your code will nest my lists not what I need :(
sIK
+2  A: 
$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>";
    }

}
Olorin
@Olorin, Krazy this code worked. Thanks!
sIK
your welcome :D
Olorin
A: 
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++;
}
Tank
A: 

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>";        
    }
}
fortheworld