Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6596621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:05:59+00:00 2026-05-25T18:05:59+00:00

Below is my category.php that uses a switch statement to either show subcategories or

  • 0

Below is my category.php that uses a switch statement to either show subcategories or products. When i click on a subcategory, it displays the products like it’s supposed to. However, if the category has NO SUBCATEGORIES, no products will show at all.

Is this a case where the switch statement needs to be altered, the entire foreach loop needs to be in an if statement or is there something wrong with the db and the ids… all of my cats in the menu have a parent_id = 0, subcats correspond to the id for the category table, the products are assigned a category_id. Is this a JOIN thing?

<?php
foreach ($listing as $key => $list){
echo "<img src='".$list['thumbnail']."' border='0' align='left' />";
echo "<h4>";

switch($level){
    case "1":
    echo anchor('welcome/cat/'.$list['id'],$list['name']);
    break;
    case "2":
    echo anchor('welcome/product/'.$list['id'],$list['name']);
    break;

}
echo "</h4>";
echo "<p>".$list['shortdesc']."</p><br style='clear:both'/>";
}
?>

Any help is much appreciated.

the $level is located in the welcome controller.

function cat($id){

    $cat = $this->MCats->getCategory($id);
    if (!count($cat)){
        redirect('welcome/index','refresh');
    }
    $data['title'] = "Company |" .$cat['name'];

    if ($cat['parentid'] < 1){
        // show other cats
        $data['listing'] = $this->MCats->getSubCategories($id);
        $data['level'] = 1;

        }else{
        // show products
        $data['level'] = 2;
        $data['listing'] = $this->MCats->getProductsByCategory($id);
        }
        $data['category'] = $cat;
        $data['main'] = 'category';
        $data['navlist'] = $this->MCats->getCategoriesNav();
        $this->load->vars($data);
        $this->load->view('template');
}

get products by category function… This selects the products based on category_id in the products table. still no show though…

 function getProductsByCategory($catid){
    $data = array();
    $this->db->select('id,name,shortdesc,thumbnail');
    $this->db->where('category_id',$catid);
    $this->db->where('status','active');
    $Q = $this->db->get('products');
    if ($Q->num_rows() > 0){
        foreach ($Q->result_array() as $row){
            $data[] = $row;
        }
    }
    $Q->free_result();
    return $data;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:05:59+00:00Added an answer on May 25, 2026 at 6:05 pm

    Try doing following. First you check in database, does requested category have products. Then check does it have subcategories. If sencond is false, then choose first and display products linked to main category.

    If main category has no products, but it does have subcategories and the do have products, then it work as described in your code.

    If main category has no products and no subcategories, then there is no point in displaying it at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The script below should be exluding items that are assigned to the my-menu category.
I'm trying to build a category/subcategory class with php & MySQL. I am stuck
What I want to do is a PHP script that gets all the products
In the below code there's a loop that features all products under a given
How to retrieve the value of type in the below XML <info><category>Flip</category><info>2</info><type>Tree</type></info>
Below is part of the XML which I am processing with PHP's XSLTProcessor :
I have created a category template in Wordpress for all posts that are in
I would like to ask a help about Zensor which is a plugin that
Possible Duplicate: Call php function from javascript I understand that php is server side
In the PHP function below, I want to exclude cat_ID=1 How can I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.