Aureole Posted September 11, 2007 Share Posted September 11, 2007 Ok well basically I want to have Categories...then Forums I'm wondering if it's possible/a good idea to query and have a while() when you're already inside a while() statement...e.g. <?php $query = "SELECT * FROM `categories`'"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $catname = $row['cat_name']; echo('<div class="cattitle">'.$catname.'</div>'); echo('div class="forums">'); $query = "SELECT * FROM `forums` WHERE category='{$catname}''"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $forumname = $row['forum_name']; $forumid = $row['forum_id']; echo('<a href="viewforum.php?id='.$forumid.'">'.$forumname.'</a>'); // etc...... } } ?> That should give you a rough idea of what I'm trying to do... Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/ Share on other sites More sharing options...
liebs19 Posted September 11, 2007 Share Posted September 11, 2007 There is nothing wrong with using while loops inside of while loops. Are your loops not working correctly? Are you getting any error messages? Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/#findComment-346422 Share on other sites More sharing options...
Aureole Posted September 11, 2007 Author Share Posted September 11, 2007 No I'm planning on making something I find making things is the best way to learn rather than reading useless tutorials that have no usage in the real World, I just wanted to know if it was possible before I began. Thanks a lot, I'm going to leave this unsolved for now just in case I bump into any errors as last time I tried it - it didn't work as intended. Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/#findComment-346424 Share on other sites More sharing options...
liebs19 Posted September 11, 2007 Share Posted September 11, 2007 I couldn't agree more. Until I actually sit down and start making errors in my coding I simply will never learn anything. Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/#findComment-346426 Share on other sites More sharing options...
Barand Posted September 11, 2007 Share Posted September 11, 2007 The problem with that nested while() method is you end up sending many queries to your server. Instead you can accomplish the same thing with a single query using a join between the 2 tables. Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/#findComment-346443 Share on other sites More sharing options...
liebs19 Posted September 11, 2007 Share Posted September 11, 2007 Yeah, I guess I didn't really look at the full code of what those loops were doing. Nested while loops are good for outputting the results but you want to do the query first outside of it. Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/#findComment-346449 Share on other sites More sharing options...
Aureole Posted September 11, 2007 Author Share Posted September 11, 2007 I've never used join before how does it work? Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/#findComment-346464 Share on other sites More sharing options...
Barand Posted September 11, 2007 Share Posted September 11, 2007 This will explain better than I can http://www.w3schools.com/sql/sql_join.asp Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/#findComment-346468 Share on other sites More sharing options...
Aureole Posted September 11, 2007 Author Share Posted September 11, 2007 Aaah...ok this should give me something to do for the next 5 Hours... Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/#findComment-346471 Share on other sites More sharing options...
Barand Posted September 11, 2007 Share Posted September 11, 2007 Worth mastering. It's JOINS that give relational databases their power. Have fun. Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/#findComment-346476 Share on other sites More sharing options...
Aureole Posted September 12, 2007 Author Share Posted September 12, 2007 Well I will definitely have to learn joins then, thanks for your help anyway. Quote Link to comment https://forums.phpfreaks.com/topic/68920-is-it-possiblea-good-idea-to/#findComment-346480 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.