Jump to content

loops


Recommended Posts

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....

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.