Forum Moderators: coopster

Message Too Old, No Replies

problem about php

         

derek123

11:21 pm on Sep 25, 2004 (gmt 0)

10+ Year Member




"while (mysql_fetch_array($result)){ "

lets say "$result" contains 9 pictures.and i want to print it out in matrix way...that is 3x3.so..should i use another loop to get the expected result? please show me the code. Thanks.

mincklerstraat

9:29 am on Sep 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi derek, welcome to webmasterworld! [webmasterworld.com].
It'd probably be easier for me now just to write out the code you want, but I won't do that, since this forum is here to help people learn php, and not write the php for them. So I'll just describe one way you could do this.

In this case, I'd set up an array $pics and in your foreach loop, just add the result as an element to the array. Then I'd do one of the two:
a) just echo out the table or other html with all 9 pictures in the same place (do this if b looks too difficult)
b) set up a 'while' loop or some other kind that will repeat three times, set up a counter starting at zero, echo out each picture w/ html as $pics[$counter] $pics[($counter + 1)] $pics[($counter + 2)] (appropriate HTML in between - look up 'tables' in an html tutorial if you have no idea how to do this), and then increment the counter by three - $counter = $counter + 3;

derek123

10:43 am on Sep 26, 2004 (gmt 0)

10+ Year Member



Hi min,
thank you for your reply.

my idea is something like this:

print "<table border=1>";
while ($qry = mysql_fetch_array($result)) {
print "<tr>";
$counter = 0;
if ($counter < 4){
print "<td>";
print "<IMG SRC=\"$qry[picture]>";
print "</td>";
$counter = $counter + 1;
print "</td>";
}
print "</tr>";
}
print "</table>";

but it seems not work as i expected.

mincklerstraat

11:53 am on Sep 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've got a good bit of the code you need here - I'll be lazy and give you some code - it uses 'for', which is sorta like 'while' - see [be2.php.net...] , the php manual is your best friend.
if($result){ // prevents you from getting errors if there's an empty result
while ($qry = mysql_fetch_array($result)) {
echo "\n".'<tr>'; //"\n" makes a linebreak in your HTML source, always a good practice for html debugging - note, needs to be in double brackets
for($counter = 0; $counter < 3; $counter +1){
echo '<td><img src="'.$qry['picture'].'" alt=""></td>';
} // (end for loop)
echo '</tr>';
} // (end while loop)
} // (end if clause)

Try this; it isn't tested. Have fun. Thx for posting a little code, makes me feel I'm not doing all your homework!

derek123

12:31 pm on Sep 26, 2004 (gmt 0)

10+ Year Member



um....i had try your method before...after tested..
the result become:

000
111
222
...
...

mincklerstraat

5:56 pm on Sep 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



posting a bit of your html source (not everything if it's on another page) might help me see what could be wrong.

derek123

10:44 pm on Sep 26, 2004 (gmt 0)

10+ Year Member



I fixed the problem already....
thanks for your reply~