Jump to content

Archived

This topic is now archived and is closed to further replies.

markbett

sql loop not looping

Recommended Posts

my code (see below) should work as follows
1) 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 out

3) 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 topics

the problem is that this code is only working for the very first topic.... WHY!?!?!

[code]<?php
include $_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]

Share this post


Link to post
Share on other sites
Can you tell me what it is doing?  Perhaps viewing the source and telling me what is says?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
you are overwriting the $row from the first while loop, with the 2nd while loop

edit: ^^ 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

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.