Welcome Guest, Not a member yet? Register   Sign In
Category DB call convert to CI - Model/Controller help
#1

[eluser]Devon Lambert[/eluser]
Can anyone tell me how to convert the following to proper CI syntax:

// This would go in the Model //

Code:
function get_categories($where_sql='') {



        global $db;


        $categories = array();

        $query = "SELECT name, type FROM categories ".$where_sql." ORDER BY categories.order ASC";

        $result = $db->query($query);


        while (($category = mysql_fetch_array($result))) {

            $categories[] = $category;

        }


        return $categories;

    }

// This would be the call in the controller //

Code:
$categories = $this->mdl_home->get_categories();

Not really sure what the purpose of the "$db" and "$where_sql" variables are in this function but it appears that the $db variable is being used as a function similar to that of CI's db class?

Sorry still new to all this. :-)

UPDATE: Thought it would help if I gave what I have so far?

Code:
function get_categories() {



        $categories = array();


        $this->db->select('name,type');
        $this->db->order_by('order','asc');
        $query = $this->db->get('categories');

        //Tried this first

        //$result = $this->db->query('$query');
        //while ($category = $result->result_array())

        // Tried this second
        //$result = $query->result_array();


        while ($category = $query->result_array())
        {

            $categories[] = $category;

        }

        return $categories;

    }
#2

[eluser]gtech[/eluser]
umm well this would work
Code:
function get_categories($where_sql='') {
    $query = "SELECT name, type FROM categories ".$where_sql." ORDER BY categories.order ASC";
    $result = $this->$db->query($query);
    return $result->result_array();
}
#3

[eluser]Devon Lambert[/eluser]
gTech to the rescue.

Again you dig me out of a hole. :-)
#4

[eluser]gtech[/eluser]
No problems: the result_array() function looks as though it does the same as the while loop in the original function.
#5

[eluser]Devon Lambert[/eluser]
Ok one more thing gTech, or anyone else who has any idea how this conversion works.

I'm trying to pass the $categories variable to my view file as an array but for some reason PHP is throwing back an undefined variable error.

Here is the code so far:

// In the controller //

Code:
$categories = $this->mdl_home->get_categories();
$this->load->view('view_file', $categories);

// In the view //

Code:
<ul>
&lt;?php foreach($name as $cat_name):?&gt;
<li>&lt;?php echo $cat_name;?&gt;</li>
&lt;?php endforeach;?&gt;
</ul>
#6

[eluser]gtech[/eluser]
well result_array will just return somthing like

array('0'=>array('col1'=>'data','col2'=>'data'),'1'=>array(....etc)...etc);

so when passed in to the views you will have the variables $0 $1 ect defined this is not what you want.

instead pass the result_array inside an array.
Code:
$viewdata = array();
$viewdata['categories'] = $this->mdl_home->get_categories();
$this->load->view('view_file', $viewdata);

then the view will have the $categories array available to you in the view.
Code:
<ul>
&lt;?php foreach($categories as $key => $value):?&gt;
  <li>&lt;?php echo $key.":".$value;?&gt;</li>
&lt;?php endforeach;?&gt;
</ul>

if you did
&lt;?php print_r($categories); ?&gt; in the view would would see how the array is built up.
#7

[eluser]Devon Lambert[/eluser]
Thanks again gTech.

Worked like a charm.

I still need to brush up on these arrays/objects.




Theme © iAndrew 2016 - Forum software by © MyBB