Part of the EllisLab Network
   
 
Want to show categories with subcategories
Posted: 18 November 2009 07:31 AM   [ Ignore ]  
Summer Student
Total Posts:  9
Joined  01-08-2008

Hi everybody.

Basically i’m a designer. But now i want to do some work with php mysql. I’m practicing php and the framework codeigniter.

I have the basic understanding of codeigniter.
I can show the category but now i want to
- show the category with subcategories like the following:

  - category-1
    - subcat-1
    - subcat-2
    - subcat-3
    - subcat-4

  - category-2
    - subcat 2-1
    - subcat 2-2
    - subcat 2-3
    - subcat 2-4

I want this approach bcz i want to show them with an accordion menu.

Please some one help me that how can i get this types of view.

Thanks.
Shahidul

Profile
 
 
Posted: 18 November 2009 08:05 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3133
Joined  06-11-2007

Depends. Do you want one level exactly like you have shown, or unlimited levels?

  - category-1
  - subcat-1
  - subcat-2
  - subcat-3
    - sub-sub-cat-3-1
  - subcat-4

Unlimited gets tricky if you want to show a whole tree for navigation, etc.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 19 November 2009 01:39 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  9
Joined  01-08-2008

Thanks for your reply Phil Sturgeon

I just want them one level. I have just categories in my category table and subcategories in my subcategory table. Now i want to view them in an accordion menu.
as:
- category-1
    - subcat - 1 of category -1
    - subcat - 2 of category -1
    - subcat - 3 of category -1
    - subcat - 4 of category -1
  - category-2
    - subcat - 1 of category -2
    - subcat - 2 of category -2
    - subcat - 3 of category -2
    - subcat - 4 of category -2

Thanks.
Shahidul

Profile
 
 
Posted: 19 November 2009 05:40 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3133
Joined  06-11-2007

Your categories table should at minimum have the following fields “id | title | parent_id”.

Then you have two simple queries.

SELECT * FROM categories WHERE parent_id = 0;

SELECT * FROM categories WHERE parent_id = $foo;

If a parent = 0, then it is a main category. If a parent is set, it is a sub category.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 19 November 2009 08:04 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  74
Joined  11-02-2009

You could have two tables

catagories
cat_id
cat_name

subcatagories
sub_id
sub_name
parent_id (foreign key cat_id)

that way it is easier to manage if you delete entire catagories and may be easier to see the catagories if the list gets large.

then when you want to process your loop

$catagores // results from select * from catagories;
$subs // results from select * from subcatagories;

foreach ($catagories as $cat)
{
    
echo $catagory->cat_name;
    
// 1. either be lazy and search through all sub-cats
    // for this parent

    // 2. Or you can perform the sql query inside here
    // SELECT *.. .. where parent_id = $cat->cat_id

    
foreach($subs as $sub)
    
{
        
if ($sub->sub_id === $cat->cat_id)
            echo 
$sub->sub_name// and any info
    
}

 Signature 

Repopulating multiple select items

Profile
 
 
Posted: 19 November 2009 08:34 AM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3133
Joined  06-11-2007

Why have two tables when one works just as well?

Using self-joins you can do selects and deletes to make it just the same as having two, without having to double up on code and schema.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 19 November 2009 01:33 PM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  74
Joined  11-02-2009

The post description suggested that Shahidul was a designer and was practicing php so I assumed it would be easier to for Shahidul if the steps were broken down into two tables to clearly see the logic and approach for said task.

yeh I would probably go one myself as a subcatagory extends catagory in OOP terms. Having it in the same table would also allow for more nested layers as well.

Yes doubling on the schema but both involve nested foreach loop and the same two select statements. so I’m not sure about the doubling of the code? I can’t see how to do it in one fell swoop.

Having two tables may be useful if a category has different fields from a subcategory and by making it a relational there is no need to worry about dealing with orphans manually EDIT:(for beginner).

I wasn’t trying to one-up you mate hey I was just suggesting a different approach.
*thumbs up smiley (they dont have one)*

 Signature 

Repopulating multiple select items

Profile
 
 
Posted: 20 November 2009 10:36 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  9
Joined  01-08-2008

Thanks both Phil Sturgeon & andrewtheandroid for your valuable time and replies.

andrewtheandroid, i understood your codes and it do work for me i changed it a bit.
I do the second query with the first queries result and it working.

I have the following Models::

function getCategory()
    
{
        $query 
$this->db->get_where('trainings_category', array('cat_status' => '1'));        
        return 
$query->result();
    
}
     
function getTrainingsName($id)
    
{
        $query 
$this->db->get_where('trainings_name', array('training_cat_id' =>$id,'training_title_status' => '1'));        
        return 
$query->result();
    

And the following view i created to show the accordion menu.

<div id="basic-accordian" >
    <!--
Parent of the Accordion-->
    <!--
Start of each accordion item-->
<?php foreach($cat_list as $t_cat): ?>

    
<div id="<?= $t_cat->cat_id ?>-header" class="accordion_headings" ><?$t_cat->cat_name ?></div>
    <!--
Heading of the accordion clicked to show n hide ) -->
    <!--
Prefix of heading (the DIV above this) and content (the DIV below thisto be same... egfoo-header foo-content-->
    <
div id="<?= $t_cat->cat_id ?>-content">
      <!--
DIV which show/hide on click of header-->
      <!--
This DIV is for inline styling like padding...-->
      
<? $training_name_query $this->Accordion_model->getTrainingsName($t_cat->cat_id);?>
      <?php 
foreach($training_name_query as $t_name): ?>
      
<div class="accordion_child"><a href="#"></a>
       
          <
div>
            <
div class="A_menu_item"><?$t_name->training_title?></div>
          </
div>
      </
div>
      
<?php endforeach ?>
    
</div>
    <!--
End of each accordion item-->
    <!--
Start of each accordion item-->
<?php endforeach ?>
    
<!--End of each accordion item-->
</
div

=> If the above structure ok?

Thanks.
Shahidul

Profile
 
 
Posted: 21 November 2009 01:57 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  74
Joined  11-02-2009

Yeh that looks good so you’re going to get the Jquery accordian action hey?

If you are using firefox I recommend an addon called ‘View Source Chart’ it can show you your source in a formatted structure so you can easilly check the structure of your containers esp with deeply nested divs/spans.

 Signature 

Repopulating multiple select items

Profile
 
 
Posted: 21 November 2009 02:07 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  9
Joined  01-08-2008

Yes.

Thanks everybody.

Profile
 
 
   
 
 
‹‹ PHP Frameworks      CI #3 on Google ››