Jump to content

Help with while loop


herghost

Recommended Posts

Evening everyone.

 

1st of all here is my code

 

<?php 
include('connect.php');
$query = mysql_query("SELECT * FROM cats");
while($row = mysql_fetch_assoc($query)){

$catid = $row['catid'];
$catname = $row['cname'];

$query1 = mysql_query("SELECT * FROM subc WHERE catid = $catid");
while($row = mysql_fetch_assoc($query1)){

$subid = $row['sid'];
$sname = $row['sname'];


?>

<li class="top"><a href="javascript:ajaxpage('view.php?catid=<?php echo $catid;?>', 'content');" class="top_link" title="<?php echo $catname;?>"><span><?php echo catname;?></span></a></li>
<ul class="sub">
<li><a href="view.php?catid=<?php echo $catid;?>&subid=<?php echo $subid;?>"><?php echo $sname;?></a></li>	
</ul>
</li>

<?php 
}}
?>


</ul>
</div>

 

I have two tables, cats and subc, cats contain fields catid and catname, subc contains catid, sid, and sname

 

What I am trying to do is echo the results to build my menu.

 

However, for instance catid 1 is electronics and contains 5 sub cats, but the script is echoing 5x electronic top menus and no sub menus (the 5 sid's that match the catid)

 

I hope someone understands what I am trying to say!

 

Can any point out what I am doing wrong?

 

Cheers

Link to comment
Share on other sites

I think this would work better if you first gathered the data organized it and then generated the HTML.

The problem is in your while looping for sure but doing this the way I have suggested will be much cleaner and easier to follow.

 

Off the top of my head you could organize it into arrays..

 

<?php 
include('connect.php');

$query = mysql_query("SELECT * FROM cats");

$counter = 0;//Use this as a way to ID data as it comes in.
while($row = mysql_fetch_assoc($query))
{
$catid[$counter] = $row['catid'];	
$catname[$counter] = $row['cname'];
}

$NumTimes = count($catid); //Establishing a number of times to run the second loop
for($i=0; $i<=$NumTimes; $i++)
{
$query1 = mysql_query("SELECT * FROM subc WHERE catid = $catid[$i]");

$c=0;//Another Counter for SubID's
while($row = mysql_fetch_assoc($query1))
{		
	$subid[$i][$c] = $row['sid'];
	$sname[$i][$c] = $row['sname'];
}
}
//Now all of the Cats are stored in 2 Arrays(1 For name and 1 for ID) and SubCats are in 2 Multi Arrays(1 For name and 1 for ID)
?>

 

The first array "$catid" holds all the ID's for each Category in your database..

So in order to get the value of that first ID you use this:

$catid[0]

The index value can be 0-? how ever many categories you have.. say its 5 Categories then you could enter 0-4.

 

The Second Array "$catname" follows the same format:

$catname[0]

The Index value can be 0-? how ever many categories you have.. say its 5 Categories then you could enter 0-4.

(This will be the exact same Index values corresponding with the first array of ID's)

 

 

For the next two arrays .. they work the same but remember there could be multiple sub categories for each category.. In order to hold all of those we format the arrays multi demensionally..

 

The first multi array "$subid" holds all of the ID's for each Sub Id per Category.. so.. In order to retrieve the first sub cat of the first Cat the variable would look like so:

 

$subid[0][0];

 

$subid[0][1]; //< Equals the 2nd Sub cat of the First Cat

 

And Finally the last multi Array "$sname" is in the same format as the 1st multi array and it holds all of the names

 

Now all of the data is there and ready to be worked with however you see fit 8)

Link to comment
Share on other sites

It's because within your first loop, you are looping again but using the original $row which will overwrite that variable with the new query.

 

Instead, define a new $row, like $row2, or $subcats.

 

..and, instead of using while, you might consider foreach, which I find far more fitting for this situation.

 

..and I'd echo what efficacious has said above - split up your logic and output. Do the queries first to obtain the data, THEN insert that data into your html.

Link to comment
Share on other sites

oh yea you might want to add "increments" to the two while loop counters I forgot to add them.. 80

 

 

Updated with increments:

<?php 
include('connect.php');

$query = mysql_query("SELECT * FROM cats");

$counter = 0;//Use this as a way to ID data as it comes in.
while($row = mysql_fetch_assoc($query))
{
$catid[$counter] = $row['catid'];	
$catname[$counter] = $row['cname'];
                $Counter++;
}

$NumTimes = count($catid); //Establishing a number of times to run the second loop
for($i=0; $i<=$NumTimes; $i++)
{
$query1 = mysql_query("SELECT * FROM subc WHERE catid = $catid[$i]");

$c=0;//Another Counter for SubID's
while($row = mysql_fetch_assoc($query1))
{		
	$subid[$i][$c] = $row['sid'];
	$sname[$i][$c] = $row['sname'];
                                $c++;
}
}
//Now all of the Cats are stored in 2 Arrays(1 For name and 1 for ID) and SubCats are in 2 Multi Arrays(1 For name and 1 for ID)
?>

Link to comment
Share on other sites

Thankyou, however I am still at a loss on how I actually echo out my data into the html

 

<?php

for($i=0; $i<$NumTimes; $i++)
{
echo("
	<li class="top">
		<a href=\"javascript:ajaxpage('view.php?catid=".$catid[$i]."', 'content');\" class=\"top_link\" title=\"".$catname[$i]."\">
			<span>".$catname[$i]."</span>
		</a>
	</li>
");

//Establish Count for  sub cats
$NumCats = count($subid[$i]);//This counts the second dimension of the array (which is the num of sub categorys for that particular category)

//Loop through each SubCat
for($j=0; $j<$NumCats; $j++)
{
	echo("
		<ul class=\"sub\">
			<li>
				<a href=\"view.php?catid=".$catid[$i]."&subid=".$subid[$i][$j]."\">".$sname[$i][$j]."</a>
			</li>
		</ul>
	");
}
}
?>

 

I think that should get it done.. you may have to tweak it.

 

 

And to be xtra nice.. here is how it would all go together..:

<?php 

include('connect.php');

$query = mysql_query("SELECT * FROM cats");

$counter = 0;//Use this as a way to ID data as it comes in.

while($row = mysql_fetch_assoc($query))
{	
$catid[$counter] = $row['catid'];		
$catname[$counter] = $row['cname'];                
$Counter++;
}

$NumTimes = count($catid); //Establishing a number of times to run the second loop
for($i=0; $i<=$NumTimes; $i++)
{	
$query1 = mysql_query("SELECT * FROM subc WHERE catid = $catid[$i]");

$c=0;//Another Counter for SubID's	
                while($row = mysql_fetch_assoc($query1))	
{				
	$subid[$i][$c] = $row['sid'];		
	$sname[$i][$c] = $row['sname'];                                
	$c++;	
}
}
//Now all of the Cats are stored in 2 Arrays(1 For name and 1 for ID) and SubCats are in 2 Multi Arrays(1 For name and 1 for ID)

///Generate Display
for($j=0; $j<$NumTimes; $j++)
{
echo("
	<li class="top">
		<a href=\"javascript:ajaxpage('view.php?catid=".$catid[$j]."', 'content');\" class=\"top_link\" title=\"".$catname[$j]."\">
			<span>".$catname[$j]."</span>
		</a>
	</li>
");

//Establish Count for  sub cats
$NumCats = count($subid[$j]);//This counts the second dimension of the array (which is the num of sub categorys for that particular category)

//Loop through each SubCat
for($k=0; $k<$NumCats; $k++)
{
	echo("
		<ul class=\"sub\">
			<li>
				<a href=\"view.php?catid=".$catid[$j]."&subid=".$subid[$j][$k]."\">".$sname[$j][$k]."</a>
			</li>
		</ul>
	");
}
}
?>

 

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.