Jump to content

Working with Array


sunnyd

Recommended Posts

Hi all,

 

New to this forum and I hope you can help me.  I am trying to build my own shopping cart and so far everything is going well.  I am stuck on one thing however.  I am trying to setup a Category > Sub Category structure for my products.  This is the sql that I use for the categories table

 

CREATE TABLE `shop_category` (
   `cat_id` int(10) unsigned not null auto_increment,
   `cat_parent_id` int(11) not null default '0',
   `cat_name` varchar(50) not null,
   `cat_description` varchar(200) not null,
   `cat_image` varchar(255) not null,
   PRIMARY KEY (`cat_id`),
   KEY `cat_parent_id` (`cat_parent_id`),
   KEY `cat_name` (`cat_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11

 

And the PHP function that I am trying to use is shown below:

 

function fetch_categories()
    {
        /* $this->db->order_by('cat_name');
        $query = $this->db->get('shop_category');
        if ($query->num_rows > 1)
        {
            return $query->result_array();
        } */
        
        $data = array();
        $this->db->select('cat_id,cat_name,cat_parent_id');
        // $this->db->where('status', 'active');
        $this->db->orderby('cat_parent_id','asc');
        $this->db->orderby('cat_name','asc');
        $this->db->groupby('cat_parent_id,cat_id');
        $Q = $this->db->get('shop_category');
        if ($Q->num_rows() > 0){
        foreach ($Q->result() as $row){
        	if ($row->cat_parent_id > 0){
        		$data[0][$row->cat_parent_id]['children'][$row->cat_id] = $row->cat_name;
        	
        	}else{
        		$data[0][$row->cat_id]['cat_name'] = $row->cat_name;
        	}
        }
        }
        $Q->free_result(); 
        return $data;  
        
    }

 

 

 

In my controller, I call the function using this code:

 

function categories()
    {
        $data['site_name'] = "Site Name";
        $data['title'] = "Products";
        $data['meta']       = array('description' => 'welcome to codeigniter',
                                    'keywords' => 'welcome, codeigniter, ci',
                                );
        $data['categories'] = $this->products_model->fetch_categories();
        $data['content'] = $this->load->view('categories', $data , TRUE);    
        $this->load->view($this->template , $data);
    }

 

 

I then  tried to dump the result of the function in my view and this is what I got (and I am assuming that is correct):

 

Array
(
    [0] => Array
        (
            [4] => Array
                (
                    [cat_name] => Books
                    [children] => Array
                        (
                            [8] => Autobiography
                            [7] => Fiction
                            [5] => Horror
                            [6] => Science Fiction
                        )

                )

            [1] => Array
                (
                    [cat_name] => Cars
                    [children] => Array
                        (
                            [3] => Luxury
                        )

                )

            [2] => Array
                (
                    [cat_name] => Manga
                )

        )

)

 

Unfortunately, I can seem to be able to put this into a usable format.  Ideally, what I would like to have is something like a table, that show the main Parent category, then the subcategories are listed below it.  Any help on this would be appreciated. Thanks

 

Sunnyd

 

P.S.  I should mention that I am using CodeIgniter.

 

Link to comment
https://forums.phpfreaks.com/topic/179954-working-with-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.