I have mysql database with two tables.categories and products.

categories

  1. id
  2. parent_id
  3. category_name
  4. image
  5. status

products

  1. id

  2. category_id

  3. product_name

  4. product_description

  5. image

  6. status

Question
Using php code how to go to the products page after the n level categories?
Below is my php code.Thats going to the subcategories.but i am confused with the condition to go to the products page.

<?php
    $category_id = $_GET['id'];

# Get categories
$categories = array();
$qry        = mysql_query("SELECT * FROM categories
                            WHERE parent_id='".$category_id."' AND status = '1'")
              or die(mysql_error());
# Get totals
$qry_total = "SELECT COUNT(*) FROM categories
                            WHERE parent_id='".$category_id."' AND status = '1'";
$total_qry = mysql_query($qry_total) or die(mysql_error());
list($total) = mysql_fetch_array($total_qry);

?>
 <!--Content Side--> 
 <div class="col-lg-9 col-md-12 col-sm-12 col-xs-12">
                    <section class="blog-container">


                        <div class="row clearfix">
                            <!--Products Post-->
                              <?php 


                           while($row   = mysql_fetch_array($qry)) 
                            {     if($total==0){?>

                         <div class="col-lg-4 col-md-6 col-sm-6 col-xs-12 blog-post wow fadeInLeft" data-wow-delay="0ms" data-wow-duration="1500ms">
                          <a href="<?php echo $root_path.'products.php?id='.$row['parent_id']?>">

                                <article class="column-inner">
                                    <figure class="image-box">
                                <?php  if(!empty($row['image'])) {?>
                           <img src="<?php echo $root_path;?>timthumb.php?src=<?php echo $root_path.@$row['image']?>&q=400&w=250&h=200&zc=2" alt="">
                         <?php } else{ ?>

                      <img src="<?php echo $root_path;?>timthumb.php?src=<?php echo $root_path."images/resource/no-photo.gif"?>&q=400&w=250&h=200&zc=2" alt="">    
                         <?php }  ?>
                                    </figure>
                                    <div class="lower-part">
                                        <div class="post-title" ><h3><?php echo $row['category_name']; ?></h3></div>

                                    </div>
                                </article>
                                 </a>  
                            </div>
                           <?php }  else{  
                            ?>   
               <div class="col-lg-4 col-md-6 col-sm-6 col-xs-12 blog-post wow fadeInLeft" data-wow-delay="0ms" data-wow-duration="1500ms">

                          <a href="<?php echo $root_path.'product_categories.php?id='.$row['id']?>">

                                <article class="column-inner">
                                    <figure class="image-box">
                                <?php  if(!empty($row['image'])) {?>
                           <img src="<?php echo $root_path;?>timthumb.php?src=<?php echo $root_path.@$row['image']?>&q=400&w=250&h=200&zc=2" alt="">
                         <?php } else{ ?>

                      <img src="<?php echo $root_path;?>timthumb.php?src=<?php echo $root_path."images/resource/no-photo.gif"?>&q=400&w=250&h=200&zc=2" alt="">    
                         <?php }  ?>
                                    </figure>
                                    <div class="lower-part">
                                        <div class="post-title" ><h3><?php echo $row['category_name']; ?></h3></div>

                                    </div>
                                </article>
                                 </a>  
                            </div>
                             <?php } } ?> 
                        </div>


                    </section>

                </div>
                <!--Content Side-->
Member Avatar for diafol

Suggestions:
1) Use mysqli/PDO instead of deprecated mysql - see DW Tutorial: Common Issues with MySQL and PHP
2) Mixing HTML and PHP to this extent is horrible - both to read and to maintain - separate them out.
3) "Adjacency List" models (your self-referencing parent_id) have been covered many times on this site - there's a section on it in the tutorial I linked to above (specifically #10 How to Store Menus and Submenus - which has the same structure). Alternatively have a look at jkon's ILM model tutorial

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.