0

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

2
Contributors
2
Replies
18
Views
6 Months
Discussion Span
Last Post by hrushi9
0

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 )

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.