Page 1 of 1

MSQL Dynamic Menu/Best Way

Posted: Tue Dec 02, 2003 9:27 pm
by ptysell
This is what i have. It works. I was just wondering if there is a better way to do this.

Code: Select all

function genSection()
		{
		
		$query = "SELECT * FROM section";
		$result = dbquery($query);
		$num_sections = mysql_num_rows($result);

		for($i = 0; $i < $num_sections; $i++)
			&#123;
			$menu_data = mysql_fetch_array($result);
			echo "<p><b>$menu_data&#1111;section]</b></p>";
			genSubSection($menu_data&#1111;id]);
			&#125;
		
		
		&#125;
		
	function genSubSection($id)
		&#123;
		$query = "SELECT * FROM subsection WHERE id = '$id'";
		$result = dbquery($query);
		$num_subsections = mysql_num_rows($result);
		
		for($i = 0; $i < $num_subsections; $i++)
			&#123;
			$subsection_data = mysql_fetch_array($result);
			echo "<p>$subsection_data&#1111;subsection]</p>";
			&#125;
		&#125;
Thanks for the comments and sugestions.

Posted: Tue Dec 02, 2003 9:37 pm
by microthick
I usually use a simple join to create a query like:

$sql = "select section.*, subsection.* from section, subsection where section.id = subsection.id order by section.name, subsection.name";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

pseudo code:

Code: Select all

set curr_section = "";
loop through $row &#123;
   if curr_section != $row&#1111;section_name] &#123;
      output name of section
   &#125;
   
   output name of subsection
 
   set curr_section = $row&#1111;section_name];
&#125;
maybe sorta sloppy, but it getss the job done.

Posted: Tue Dec 02, 2003 9:41 pm
by DuFF
I think you can make it just a little bit simpler. Otherwise its perfectly good code.

Heres my take:

Code: Select all

<?php
function genSection()
      {
      
      $query = "SELECT * FROM section";
      $result = dbquery($query);

      while($menu_data = mysql_fetch_array($result))
         {
         echo "<p><b>$menu_data['section']</b></p>";
         genSubSection($menu_data['id']);
         }      
      } 

function genSubSection($id)
      {
      $query = "SELECT * FROM subsection WHERE id = '$id'";
      $result = dbquery($query);
      
      while($subsection_data = mysql_fetch_array($result))
         {
         echo "<p>$subsection_data['subsection']</p>";
         }
      } 
?>
Also, scroll down on this link and read the part titled "Array do's and don'ts - Why is $foo[bar] wrong?" Notice I added quotes into the arrays. This isn't a big error, but its worth noting for future reference.

Posted: Wed Dec 03, 2003 4:20 am
by ptysell
This is what i came up with as my final product. Please comment on it. Thi is only the backbone and does not include any of my HTML if you are wondering.

Code: Select all

<?php
echo "<html><body>";
include("../database/db_user.php");

genSection();
genSubSection();

function genSection()
	{
	$query = "SELECT * FROM section";
	$results = dbquery($query);
	
	while($section_data = mysql_fetch_array($results))
		{
		echo "<p><b>$section_data[section]</b></p>\n"; 
		} 
	} 
	
function genSubSection()
	{
	$query = "SELECT DISTINCT id FROM subsection";
	$results = dbquery($query);
	
	while($section_id = mysql_fetch_array($results))
		{
		echo "<p>$section_id[id]</p>\n";
		$query2 = "SELECT * FROM subsection WHERE id = '$section_id[id]'";
		$results2 = dbquery($query2);
		
		while($subsection_data = mysql_fetch_array($results2))
			{
			echo "<p>$subsection_data[subsection]</p>\n";
			}	
		}
	}
echo "</body></html>";
?>