Shaun
Shaun

Reputation: 153

PHP showing data

I am showing images which are stored in my DB.

See code below

$num_rows = mysql_num_rows($result41);   

for ($i = 1; $i <= mysql_num_rows($result41); $i++)
{
  $row = mysql_fetch_array($result41);
  $upload_id = $row ['upload_id'];
  $file = $row ['FILE_NAME'];
  echo"     
  <td>
    <a href='image.php?id=$upload_id&gallery=$id'><center>   
    <img src='uploads/$file' alt='$name Gallery' title='$name Album'  class='resize'>                       
  </td>";   
}

if ($i % 0 == 4) {
  echo '</tr>'; // it's time to move to next row
}

My question is, after showing 4 columns how do I move onto another row? (only 4 images per row)

I have if ($i % 0 == 4) in my script but doesn't seem to be working?

Thanks

Upvotes: 4

Views: 73

Answers (2)

razemauze
razemauze

Reputation: 2676

This is how I would do it:

$num_rows = mysql_num_rows($result41); 
$k = 1; 

for ($i = 1; $i <= mysql_num_rows($result41); $i++){
    $row = mysql_fetch_array($result41);
    $upload_id = $row ['upload_id'];
    $file = $row ['FILE_NAME'];
    echo"     
    <td>
        <a href='image.php?id=$upload_id&gallery=$id'><center>   
        <img src='uploads/$file' alt='$name Gallery' title='$name Album'  class='resize'>                       
    </td>";
    if($k == 4){
        $k = 1
        echo "</tr><tr>";
    } else {
        $k++;
    }
}

The $k variable counts to 4 and resets every time. While it resets, it also ends and creates a new row in the table.

Upvotes: 0

Servuc
Servuc

Reputation: 348

Like this :

for ($i = 1; $i <= mysql_num_rows($result41); $i++)
{
    $row = mysql_fetch_array($result41);
    $upload_id = $row ['upload_id'];
    $file = $row ['FILE_NAME'];
    if($i % 4 == 0) echo "<tr>";
    echo"     
    <td>
    <a href='image.php?id=$upload_id&gallery=$id'><center>   
    <img src='uploads/$file' alt='$name Gallery' title='$name Album'        class='resize'>                       
    </td>";
    if($i % 4 == 3) echo "</tr>"; 
}

And look at PHP PDO for your Mysql access into Google ;)

And start you loop (for) at 0 ?

Upvotes: 3

Related Questions