Passing Params to a Codeigniter Library
I have a custom library (in application\libraries) which I can call fine, however I want to pass data from model, via the controller: In the controller:
$MenuData['daily'] = $this->bookmarks_model->getDaily();
$this->load->library('MyMenu');
$menu = new MyMenu;
$data['menu'] = $menu->ShowMenu($MenuData);
In the MyMenu library:
function ShowMenu($Params)
{
$CI =& get_instance();
$CI->load->helper('url');
$CI->config->item('base_url');
//More Code here
$menu .= "<li><a href='#'>开发者_如何转开发;Daily</a>";
$menu .= " <ul>";
foreach($daily as $row) :
$menu .= "<li><a href='" . $row->url . "' target='_blank'>" . $row->short_title . "</a></li>";
endforeach;
$menu .= " </ul> ";
$menu .= " </li>";
//More Code here
}
return $menu;
However I'm getting an undefined variable error and invalid arguments for for each. Any help greatly appreciated!
You are passing a multi-dimensional array ($MenuData
)to the function, then trying to pass one of the second level arrays ($MenuData['daily']
) inside the MD array to the foreach loop without referencing the first level. Instead of :
foreach($daily as $row) :
Try:
foreach($Params['daily'] as $row) :
Or before your foreach loop declare a variable to hold the second level array:
$daily = $Params['daily']
foreach($daily as $row):
Sorry but it may just be me. This .=
means concatenate at the end of the string already found in the variable but you haven't used it before now. So should this line:
$menu .= "<li><a href='#'>Daily</a>";
be:
$menu = "<li><a href='#'>Daily</a>";
精彩评论