0

I am a starter in CodeIgniter. I am creating a small application in Ci where am loading my menu from database .

  1. The database consists of 3 fields (id,menu_Name,menu_link)

  2. I have created a function in Libraries to fetch data from db

  3. Created a controller by loading the library

Am attaching the code which i have created

Code For libraries Folder Structure ::::---- libraries/Functions.php

function getMenus()
{
    $arrRow =array();
    $sql    = "select * from tbl_menus ";
    $res    =mysql_query($sql); 
    if(mysql_num_rows($res) >0) {
        while($row= mysql_fetch_object($res)){
                $arrRow[]   =$row;
        }

    }

} 

Controller Code

    public function index()
{
   $val_menu = $this->input->post('menu_Name');
   $val_link= $this->input->post('menu_link');
   $query=$this->db->get_where('tbl_menus',array('menu_Name'=>$val_menu,'menu_link'=>$val_link));

   if ($query->num_rows() > 0)
   {
    $arrRow=$query->row_array();
    $data=$arrRow;
   }
    $data=array('menu_Name'=>$data,'menu_link'=>$data);
    $this->load->helper('url');
    $this->load->database();

    $this->load->view('Header');  
    $this->load->view('SideMenu',$data);     
    $this->load->view('Pages/MainPage',$data);   
    $this->load->view("Footer");  
}

My View Code

<?php

$arrMenus=array();
$arrMenus   =$this->functions->getMenus();

?>

<div class="container">

<div class="sidebar1">



<?php if(count($arrMenus) >0) {
foreach($arrMenus as $key_menu=>$val_menu) {
$menu_link  =$val_menu['menu_link'];?> 
<ul class="nav">
//Before Edit
<li><a href="<?=site_url()?>/<?=$menu_link?>" class="lnk"><?=$val_menu['menu_name']?>
//After Edit
<li><a href="<?=site_url()?>/<?php echo $menu_link?>" class="lnk"><?php echo  $val_menu['menu_name']?></a></li>  
</a></li>
</ul>

Print_r() result

Array ( [menuId] => 1 [menu_Name] => UserHome [menu_link] => Pages/MainPage ) 

View is not working properly..ie the view page is not displaying the menu...so i used print_r($arrMenus) inside the controller;..its showing the data...but menu links are left blank .Print_r($arrMenus) in view page displaying error that conversion problem

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Well, that won't work either because it isn't properly formatted. http://php.net/manual/en/language.basic-syntax.phptags.php Short tags allow you to use ` ?>` instead of ``. If they aren't enabled on the server, ` ?>` will NOT work. Try using `` instead of ` ?>` to see if that is the problem. `` – stormdrain Sep 30 '13 at 15:55
  • can you post the result of `print_r($arrMenus);` – omma2289 Sep 30 '13 at 15:57
  • Hello short tag is already enabled in my wamp...so i changed the code to
  • echo $val_menu['menu_name']?>
  • – Nidheesh N Namboodhiri Sep 30 '13 at 16:01
  • When i put print_r() it works well but now it is dead – Nidheesh N Namboodhiri Sep 30 '13 at 16:06