BO1990
BO1990

Reputation: 1

Display categories in Codeigniter / PHP

I am trying to display categories stored in my database:

http://dl.dropbox.com/u/16459516/Screen%20shot%202012-05-05%20at%2015.59.40.png

My idea was to get all data out of the database within my model and the reconstruct the array within my function:

function get_all_categories() {

    $query = $this->db->query('SELECT * FROM categories');

    //code to reconstruct my array like:
    //foreach($query->result_array() as $row) {
        //$categories[$row['niveau_1']] = array[]
    //}

    return $categories;
}

and output some array like:

[0] => Array
       (
       [niveau_1] => main category name
       [niveau_2] => Array(
                        ...children here and children of these childs
                        )
       )

Does anyone know how to do this? Im stuck here and dont know how to create the array... i know how to display it in my views but getting them in the array seems hard for me.

Thanks in advance!

Upvotes: 0

Views: 1310

Answers (3)

Dalen
Dalen

Reputation: 9006

This is an idea:

  1. foreach category (except level 4) check if it is present into the result array($c)
  2. if it is not then create an array using a label the category's name
  3. once controlle categories 2 and 3 you can insert the last one inside the generated array

$query = $this->db->query('SELECT * FROM categories');

$raw_categories = $query->result_array();

$c = array();

foreach($raw_categories AS $row)
{
   if(!array_key_exists($row['niveau_2'],$c) && !empty($row['niveau_2']))
   {
      $c[$row['niveau_2']] = array();
   }

   if(!array_key_exists($row['niveau_3'],$c[$row['niveau_2']]) && 
      !empty($row['niveau_3']))
   {
      $c[$row['niveau_2']]['niveau_3'] = array();
   }

   $c[$row['niveau_2']]['niveau_3'][] = $row['niveau_4'];
}

this is not tested but you can use it as an idea

Upvotes: 2

uzsolt
uzsolt

Reputation: 6027

I think your database is wrong. As I understand it's a "table of content" or similar (category, sub-category, sub-sub-category, etc.). "1. Visie, Beleid..." appears many-many times.

If I'd do I change the database:

table niveau_1
    id_1 - primary key
    title - string
table niveau_2
    id_2 - primary key
    id_1 - connect to niveau_1 (which niveau_1 is its 'parent')
    title - string
table niveau_3
    id_3 - primary key
    id_2 - connect to niveau_2 (which niveau_2 is its parent)
    title - string

In this case in your database will not duplicated elements and I think your desired query will be simplier:

SELECT
  niveau_1.title AS title_1,
  GROUP_CONCAT(niveau_2.title,"@") AS title_2,
  GROUP_CONCAT(niveau_3.title,"@") AS title_3
FROM niveau_1
LEFT JOIN niveau_2 ON niveau_2.id_1=niveau_1.id_1
LEFT JOIN niveau_3 ON niveau_3.id_2=niveau_2.id_2
GROUP BY niveau_2.id_2

This query is only a half-solution, it isn't perfect for you but you can start from here.

Upvotes: 1

NDBoost
NDBoost

Reputation: 10634

// your model using activerecord
public function getCategories(){
  $q = $this->db->get('categories');
  if($q->num_rows > 0){
    return $q->result();
  }else{
    return false;
  }
}

//your controller
$this->load->model('category_model');
$data['results'] = $this->category_model->getCategories();
foreach($data['results'] as $c){
  //iterate through categories, match them to points.
  //match category_id in results with category_id in the other table results
  //setup a multi dimensional array to build up what you need.
}

Upvotes: 0

Related Questions