Welcome Guest, Not a member yet? Register   Sign In
problem in listing category and subcategory in the same page
#8

[eluser]Nick_MyShuitings[/eluser]
Its prolly a bit out of your php level, but what you need here is a fancy recursive function to work its way down N-levels as needed and give you a fancy little array. Then a similar magic function to print the sucker out. Otherwise: yes... you need to just add another foreach.

If you're serious about it... then check out these two functions of mine. These suckers work with the whole id, parent_id paradigm, where a table makes reference to previous rows to provide for N level deep nesting:

Code:
/**
   * Get Head
   *
   * @return array
   * Use: takes a parent_id as an input and outputs that item
   */
  public function _get_head($id, $table = 0) {
    $table = ($table === 0)?$this->_table:$table;
    $this->db->where($table.'_id', $id);
    if ($this->condition_field) {
      $this->db->where($this->condition_field, '0');
    }
    $this->db->order_by($this->order_field, "asc");
    $query = $this->db->get($table);
    return $query->result_array();
  }
  /**
   * Get Items
   *
   * @return array
   * Use: takes a parent_id as an input and outputs all the children
   */
  public function & _get_items($id = 0, $table = 0) {
    $table = ($table === 0)?$this->_table:$table;
    $itemOverview =& $this->_get_head($id, $table);
    foreach ($itemOverview as $key => $val) {
      $item = $itemOverview[$key];
      if ($item['id'] != 0) {
        $childItems =& $this->_get_items($item['id'], $table);
      }
      if (!empty($childItems)) {
        $item['children'] =& $childItems;
      }
      $items[] = $item;
    }
    return $items;
  }

and I am way to tired to try and explain how that works right now... either read up on how recursive functions work then study that code, or just slap another foreach into your code and you can go another level deeper.

PS... urgent is your problem, nobody here on the forum feels your urgency... and the fact that you posted again an entire day and a half later without any "hey I tried all of this and still no luck" is even more of a telltale...


Messages In This Thread
problem in listing category and subcategory in the same page - by El Forum - 05-08-2011, 11:58 PM



Theme © iAndrew 2016 - Forum software by © MyBB