tsm1248 Posted November 24, 2010 Share Posted November 24, 2010 Is it possible to have a loop run in a loop ? Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/ Share on other sites More sharing options...
kenrbnsn Posted November 24, 2010 Share Posted November 24, 2010 Yes. Are you having a problem? Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139015 Share on other sites More sharing options...
tsm1248 Posted November 24, 2010 Author Share Posted November 24, 2010 Yes i connect and do the normal routine and i echo out all the rows that have a $row->type==tickets the issue is can i now call from a different table a loop os users into that table? So another question arrises can i loop from another table within a loop Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139016 Share on other sites More sharing options...
kenrbnsn Posted November 24, 2010 Share Posted November 24, 2010 If you're having problems with your code, please post the relevant pieces between tags. Ken Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139020 Share on other sites More sharing options...
tsm1248 Posted November 24, 2010 Author Share Posted November 24, 2010 Scroll all the way to the bottom! <?php mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("db1") or die(mysql_error()); $query = "SELECT * FROM tickets"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { can i have another loop here from a different table } ?> Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139023 Share on other sites More sharing options...
tsm1248 Posted November 24, 2010 Author Share Posted November 24, 2010 Sry had to shorten the code..sry it took a little long Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139027 Share on other sites More sharing options...
johnsmith153 Posted November 24, 2010 Share Posted November 24, 2010 You can, but ensure you don't use $result as the variable. This should be ok: <?php mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("db1") or die(mysql_error()); $query = "SELECT * FROM tickets"; $result = mysql_query($query) or die(mysql_error()); $query = "SELECT * FROM more_tickets"; $result2 = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { while($row = mysql_fetch_array($result2)) { } } ?> Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139035 Share on other sites More sharing options...
johnsmith153 Posted November 24, 2010 Share Posted November 24, 2010 Actually, do this also: while($row = mysql_fetch_array($result)) { while($row2 = mysql_fetch_array($result2)) { Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139040 Share on other sites More sharing options...
Jerred121 Posted November 24, 2010 Share Posted November 24, 2010 You can, but ensure you don't use $result as the variable. This should be ok: <?php mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("db1") or die(mysql_error()); $query = "SELECT * FROM tickets"; $result = mysql_query($query) or die(mysql_error()); $query = "SELECT * FROM more_tickets"; $result2 = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { while($row = mysql_fetch_array($result2)) { } } ?> Unless you want the inner loop to increment the same table as the first loop - i've hade to need to do this a couple of times - it starts getting really confusing when there are about 5-6 embedded loops all incrementing the same thing... Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139044 Share on other sites More sharing options...
tsm1248 Posted November 24, 2010 Author Share Posted November 24, 2010 Thanks guys that worked fantastically can i have a forum within a forum and have the button within the html forum tag send to a different php page <forum> <forum> both forums go to dif pages </forum> </forum> Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139084 Share on other sites More sharing options...
kenrbnsn Posted November 24, 2010 Share Posted November 24, 2010 There is no such HTML tag "<forum>". Do mean "<form>"? No, you can not have nested <form> tags. Ken Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139086 Share on other sites More sharing options...
tsm1248 Posted November 24, 2010 Author Share Posted November 24, 2010 Wow just figured it out just placed an if statement on one page for both of them..thanks for all your help guys great community and great people Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139087 Share on other sites More sharing options...
tsm1248 Posted November 24, 2010 Author Share Posted November 24, 2010 OK so the first time loop outputs the first line both loops work then after that only the first loop works? Link to comment https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139185 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.