Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what you want to do. It supports any number of rows and outputs 4 at a time. If there are 15 items for example, it outputs 4 in first three rows and 3 in the last one.</p> <pre><code>echo"&lt;table border=1&gt;"; $images = array(); $names = array(); while ($row = mysql_fetch_array($content)) { $images[] = $row['image']; $names[] = $row['name']; } while(!empty($images)) { echo "&lt;tr&gt;"; foreach($images as $count=&gt;$image) { echo "&lt;td&gt;&lt;img src='".$image."' width='100'&gt;&lt;/td&gt;"; if($count==3) { $images = array_slice($images, 4); break; } } echo "&lt;/tr&gt;&lt;tr&gt;"; foreach($names as $count=&gt;$name) { echo"&lt;td&gt;" . $name . "&lt;/td&gt;"; if($count==3) { $names = array_slice($names, 4); break; } } echo "&lt;/tr&gt;"; } echo"&lt;/table&gt;"; </code></pre> <p>Here is a shorter version which does the same:</p> <pre><code>echo"&lt;table border=1&gt;"; //get images and names in two arrays $images = array(); $names = array(); while ($row = mysql_fetch_array($content)) { $images[] = "&lt;img src='".$row['image']."' width='100'&gt;"; $names[] = $row['name']; } while(!empty($images)) { //output images foreach(array($images, $names) as $items) { echo "&lt;tr&gt;"; foreach($items as $key=&gt;$item) { echo "&lt;td&gt;$item&lt;/td&gt;"; //output only four of them if($key==3) { break; } } echo "&lt;/tr&gt;"; } //remove the first four images from $images because they're already printed $images = array_slice($images, 4); $names = array_slice($names, 4); } echo"&lt;/table&gt;"; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload