Cannot set menu name
I am having difficult to display menu with this code. In the parent menu i just get 'Array' displayed instead of menu name. Here is the code.
model
function getCategoriesNav(){
//page 157
$data = array();
$this->db->select('id, name, parentid');
$this->db->where('status', 'active');
$this->db->order_by('parentid', 'asc');
$this->db->order_by('name', 'asc');
$this->db->group_by('parentid', 'id');
$Q = $this->db->get('categories');
if($Q->num_rows >0){
foreach($Q->result() as $row){
if($row->parentid > 0){
$data[0][$row->parentid]['children'][$row->id] = $row->name;
}else{
$data[0][$row->id]['name'] = $row->name;
}
}
}
$Q->free_result();
return $data;
}
controller is
function index()
{
$data['title'] = 'News';
$data['navlist'] = $this->MCats->getCategoriesNav();
$data['mainf'] = $this->MProducts->getMainFeature();
$skip = $data['mainf']['id'];
$data['sidef'] = $this->MProducts->getRandomProducts(3, $skip);
$data['main'] = 'home';
$this->load->vars($data);
$this->load->view('template');
}
and the view is
<?php
if(count($navlist)){
echo "<ul>";
foreach($navlist as $key => $list){
foreach($list as $topkey => $toplist){
echo "<li class='cat'>";
echo anchor("welcome/cat/$topkey", $toplist);
echo "</li>\n";
if(count($toplist['children'])){
foreach($toplist['children'] as $subkey => $subnam开发者_StackOverflow中文版e){
echo "\n<li class='subcat'>";
echo anchor("welcome/cat/$subkey", $subname);
echo "</li>";
}
}
}
}
echo "</ul>\n";
}
?>
please help
This is my examination of your array structure:
You have a four level multi-dimensional array to hold a row name.
[0][parent id]['children'][row id] = row name.
Your foreach(navlist as key=>list)
sets up [parent id] as $key
and ['children'][row id] = row name as $list
.
Your foreach($list as $topkey=>$toplist)
sets up ['children'] as $topkey
and [row id] = row name as $toplist
.
Your call to anchor('welcome/cat/$topkey, $toplist)
is sending an array to the anchor helper function, which may be why you are seeing 'array' output to the browser. This function only accepts a string for the 'text to display' argument.
EDIT
To answer your comment, try this in your view:
Instead of:
echo anchor("welcome/cat/$topkey", $toplist);
Use this:
echo anchor("welcome/cat/$topkey", $toplist['name']);
Now you will be passing the value of $data[0][$row->id]['name']
which is your construct to hold $row->name
in your model.
EDIT 2:
I also notice your use of
if($Q->num_rows >0){
You are missing the parenthesis for the num_rows function.
if($Q->num_rows() > 0) {
This may have something to do with your missing parent and children categories. If not, I recommend removing your group_by()
clause from the query, just to see if that is messing things up for you. If those don't help, keep remove the order_by()
's and see what happens.
精彩评论