display results in table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

display results in table

Post 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
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post 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>
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post 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.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

That worked perfectly.

Thank you very much for the help.

-Steve
Post Reply