markbett Posted September 23, 2006 Share Posted September 23, 2006 my code (see below) should work as follows1) run a sql query and get get all the topics and their ID# (this returns 4 topics right now)2) while the topics are being returned 1 row at a time I take the topic ID and run a second SQL query using it to find all the links in the link table and echo them out3) once all the links are done, we should move on to the next topic and then so on doing the same thing for each of the 4 topicsthe problem is that this code is only working for the very first topic.... WHY!?!?![code]<?phpinclude $_SERVER['DOCUMENT_ROOT'].'/sbqa/layout2.php';$row=''; $sql = mysql_query("SELECT * FROM link_types"); while($row = mysql_fetch_array($sql)){ stripslashes(extract($row)); echo '<table border="0" align="center"> <tr> <td colspan="2" class="box_title">'.$type.'</td> </tr>'; $sql = mysql_query("SELECT * FROM links WHERE (type_1='$type_id' OR type_2='$type_id' OR type_3='$type_id') ORDER BY title"); while($row = mysql_fetch_array($sql)){ stripslashes(extract($row)); echo'<tr><td><a href="'.$url.'">'.$title.'</a></td><td>'.$description.'</td> </tr>'; $url=''; $title=''; $description=''; } echo '</table> <br /> <br />'; }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21739-sql-loop-not-looping/ Share on other sites More sharing options...
willfitch Posted September 23, 2006 Share Posted September 23, 2006 Can you tell me what it is doing? Perhaps viewing the source and telling me what is says? Quote Link to comment https://forums.phpfreaks.com/topic/21739-sql-loop-not-looping/#findComment-97062 Share on other sites More sharing options...
markbett Posted September 23, 2006 Author Share Posted September 23, 2006 it only runs one topic and fills in all the subcategories of that topic but never moves back into the first while loop to finish it out Quote Link to comment https://forums.phpfreaks.com/topic/21739-sql-loop-not-looping/#findComment-97068 Share on other sites More sharing options...
michaellunsford Posted September 23, 2006 Share Posted September 23, 2006 looks like you're resetting your $sql variable in the middle of the while($row = mysql_fetch_array($sql) loop. Try a different variable name for the second query. Quote Link to comment https://forums.phpfreaks.com/topic/21739-sql-loop-not-looping/#findComment-97071 Share on other sites More sharing options...
willfitch Posted September 23, 2006 Share Posted September 23, 2006 Good catch Michael. He's write, Mark. Quote Link to comment https://forums.phpfreaks.com/topic/21739-sql-loop-not-looping/#findComment-97074 Share on other sites More sharing options...
.josh Posted September 23, 2006 Share Posted September 23, 2006 you are overwriting the $row from the first while loop, with the 2nd while loopedit: ^^ the $sql overwrite is okay. the main one is only needed one time, in order to get the results. the result source is then used in the outer while loop. So it doesn't matter that it is over-written inside that loop. It's the $row he is over-writing. the outer loop is based on $row, but he starts his inner loop using the same variable name. the inner loop loops through the over-written $row and then breaks back to the outer loop, which then becomes false, because it checks $row, which the inner loop already incremented the pointer to the end of it. you need to use a different variable name for your inner loop, instead of $row. editedit: okay actually, yes, you need to use a diff var besides $sql, as well. i just noticed that you did not seperate the query string from the query function. so yes, you need to use some different var than $sql, but you also need to do the same for $row Quote Link to comment https://forums.phpfreaks.com/topic/21739-sql-loop-not-looping/#findComment-97075 Share on other sites More sharing options...
markbett Posted September 23, 2006 Author Share Posted September 23, 2006 yeah he is... i dont kn wo why but ii swear ive used the same code previously wihtout any issue grrr thank you so much it was annoying me i couldnt get it.... Quote Link to comment https://forums.phpfreaks.com/topic/21739-sql-loop-not-looping/#findComment-97388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.