• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mysql menu

#1
[eluser]georgerobbo[/eluser]
Hello,

I'm trying to build a mysql driven menu. I have the values in a table called menu and field called menu. I can pull the information from the database and insert it into the menu, but only one value. Add another value to the column and that replaces the previous value. I need to set it so all values from the menu column are posted.
Code:
<div id="menu">
            <ul>
                &lt;?php foreach($menu as $menu_item):?&gt;
                <li><a href="#">&lt;?php echo $menu_item;?&gt;</a></li>
                &lt;?php endforeach;?&gt;
            </ul>
        </div>


Code:
&lt;?php

class Root extends Controller {

    function Root()
    {
        parent::Controller();    
    }
    
     function index()
    {
        $query = $this->db->query('SELECT menu FROM menu');    
            
        foreach ($query->result() as $row)
        {
        $data['menu']= $row;
        }

        $this->load->view('meta');
        $this->load->view('header', $data);
        $this->load->view('panel');
        $this->load->view('content');
        $this->load->view('footer');
   }
}

I know I have got the
Code:
$data['menu'}
incorrect. What should replace it?

#2
[eluser]jedd[/eluser]
You might be better off with result_array() instead of looping through a row-by-row construct. Arrays are probably more useful to you than objects here, too.

Oh, and you'd normally do a rum_rows() first to check you've got >0 results available, before calling the result_array().

#3
[eluser]georgerobbo[/eluser]
I don't understand how to get all the values from the array into my $data['menu'] loop.

#4
[eluser]jedd[/eluser]
Instead of this:
Quote:
Code:
$query = $this->db->query('SELECT menu FROM menu');    
            
foreach ($query->result() as $row)  {
    $data['menu']= $row;
    }

... do this:

Code:
$query = $this->db->query ('SELECT menu FROM menu');
if ($query->num_rows() != 0)
    $data['menu'] = $query->result_array();

You might want to wrap other stuff into that logic - such as setting another variable to notify your menu view code that it's empty, or just handle an empty array in the view - or bomb out in the controller with a die() or similar, assuming that an empty menu at this point signifies an error with aborting on (it does imply something horrible has happened to your database connectivity, I think).

Depending how your view menu works - if it's a table of URL's say? - you might want to select * from menu instead, so you can do links to menu #'s rather than just menu names. But as I say, this depends on how you've set up your menu links.

#5
[eluser]Yash[/eluser]
Dude asking such Qs are always easy. Just read user guide bit more. And you will find it extremely useful and never ask such Qs.

Anyways hope you got answer from jedd


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.