Page 1 of 1

display results in table

Posted: Tue Sep 26, 2006 10:55 pm
by waradmin
I want to generate a page of photos from my database.

I will have code similar to:

Code: Select all

$result = mysql_query("SELECT * FROM photos WHERE global_id='$_GET['id']'") or die(mysql_error()); 
$num_rows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
Then I want it to create a table with 4 cells in each row, once there have been 4 <td>(image)</td>'s, go to the next row (<tr>) and repeat.

I assume it will start with something like:

Code: Select all

$i = 0;
while($i < $num_rows) {
//some code here to display first <tr> then 4 <td></td>'s, then the end </tr>, then repeat
}
So what would the code be that controls the maximum of 4 <td>'s and a <tr> at the beginning and </tr> at the end, then going to a new <tr>?

Thanks in advance, I googled and found nothing.

-Steve

Posted: Tue Sep 26, 2006 10:59 pm
by shneoh
a dummy one:

Code: Select all

<table>
<?php
$i = 0;
while($i < $num_rows) {
    if($i ==4){
        $i=0;
        echo "</tr>";
    }
    if($i ==0)
        echo "<tr>";
    echo "<td><img ....></td>";
    $i++;
}
?>
</table>

Posted: Tue Sep 26, 2006 11:41 pm
by waradmin
alright, it works, now how would I do a query to:

get the album ($_GET['album']) and the pictures with it ($_GET['id'])

I got

Code: Select all

$result = mysql_query("SELECT * FROM photos WHERE global_id='{$_GET[id]}' AND album_id='{$_GET[album]}' ORDER BY id desc") or die(mysql_error());
but it displays all the pictures and not just the ones that match up the global id and album (from the address)

(ex address: http://______.com/photos.php?album=1&id=830203)

Thanks.

Posted: Tue Sep 26, 2006 11:46 pm
by shneoh
I am not familiar with the working on {}. Here's my way to do, you can try.

Change this line: global_id='{$_GET[id]}' AND album_id='{$_GET[album]}'

To this: global_id='".$_GET[id]."' AND album_id='".$_GET[album]."'

waradmin wrote:alright, it works, now how would I do a query to:

get the album ($_GET['album']) and the pictures with it ($_GET['id'])

I got

Code: Select all

$result = mysql_query("SELECT * FROM photos WHERE global_id='{$_GET[id]}' AND album_id='{$_GET[album]}' ORDER BY id desc") or die(mysql_error());
but it displays all the pictures and not just the ones that match up the global id and album (from the address)

(ex address: http://______.com/photos.php?album=1&id=830203)

Thanks.

Posted: Tue Sep 26, 2006 11:55 pm
by waradmin
That worked perfectly.

Thank you very much for the help.

-Steve