Page 1 of 1

Loop Question

Posted: Sat Oct 31, 2009 5:04 pm
by MiniMonty
Hi all,
bit of a newb at all this but having a lot of fun learning PHP and MySQL !

Need a bit of help with a loop issue.
I have a DB with a table called "pictures".
The table has a row "IID" (image ID) and a row "UID (user ID).
When users upload images the IID increments and the UID records which user upload the image.
Users are recorded by their member number.
So it looks like this:

Image

I'm trying to build galleries which display all the images uploaded by a certain user.
A logged in user is identified by "$id" and after some bits and bobs for sessions etc., my php says

Code: Select all

 
$sql = mysql_query("SELECT * FROM pictures WHERE UID='$id'");
 
and now I need to loop through and build a table of the images that user "$id" has uploaded.
And that's where I'm stuck.
My experience is with Flash and as such I would naturally use a 'for' loop but the syntax I know in Flash
is different and I just can't make that work.

All and any advice / code / tutorials on how to extract the info and build a table of images in the html
very much appreciated !

Best wishes
Monty

Re: Loop Question

Posted: Sat Oct 31, 2009 5:11 pm
by requinix
for loops

However a while loop, demonstrated here in example #2, will be a better choice.

Re: Loop Question

Posted: Sat Oct 31, 2009 8:10 pm
by MiniMonty
Thanks for the reply - care to expand on that at all ?

seeing my table structure and having read what I'm trying to do do you have any code I (as a newb) could use as a staring point ?

I'm not asking anyone to do it all for me (because I'm enjoying learning) but the manual is very "dry"
if you get my drift and a bit of human conversation over the issues involved often helps a LOT.

Best wishes
Monty

Re: Loop Question

Posted: Sat Oct 31, 2009 9:30 pm
by requinix
Okay.

So you've got your query. You ran it through mysql_query: $sql is a "handle" you can use to get the results.

mysql_fetch_assoc is one way to get them. Give it $sql and it will give you an array of results with the keys being the column names and the values being the... well, the values. There's a few other mysql_fetch_* functions you can use if you don't want that one.

So

Code: Select all

$sql = mysql_query("SELECT * FROM pictures WHERE UID='$id'");
$first = mysql_fetch_assoc($sql);
$second = mysql_fetch_assoc($sql);
$third = mysql_fetch_assoc($sql);
// ...
One problem: you don't know when to stop. You could use mysql_num_rows and a for loop, but the simplest solution is to realize that mysql_fetch_assoc will return false when it runs out of rows.

Code: Select all

$sql = mysql_query("SELECT * FROM pictures WHERE UID='$id'");
 
while (($row = mysql_fetch_assoc($sql)) !== false) {
    // do something with this row
}
A more detailed example would be

Code: Select all

$sql = mysql_query("SELECT * FROM pictures WHERE UID='$id'");
 
echo "<table>";
while (($row = mysql_fetch_assoc($sql)) !== false) {
    echo "<tr>";
    echo "<td>", $row["field1"], "</td>";
    echo "<td>", $row["field2"], "</td>";
    echo "<td>", $row["field3"], "</td>";
    echo "</tr>";
}
echo "</table>";