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


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.
<div id="menu">
                &lt;?php foreach($menu as $menu_item):?&gt;
                <li><a href="#">&lt;?php echo $menu_item;?&gt;</a></li>
                &lt;?php endforeach;?&gt;


class Root extends Controller {

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

        $this->load->view('header', $data);

I know I have got the
incorrect. What should replace it?

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().

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

Instead of this:
$query = $this->db->query('SELECT menu FROM menu');    
foreach ($query->result() as $row)  {
    $data['menu']= $row;

... do this:

$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.

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.