Jump to content

Categorizing Tables


eugene2009

Recommended Posts

So i got this part figured out..

 

CREATE TABLE category(
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
parent INT DEFAULT NULL);


INSERT INTO category
VALUES(1,'ELECTRONICS',NULL),(2,'TELEVISIONS',1),(3,'TUBE',2),
(4,'LCD',2),(5,'PLASMA',2),(6,'PORTABLE ELECTRONICS',1),
(7,'MP3 PLAYERS',6),(8,'FLASH',7),
(9,'CD PLAYERS',6),(10,'2 WAY RADIOS',6);

SELECT * FROM category ORDER BY category_id;

 

My question is now.. how do I generate html pages where

 

PAGE 1 (ELECTRONICS)
   Televisions
   Portable Electronics


PAGE 2 (DEPENDING ON PAGE 1 SELECTION)

.. Lists here all the childs of electronics..

PAGE 3 

..Lists the subcategory of previous page. 

 

Does anyone know what im talking about?

Link to comment
Share on other sites

Personally I would change the root category parent ids to 0 then generate the entire tree at once with something like.  Or if you seperate by page you would just need to send the "parent" id to this same type of function via a get variable.

 

Do a search on Adjacency List Model their are tons of example out there.

 

function display($parent) {

   $result = mysql_query('SELECT category_id, name FROM categories WHERE parent ='. $parent);
   $html = '<ul>';
   while ($row = mysql_fetch_array($result)) {
       $html .= '<li><a href="whatever.php?cat='.$row['category_id'].'" >'.$row['name'].'</a></li>';
       display($row['id']);
   }
   $html .= '</ul>';
   return $html;
}

$tree = display(0); 

echo $tree;

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.