PHP shows data

I show images that 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 time to move to next row
}

My question is: after displaying 4 columns, how can I go to another row? (4 images per line)

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

thanks

+4
source share
2 answers

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 PHP PDOfor your mysql access on google;)

And run the loop (for) at 0?

+4
source

Here's how I 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 variable is $kcounted to 4 and reset every time. While it is reset, it also ends and creates a new row in the table.

0
source

Source: https://habr.com/ru/post/1607620/


All Articles