miracle_potential Posted July 1, 2008 Share Posted July 1, 2008 Having a little bit of trouble again *sigh* I have a loop inside a loop. Which works for the first part and only the first part. I guess my question is if a "while" statement is in another does that internal loop only run once? So really what I have is while($var = fetch_array){ some code while($second_var){ more code } } But the internal loop only runs once should I put a reset in? Thanks guys its had me stuck for days now.... Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 1, 2008 Share Posted July 1, 2008 The inside loop will run just as it would outside of a while loop. When it is inside, it will loop through the criteria multiple times depending on the outer loop. Quote Link to comment Share on other sites More sharing options...
ag3nt42 Posted July 1, 2008 Share Posted July 1, 2008 the first while will only run while its getting values from fetch. if the secound while is still going it will finish during the first while because the second while is only allowed to go while the first while is going ..phew. make sense? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 1, 2008 Share Posted July 1, 2008 We have to see you actual code to tell you what's wrong. Please post it between tags. Ken Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 1, 2008 Share Posted July 1, 2008 I believe his question is related to his code at this topic: http://www.phpfreaks.com/forums/index.php/topic,203386.msg921127.html#msg921127 Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 2, 2008 Author Share Posted July 2, 2008 Lemmin is right still stuck with it ]: might just do it with XML transformations although this would be easier to update and keep on track. Although there are some slight differances but it makes no differance as to the code itself Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 2, 2008 Author Share Posted July 2, 2008 function catagories_fetch(){ //Fetch Catagories $cfmain = mysql_query( "SELECT * FROM catagories ORDER BY ID DESC") or die(mysql_error()); $cfsub = mysql_query( "SELECT * FROM subcatagories ORDER BY ID DESC") or die(mysql_error()); echo '<div id="prd">'; while($cff = mysql_fetch_array($cfmain)){ echo '<table width="680" border="0" cellspacing="0" cellpadding="0">'; echo '<tr><td class="browse">'; echo '<a href="javascript:collapse' . $cff['ID'] . '.slideit()" class="productheaders">' . str_replace("_", " ", $cff["catagory"]) . '</a>'; echo '</td></tr>'; //End Row 1 echo "<tr><td><div class='catagorydivs' id='catagorydivs" . $cff['ID'] ."' onmouseover=style.backgroundColor='#ECE0ED' onmouseout=style.backgroundColor='#FFFFFF'>"; echo "<table width='100%'>"; echo '<tr><td class="maintxt"><strong>' . $cff['dblurb'] . '</strong></td></tr><tr><td></td> </tr>'; while ($cfsubf = mysql_fetch_array($cfsub)){ if($cfsubf['CID'] == $cff['CID'] && $cfsubf['active'] = '1'){ echo '<tr><td> <a class="maintxt" href="http://new.noirorchidemporium.co.uk/Iframes/products/catagories/?paction=prod_list_pc&catagory=' . $cfsubf['catagory'] . '">' . $cfsubf['catagory'] . ' --></a></td></tr>'; }//End IF issub check }//End contained WHILE echo '<tr><td> </td></tr>'; echo '</table>'; echo '</div></td></tr>'; echo '<tr><td> </td></tr>'; //End Row 2 echo '</table>'; echo '<script type="text/javascript"> //Syntax: var uniquevar=new animatedcollapse("DIV_id", animatetime_milisec, enablepersist(true/fase), [initialstate] ) var collapse' . $cff['ID'] . ' = new animatedcollapse("catagorydivs' . $cff['ID'] .'", 400, false, [close])</script>'; }//End while echo "</div>"; }//End catagories_fetch() Theres the code really sorry about how long it is. I did try a foreach on the main catagories but it does the same thing. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 2, 2008 Share Posted July 2, 2008 First the code in not long, only 33 lines when you clean up the formatting. I did see one error that might be causing your problem. In this "if" statement <?php if($cfsubf['CID'] == $cff['CID'] && $cfsubf['active'] = '1'){ ?> you are using a single "=" when you should be using a double "==". Single "=" are used for assignment, double "==" are used for comparison. Corrected: <?php if($cfsubf['CID'] == $cff['CID'] && $cfsubf['active'] == '1'){ ?> If that's not the problem, I suggest you do some debugging. Strip out the HTML output from the loops leaving just the PHP, put some echo statements in the loops to show what you're getting and run the script. Also I suggest using the mysql_fetch_assoc() function instead of the mysql_fetch_array() function, since you're only using the associative parts of the returned array. If I were debugging this, here's the code I would use: <?php function catagories_fetch(){ //Fetch Catagories $cfmain = mysql_query("SELECT * FROM catagories ORDER BY ID DESC") or die(mysql_error()); $cfsub = mysql_query("SELECT * FROM subcatagories ORDER BY ID DESC") or die(mysql_error()); while($cff = mysql_fetch_array($cfmain)){ echo 'In outer loop<br>'; while ($cfsubf = mysql_fetch_array($cfsub)){ echo ' ... In inner loop ... '; if($cfsubf['CID'] == $cff['CID'] && $cfsubf['active'] == '1'){ echo ' IF succeeded'; }//End IF issub check echo '<br>'; }//End contained WHILE }//End while }//End catagories_fetch() ?> Ken Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 2, 2008 Share Posted July 2, 2008 I've already been over those if statements with him a couple times, he doesn't believe me! Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 2, 2008 Author Share Posted July 2, 2008 Lol I'll give it a go when I get home if your both adament its the if statement haha. They worked before. And also the first catagory works its only ever one main catagory that works which is why i believe that its not the if statement Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 3, 2008 Author Share Posted July 3, 2008 Ok lol I changed the if statement to what you guys suggested and (as I thought) made no differance. I tried debugging it and it just seems that the internal loop is escaping after the first time round on the main loop. I really dont have a clue whats going on with it if you guys want to work this out be my guest it would make things a little easier in the long run but I think I'm going to have to do it with XML transformations. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 3, 2008 Share Posted July 3, 2008 For us to test your code, we need your data. (database table definitions & data) Ken Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 3, 2008 Author Share Posted July 3, 2008 I'll get it to you this evening after work Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 3, 2008 Share Posted July 3, 2008 Ok lol I changed the if statement to what you guys suggested and (as I thought) made no differance. I explained to you earlier why there would be no difference in that particular case, but the fact is, there is a huge difference between "=" and "==." In any case where the if statement was actually necessary, using the wrong one would cause very unwanted results. @kenrbnsn, he posted the table definitions and a lot more stuff about his problem in the topic that I referenced earlier. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 3, 2008 Share Posted July 3, 2008 @kenrbnsn, he posted the table definitions and a lot more stuff about his problem in the topic that I referenced earlier. But, I'm not looking at that problem, I'm looking at this problem. If the OP had continued to ask this in the original post then everything would have been clearer. This is why double posting is strongly discouraged. Ken Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 3, 2008 Share Posted July 3, 2008 Well, I took the table definitions from the other post and the OP's code (after fixing the "==") and ran it. It seemed to run fine. Twice though the main loop and 4 times in the second loop. Ken Quote Link to comment Share on other sites More sharing options...
miracle_potential Posted July 4, 2008 Author Share Posted July 4, 2008 http://new.noirorchidemporium.co.uk/Iframes/products/catagories Thats what I'm getting though ??? Sorry I didnt post code and sorry about the double post but this is really bugging me. I will post code tonight. Quote Link to comment 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.