patternphpMinor
Mutlilevel page list in CodeIgniter
Viewed 0 times
mutlilevellistpagecodeigniter
Problem
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.
My controller:
```
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->db->query($sql);
if($query->num_rows() > 0)
{
$res = $query->result_array();
foreach($res as $row):
$items[$row['id_parent']][] = $row;
endforeach;
if($type =='list') $this->_menu_list($items); //renders ul tags
if($type =='dropdown') $this->_menu_dropdown($items); //renders option tag
return $this->model_result;
}
}
function _menu_list($items, $parent = null)
{
$index = $parent == null ? '0' : $parent;
if (isset($items[$index]))
{
$this->model_result .= 'model_result .= $parent == null ? ' id="category"
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 '';
$dropdown = $this->my_model->get_menu($id_menu,'dropdown');
echo $dropdown;
echo ''; //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->db->query($sql);
if($query->num_rows() > 0)
{
$res = $query->result_array();
foreach($res as $row):
$items[$row['id_parent']][] = $row;
endforeach;
if($type =='list') $this->_menu_list($items); //renders ul tags
if($type =='dropdown') $this->_menu_dropdown($items); //renders option tag
return $this->model_result;
}
}
function _menu_list($items, $parent = null)
{
$index = $parent == null ? '0' : $parent;
if (isset($items[$index]))
{
$this->model_result .= 'model_result .= $parent == null ? ' id="category"
Solution
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.
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.
Context
StackExchange Code Review Q#9407, answer score: 2
Revisions (0)
No revisions yet.