I a trying to generate a menu for a webshop. I have written a method for it.
Slim says "undefined variable on line 53. I have marked the line with *
Class Menu {
function generate_menu() {
$menu = 'ul';
$catergory = Model::factory('category')->find_many();
***foreach ($category as $item) {***
$menu .= '<li>';
$menu .= '<a href = "' . $item->link . '">' . $item->title . '</a>';
$menu .= '</li>';
$count = Model::factory('sub_category')->where('category_title', $item->title)->count();
if ($count >= 0) {
$sub_catergory = Model::factory('sub_category')->where('category_title', $item->title)->find_many();
$menu .= '<ul>';
foreach ($sub_catergory as $sub_catergory) {
$menu .= '<li>';
$menu .= '<a href = "' . $sub_category->link . '">' . $sub_category->title . '</a>';
$menu .= '</li>';
}
$menu .= '</ul>';
}
}
$menu .= '</ul>';
return $menu;
}
}
Could someone show me how to pass this function to the template in slim php ?
This is the get request i am trying to pass the returned $menu variable to.
$app->get('/', function () use ($app) {
$site_info = Model::factory('Site_info')->where('id', '1')->find_one();
$app->render('front_page.php', array(
'site_name' => $site_info->site_name,
'site_slogan' => $site_info->site_slogan,
'domain' => $site_info->domain
));
});
Also could someone point me in the right direction to what paris returns if a database request returns nothing?
My method for creating the menu :
Class Menu {
function generate_menu() {
$menu = '<ul>';
$category = Model::factory('category')->find_many();
foreach ($category as $item) {
$menu .= '<li>';
$menu .= '<a href = "' . $item->link . '">' . $item->title . '</a>';
$menu .= '</li>';
$count = Model::factory('sub_category')->where('category_title', $item->title)->count();
if ($count >= 0) {
$sub_category = Model::factory('sub_category')->where('category_title', $item->title)->find_many();
$menu .= '<ul>';
foreach ($sub_category as $value) {
$menu .= '<li>';
$menu .= '<a href = "' . $value->link . '">' . $value->title . '</a>';
$menu .= '</li>';
}
$menu .= '</ul>';
}
}
$menu .= '</ul>';
return $menu;
}
}
After that i assigned the method output to a variable and passed it to the template :
$app->get('/', function () use ($app) {
$site_info = Model::factory('Site_info')->where('id', '1')->find_one();
$category_menu = Menu::generate_menu();
$app->render('front_page.php', array(
'site_name' => $site_info->site_name,
'site_slogan' => $site_info->site_slogan,
'domain' => $site_info->domain,
'menu' => $category_menu
));
});