6

I'm trying to make a dynamic table with PHP. I have a page which displays all the pictures from a database. I need the table to be of 5 columns only. If more than 5 pictures are returned, it should create a new row and the displaying of the rest of the pics would continue.

Can anyone please help?

Codes go here: Code in the main page:-

    <table>
    <?php
        $all_pics_rs=get_all_pics();
        while($pic_info=mysql_fetch_array($all_pics_rs)){
        echo "<td><img src='".$pic_info['picture']."' height='300px' width='400px' /></td>";
            } 
?>
</table>

The get_all_pics() function:

$all_pics_q="SELECT * FROM pics";
        $all_pics_rs=mysql_query($all_pics_q,$connection1);
        if(!$all_pics_rs){
            die("Database query failed: ".mysql_error());
        }
        return $all_pics_rs;

This code is creating a single row. I can't think of how I can get multiple rows ... !!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
maxxon15
  • 1,559
  • 4
  • 22
  • 35
  • 2
    Just FYI - you're being downvoted because you didn't post any code. – Drew Oct 25 '11 at 08:07
  • If you can edit this question to provide relevant code examples of what you've tried, please flag it for moderator attention to be reviewed. To edit, just click the 'edit' link under your question. To flag it, click the 'flag' link, select 'other' and let us know it's ready to be reviewed. – Tim Post Oct 25 '11 at 11:46
  • @Tim Post: I don't really agree on your action. I think his question is really clear. I've seen wayyyyy worse questions on this board. – Jules Oct 25 '11 at 11:52
  • @Jules I was on the fence about it (flags were accurate), I'll re-open for now. – Tim Post Oct 25 '11 at 11:55
  • @Tim Post : Well, Ok I'll do that ... I'm new here ... didn't understand that it was absolutely necessary to post the code. – maxxon15 Oct 25 '11 at 12:30
  • @maxxon15 It's not absolutely necessary, but it does help us identify where you're getting stuck. It also helps differentiate your question from many that basically want people to do the work for them. After reading your question several times, I realized that your question is _not_ an example of that, but it did take several times :) – Tim Post Oct 25 '11 at 12:43
  • @Tim Post: Ohk!!! I'll keep that in mind from now on... :) btw, I've updated the question now. – maxxon15 Oct 25 '11 at 14:06

2 Answers2

13
$maxcols = 5;
$i = 0;

//Open the table and its first row
echo "<table>";
echo "<tr>";
while ($image = mysql_fetch_assoc($images_rs)) {

    if ($i == $maxcols) {
        $i = 0;
        echo "</tr><tr>";
    }

    echo "<td><img src=\"" . $image['src'] . "\" /></td>";

    $i++;

}

//Add empty <td>'s to even up the amount of cells in a row:
while ($i <= $maxcols) {
    echo "<td>&nbsp;</td>";
    $i++;
}

//Close the table row and the table
echo "</tr>";
echo "</table>";

I haven't tested it yet but my wild guess is something like that. Just cycle through your dataset with the images and as long as you didn't make 5 <td>'s yet, add one. Once you reach 5, close the row and create a new row.

This script is supposed to give you something like the following. It obviously depends on how many images you have and I assumed that 5 (defined it in $maxcols) was the maximum number of images you want to display in a row.

<table>
    <tr>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
    </tr>
    <tr>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;<td>
    </tr>
</table>
Jules
  • 7,148
  • 6
  • 26
  • 50
  • 1
    You have defined `i` outside of the for, so just use it to fill it up with `for(;$i < 5 /* magic number! bad! */; ++$i){){ echo ""; }` between the closing `}` and `echo "";`. No need to divide or anything. – RedX Oct 25 '11 at 11:10
  • @RedX: Yes you're right. That's an easier way to do it. Going to edit my answer accordingly. – Jules Oct 25 '11 at 11:16
3
$max_per_row = 5;
$item_count = 0;

echo "<table>";
echo "<tr>";
foreach ($images as $image)
{
    if ($item_count == $max_per_row)
    {
        echo "</tr><tr>";
        $item_count = 0;
    }
    echo "<td><img src='" . $image . "' /></td>";
    $item_count++;
}
echo "</tr>";
echo "</table>";
Tom
  • 3,031
  • 1
  • 25
  • 33