Jump to content

[SOLVED] Nested while loop


acroninj

Recommended Posts

I am trying to pull data from a CMS to display on a page. I need to show each category, then have each of the respective subcategories under it.

 

The way it is set up is in 2 tables.

Table 1: category name

Table 2: subcategory name, category name, description, etc

 

I have tried setting up a while loop for the category names, then nesting while loop in that to test if the category names match, then display the subcategory, but it does not work.

 

mysql_select_db($database_adminDB, $adminDB);
$query_groups = "SELECT groups.groupName FROM groups";
$groups = mysql_query($query_groups, $adminDB) or die(mysql_error());
$row_groups = mysql_fetch_assoc($groups);
$totalRows_groups = mysql_num_rows($groups);

mysql_select_db($database_adminDB, $adminDB);
$query_categories = "SELECT categories.categoryName, categories.groupName FROM categories";
$categories = mysql_query($query_categories, $adminDB) or die(mysql_error());
$row_categories = mysql_fetch_assoc($categories);
$totalRows_categories = mysql_num_rows($categories);


-------

do {
		echo "<h1>" . $row_groups['groupName'] . "</h1>"; 
		echo "<ul>";
		while ($row_categories = mysql_fetch_assoc($categories)){
			if($row_categories['groupName'] = $row_groups['groupName']){
				echo "<li>" . $row_categories['categoryName'] . "</li>";
			}
		}
		echo "</ul>";
	} while ($row_groups = mysql_fetch_assoc($groups));

 

Is there a way to do this?

Link to comment
https://forums.phpfreaks.com/topic/161873-solved-nested-while-loop/
Share on other sites

I see your using Dreamweaver

 

I use dreamweaver alot and have simlar problems even more so when using there site template systems.

 

Here i would do one of two things.

 

Replace the while loop with a function call and write your categories query into a function to return the catergory name or just scrap the categories query and use an MySQL group in the groups query if possible

Still no luck.

 

mysql_select_db($database_adminDB, $adminDB);
$query_groups = "SELECT groups.groupName FROM groups";
$groups = mysql_query($query_groups, $adminDB) or die(mysql_error());
$row_groups = mysql_fetch_assoc($groups);
$totalRows_groups = mysql_num_rows($groups);

function getCats($groupName){
mysql_select_db($database_adminDB, $adminDB);
$query_categories = "SELECT categories.categoryName, categories.groupName FROM categories WHERE categories.groupName = $groupName";
$categories = mysql_query($query_categories, $adminDB) or die(mysql_error());
$row_categories = mysql_fetch_assoc($categories);
$totalRows_categories = mysql_num_rows($categories);

do {
	$cats .= "<li>" . $row_categories['categoryName'] . "</li>"; 
} while ($row_categories = mysql_fetch_assoc($categories));

return $cats;
}


--------------

	do {
		echo "<h1>" . $row_groups['groupName'] . "</h1>"; 
		echo "<ul>";
		echo getCats($row_groups['groupName']);
		echo "</ul>";
	} while ($row_groups = mysql_fetch_assoc($groups)); ?>

I figured it out:

do {
		echo "<h1>" . $row_groups['groupName'] . "</h1>"; 
		echo "<ul>";
		$groupName = $row_groups['groupName'];
		$results = array();
		$searchSQL = sprintf("SELECT categories.categoryName, categories.groupName FROM categories WHERE categories.groupName = %s",GetSQLValueString($groupName, "text"));
		$searchResult = mysql_query($searchSQL) or die("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
		 $i = 1;
		 while ($row = mysql_fetch_assoc($searchResult)) {
			echo "<li>" . $row['categoryName'] . "</li>";
			$i++;
		 }
		echo "</ul>";
	} while ($row_groups = mysql_fetch_assoc($groups)); ?>

 

Thanks for your help.

Archived

This topic is now archived and is closed to further replies.

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