12:16:54 AM
Thursday
Sep 09
Su M Tu W Th F Sa
567891011
12131415161718
19202122232425
262728293012
3456789

Play My Typing Game

View Some of My Artwork

Recursion Example Involving MySQL
PROBLEM: You want infinitely nested categories. Start by building a table for your categories (id, name) have field for parent_id, which would default to 0. I choose to also add the feature of priority ordering capabilities. I recommend ignoring it exists, you could ditch it completely and change the MySQL query to SORT BY name ASC instead.

CREATE TABLE `categories` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `parent_id` int(10) unsigned NOT NULL default '0',
  `name` varchar(100) NOT NULL default '',
  `priority` int(11) NOT NULL default '100',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

INSERT INTO `categories` VALUES (1, 0, 'Example 1', 1);
INSERT INTO `categories` VALUES (2, 0, 'Example 2', 2);
INSERT INTO `categories` VALUES (3, 1, 'Child 1', 1);
INSERT INTO `categories` VALUES (4, 1, 'Child 2', 2);
INSERT INTO `categories` VALUES (5, 2, 'Another child', 1);


the backticks probably aren't necessary, it is what PHPMyAdmin defaulted to.

Ok, so now we have data. Lets create a dropdown thats recursive.

function recur_option($cat=0,$sp=0)
 {
  global $pg_content;
  $q = "SELECT id,name FROM prod_cat WHERE parent_id=".$cat." ORDER BY priority ASC";
  $r = mysql_query($q) or exit(mysql_error().$q);

  if(mysql_num_rows($r)>0)
  {
   while($a = mysql_fetch_assoc($r))
   {
     $pg_content .= "<option value=\"".$a['id']."\">";
     for($j=0;$j<$sp;$j++)
     {
       $pg_content .= "  ";
     }
     $pg_content .= $a['name'];
     $pg_content .= "</option>\n";

     // do i have children?
     $q2 = "SELECT id FROM prod_cat WHERE parent_id=".$a['id'];
     $r2 = mysql_query($q2) or exit(mysql_error().$q2);
     if(mysql_num_rows($r2)>0)
     {
       recur_option($a['id'],$sp+1);
     }
   }
 }
}


We would then now use this function on a form like this:


$pg_content = "<select name=\"parent\">";
$pg_content .= "<option value=\"0\"> </option>";
recur_option();
$pg_content .= "</select>";
echo $pg_content;


Read Previous:
Convert a Hex Color Code to RGB
Read Next:
Detecting HTTP Headers
Table of Contents


Give a listen to my music...
My Best Recordings
How my services and skills can help you:
  • Global (via Email/Skype/AIM(ichat)/Yahoo)
    • Web Development Consulting
    • Custom Web App Development (PHP/MySQL/jQuery/CSS) Cross-Browser & OS
    • Cellphone App Development (Android/MobileWeb)
    • Music Lessons & Education for Beginner Guitar, Piano, Bass, which transfers over to many other instruments I am comfortable playing but not yet at teaching.
    • Music - Soundtrack Composition & Voice Work
  • Local
    • Most all the above, but also:
      • Music Performance (Harp, Voice, Guitar, Piano, Bass, Latin Percussion)
      • Recording Live Music Audio
      • Video Streaming live music and other live events anywhere that can provide wifi
      • Training Video Streaming to your venue's trusted soundperson
Skills:
  • Global (via Email/Skype/AIM(ichat)/Yahoo)
    • Web Development Consulting
    • Custom Web App Development (PHP/MySQL/jQuery/CSS) Cross-Browser & OS
    • Cellphone App Development (Android/MobileWeb)
    • Music Lessons & Education for Beginner Guitar, Piano, Bass, which transfers over to many other instruments I am comfortable playing but not yet at teaching.
    • Music - Soundtrack Composition & Voice Work
  • Local, Most all the above, but also:
    • Music Performance (Harp, Voice, Guitar, Piano, Bass, Latin Percussion)
    • Being your venue's soundman
    • Video Streaming live music and other live events anywhere that can provide wifi
    • Training Video Streaming to your venue's trusted soundperson