Mutlilevel page list in CodeIgniter I'm newly working with CodeIgniter and PHP. I wrote a category structured 'pages' code with listing pages as multilevel list or indented select box.

page                                        page_lang
-------------------------------------       ----------------------------
id_page | id_menu | id_parent | level       id_page |  title     | etc..
-------------------------------------       ----------------------------
   1    |    1    |     0     |   0            1    | Name 1     | etc..
   2    |    1    |     1     |   1            2    | Name 1.1   | etc..
   3    |    1    |     2     |   2            3    | Name 1.1.1 | etc..
   4    |    1    |     2     |   1            4    | Name 1.2   | etc.


My controller:

function get_menu_content($id_menu, $id_parent = 0, $level = 0)
{
  echo '&lt;select&gt;';
  $dropdown = $this-&gt;my_model-&gt;get_menu($id_menu,'dropdown');
  echo $dropdown;
  echo '&lt;/select&gt;';  //echo in controller for quick test purpose
}


My_model has three functions: the first is getting result array, the other two is rendering as list and options.

class My_model extends CI_Model
{

  public $model_result;

  function __construct()
  {
     parent::__construct();
  }

function get_menu($id_menu = '1',$type = 'list')
{
  $sql = "
  SELECT a.id_parent, b.id_page, a.level, b.title 
  FROM page a
  INNER JOIN page_lang b ON a.id_page = b.id_page 
  WHERE 
    a.id_menu = $id_menu";  
  $query = $this-&gt;db-&gt;query($sql);  

  if($query-&gt;num_rows() &gt; 0)
  {
    $res = $query-&gt;result_array();
    foreach($res as $row):
      $items[$row['id_parent']][] = $row;
    endforeach;
    if($type =='list') $this-&gt;_menu_list($items); //renders ul tags
    if($type =='dropdown') $this-&gt;_menu_dropdown($items); //renders option tag
    return $this-&gt;model_result;
  }
}

function _menu_list($items, $parent = null)
{
  $index = $parent == null ? '0' : $parent;  
  if (isset($items[$index]))
  {
    $this-&gt;model_result .= '&lt;ul';
    $this-&gt;model_result .= $parent == null ? ' id="category" ' : '';
    $this-&gt;model_result .= '&gt;';    
    foreach ($items[$index] as $child)
    {
      $this-&gt;model_result .= '&lt;li rel="'.$child['id_page'].'"&gt;'.$child['title'];
      $this-&gt;_menu_list($items, $child['id_page']);
      $this-&gt;model_result .= '&lt;/li&gt;';
    }    
    $this-&gt;model_result .= '&lt;/ul&gt;';
  }
}

function _menu_dropdown($items, $parent = null, $level = '0')
{
  $index = $parent == null ? '0' : $parent;
  $space = str_repeat(' &amp;nbsp &amp;nbsp ', $level);
  if (isset($items[$index]))
  { 
    foreach ($items[$index] as $child)
    { 
      $this-&gt;model_result .= '&lt;option value="'.$child['id_page'].'"&gt;'.$space.$child['title'].'&lt;/option&gt;' . "\n";
      $this-&gt;_menu_dropdown($items, $child['id_page'],$level+1);
    }
  }
}


It works a bit slowly. I need a cleaner code I guess. I will appreciate any improvement on the code based on my database structure.
One general comment -> YOU ARE NOT ESCAPING $id_menu IN YOUR SQL QUERY!

There is a section on how to do this in the CodeIgniter Database Query Documentation.

I'm not familiar with Code Igniter but there is nothing obvious that jumps out at me as to why this would be slow.