homepage Welcome to WebmasterWorld Guest from 54.167.173.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
while inside while
matthewamzn




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

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>";
}
}
?>

 

cameraman




msg:3736154
 8:55 pm on Sep 1, 2008 (gmt 0)

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.

matthewamzn




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

Where would you put the reset? mysql_data_seek()

cameraman




msg:3736213
 11:10 pm on Sep 1, 2008 (gmt 0)

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]) {

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved