Welcome Guest, Not a member yet? Register   Sign In
Help converting small function to CI Framework... Newbie :/
#1

[eluser]carlhussey[/eluser]
Hey guys.

I'm trying to figure out how to write this function for my menu to work with CI and I was wondering if anyone could help me convert it so I can see how it was done.

All its doing is pulling a menu from the database and formatting it with html. Id like to be able to just return is as a single value that I can send to my view.

Code:
<?php
   $hostname = 'localhost';
   $username = 'dev';
   $password = 'shadow';
   $database = 'dev';
  
   try {
      $db = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
   }
   catch (PDOException $e) {
      echo $e->getMessage();
   }

   menu($db);

   function menu($db) {
      $menu = array();
      
      $query = "SELECT * FROM menu WHERE parent = 0";
      $query = $db->prepare($query);
      $query->execute();
      
      echo "<ul id=\"main-nav\">";

      while ($d = $query->fetch(PDO::FETCH_ASSOC)) {
         if ($d['parent'] == 0) {
            $url = $d['location'];
            $title = $d['title'];
        $class2 = $d['class2'];
          
            echo "<li><a >$title</a></li> ";
            
            $q2 = $db->prepare("SELECT * FROM menu WHERE parent = :id");
            $q2->bindParam(':id', $d['id']);
            $q2->execute();
            
         echo "<ul>";
        
            while ($child = $q2->fetch(PDO::FETCH_ASSOC)) {
                $ctitle = $child['title'];
                $curl = $child['location'];
                
              
               echo "<li><a >$ctitle</a></li>";
            }
            echo "</ul></li>";
            echo "<br />";
            
         }
      }
   }
?&gt;
#2

[eluser]CI_avatar[/eluser]
Set you database configuration
http://ellislab.com/codeigniter/user-gui...odels.html (See connection to database)
Code:
$hostname = 'localhost';
$username = 'dev';
$password = 'shadow';
$database = 'dev';

Make a model that will handle database query
http://ellislab.com/codeigniter/user-gui...odels.html
Code:
$query = "SELECT * FROM menu WHERE parent = 0";
$query = $db->prepare($query);
$query->execute();

Make a view that will populate your output
http://ellislab.com/codeigniter/user-gui...views.html
Code:
echo "<ul id=\"main-nav\">";

      while ($d = $query->fetch(PDO::FETCH_ASSOC)) {
         if ($d['parent'] == 0) {
            $url = $d['location'];
            $title = $d['title'];
        $class2 = $d['class2'];
          
            echo "<li><a >$title</a></li> ";
            
            $q2 = $db->prepare("SELECT * FROM menu WHERE parent = :id");
            $q2->bindParam(':id', $d['id']);
            $q2->execute();
            
         echo "<ul>";
        
            while ($child = $q2->fetch(PDO::FETCH_ASSOC)) {
                $ctitle = $child['title'];
                $curl = $child['location'];
                
              
               echo "<li><a >$ctitle</a></li>";
            }
            echo "</ul></li>";
            echo "<br />";
            
         }
      }

After making this part into MVC structure. Make a controller that will call your model (get the menus and pass it to the controller), if the controller have the menus, pass it to the view you have created.
#3

[eluser]carlhussey[/eluser]
I understand the structure and how it to do each of the files but for example I dont believe "while ($d = $query->fetch(PDO::FETCH_ASSOC)) {" is something that would work in the CI framework so I was trying to find out its equivalent
#4

[eluser]CI_avatar[/eluser]
try this one:

http://ellislab.com/codeigniter/user-gui...sults.html
#5

[eluser]carlhussey[/eluser]
is WHILE and foreach the same then? I dont see any use of a while loop in those queries

Like i said, I'm new to this so the below code is probably way off

Code:
&lt;?php

class Menu_model extends CI_Model {
    
    
     function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    

    function menu() {
    
      $menu = array();
      
            $query_str = "SELECT * FROM menu WHERE parent = 0";
      
            $c1 = "<ul id=\"main-nav\">";
            
            foreach ($query_str->result() as $row)
            {
                if($row->parent == "0"){

            $url = $row->location;
            $title = $row->title;
            $id = $row->id;
            
            $c2 = "<li><a >$title</a></li> ";
            
            $query_str2 = "SELECT * FROM menu WHERE parent = $id";
            
            $c3 = "<ul>";
        
        
           foreach ($query_srt2->result() as $row2)
            {
                
            $ctitle = $row2->title;
            $curl = $row2->location;
                
            $c4 = "<li><a >$ctitle</a></li>";
            
            }
            $c5 = "</ul></li>";
            
         }
      }
    
        return $c1.$c2.$c3.$c4.$c5;
   }
}
?&gt;
#6

[eluser]CI_avatar[/eluser]
yes your right. you can use foreach instead of while-loop.
#7

[eluser]carlhussey[/eluser]
Pretty much got it figured out. Now I just need to figure out the URI part :/

Any tips for that part? I added some notes in the code to what I need to do
Code:
&lt;?php

class Menu_model extends CI_Model {
    
    
     function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    

    function menu($page) {

     $current = $page;  //Page is = account, crackers, schedule
                        // $c1 = 'current'; <- Root Category
                        // $c2 = 'class=\"current\"'; <- Child Category
                        //If $page is password, it is under the root of account
                        //and the child is password. So both $c1 and $c2 need to
                        //has those populated for just those 2 links.

        
      
            $query_str = $this->db->query("SELECT * FROM menu WHERE parent = 0");
      
                $output = "<ul id=\"main-nav\">";
            
                      foreach ($query_str->result() as $row)
                      {
                         if($row->parent == "0"){

                            $url = $row->location;
                            $title = $row->title;
                            $id = $row->id;
        

                $output .= "<li><a >$title</a></li> ";
            
            $query_str2 = $this->db->query("SELECT * FROM menu WHERE parent = $id");
            
               $output .= "<ul>";
        
        
                    foreach ($query_str2->result() as $row2)
                    {
                
                        $ctitle = $row2->title;
                        $curl = $row2->location;
                
                $output .= "<li><a >$ctitle</a></li>";
            
                    
                
            
           }
           $output .= "</ul></li>";
         }
        
      }
    
      return $output;  
   }
  
}
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB