Welcome to WebmasterWorld Guest from 54.198.87.238

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

while inside while

     
8:45 pm on Sep 1, 2008 (gmt 0)

10+ Year Member



I'm trying to build a php statement that will display my forum's latest threads and attachments. Is there a problem with the while inside another while? It's only bringing up one result.


<?php
$thread_result = mysql_query("SELECT * FROM thread ORDER BY dateline DESC LIMIT 0,25", $connection);
if (!$thread_result) {
die("Database query failed: " . mysql_error());
}
$attach_result = mysql_query("SELECT * FROM attachment", $connection);
if (!$attach_result) {
die("Database query failed: " . mysql_error());
}
while ($thread = mysql_fetch_array($thread_result)) {
if ($thread[22] != 0) {
while ($attach = mysql_fetch_array($attach_result)) {
if ($attach[9] == $thread[2]) {
echo "
<a href=\"showthread.php?t={$thread[0]}\">$thread[1]<br>
<img src=\"attachment.php?attachmentid={$attach[0]}\"></a><br>";
}
}
}
else {
echo "<a href=\"showthread.php?t={$thread[0]}\">$thread[1]<br>";
}
}
?>
8:55 pm on Sep 1, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Once you've fetched through once, you would need to reset the record pointer on $thread with mysql_data_seek() [php.net].

Personally I would either read the threads into an array or do a single query with a JOIN.

9:36 pm on Sep 1, 2008 (gmt 0)

10+ Year Member



Where would you put the reset? mysql_data_seek()
11:10 pm on Sep 1, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



while ($thread = mysql_fetch_array($thread_result)) {
if ($thread[22] != 0) {
mysql_data_seek($attach,0);
while ($attach = mysql_fetch_array($attach_result)) {
if ($attach[9] == $thread[2]) {
 

Featured Threads

Hot Threads This Week

Hot Threads This Month