acroninj Posted June 11, 2009 Share Posted June 11, 2009 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 More sharing options...
laPistola Posted June 11, 2009 Share Posted June 11, 2009 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 Link to comment https://forums.phpfreaks.com/topic/161873-solved-nested-while-loop/#findComment-854155 Share on other sites More sharing options...
acroninj Posted June 16, 2009 Author Share Posted June 16, 2009 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)); ?> Link to comment https://forums.phpfreaks.com/topic/161873-solved-nested-while-loop/#findComment-857393 Share on other sites More sharing options...
laPistola Posted June 16, 2009 Share Posted June 16, 2009 What is this doing?? is there any errors?? Link to comment https://forums.phpfreaks.com/topic/161873-solved-nested-while-loop/#findComment-857526 Share on other sites More sharing options...
acroninj Posted June 17, 2009 Author Share Posted June 17, 2009 It stops rendering the page after the first iteration of $row_groups['groupName'] Link to comment https://forums.phpfreaks.com/topic/161873-solved-nested-while-loop/#findComment-858131 Share on other sites More sharing options...
acroninj Posted June 17, 2009 Author Share Posted June 17, 2009 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. Link to comment https://forums.phpfreaks.com/topic/161873-solved-nested-while-loop/#findComment-858168 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.