Table: category

table structure

id
label
parent

example data

id - label - parent

1 - Apparel - 0
2 - Technology - 0
3 - Travel - 0
4 - shirts - 1

For above table I have two links

  1. First link display category with no parent (e.g. Apparel, Technology, Travel )
  2. Second link display subcategory with parent (e.g. shirts (parent - Apparel) )

How to display category and its subcategory if any link is clicked.
e.g.
if first link is clicked i.e. Apparel => display - Apparel - shirts
if second link, i.e. subcategory, is clicked i.e. shirts => display - Apparel - shirts

Recommended Answers

All 2 Replies

Explaining code and database
Database

INSERT INTO `category` (`id`, `label`, `parent`) VALUES
(1, 'Apparel', 0),
(2, 'Technology', 0),
(3, 'Travel & Outdoors', 0),
(4, 'Personal Accessories', 0),
(5, 'Trophies & Plaques', 0),
(6, 'Writing Instruments & Paper', 0),
(7, 'Echo Friendly', 0),
(8, 'Drinkware & Lunchware', 0),
(9, 'Desktop', 0),
(10, 'Ethnic', 0),
(11, 'shirts', 1);

index.php

1.  $result = mysql_query("SELECT * FROM category WHERE parent = 0");
       <a href='product.php?id=" . $row['id'] . "'>" . $row['label'] . "</a>

2.  $result = mysql_query("SELECT * FROM category WHERE parent != 0");
        <a href='product.php?id=" . $row['id'] . "'>" . $row['label'] . "</a>

product.php

$id = $_REQUEST['id'];
$result = mysql_query("SELECT * FROM category WHERE id = $id"); // need help here to display following result

OUTPUT on index.php

e.g. link for category Apparel

  1. <a href='product.php?id=1'>Apparel</a>
  2. <a href='product.php?id=11'>shirts</a>

DESIRED OUTPUT

If any link from above is clicked, display following output on product.php,-

Apparel

  • shirts

(subcategory is added for only Apparel category. category id 2-Technology to category id 10-Ethnic have subcategories but not added. Apparel to Ethnic are main categories )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.