Welcome Guest, Not a member yet? Register   Sign In
result as array vs. object
#1

[eluser]roadie[/eluser]
Hi guys!

I have this function but I want to change it to get the result as objects not arrays. I've been trying for a while but can't figure it out. I'd appreciate your help!

Model: categories_model.php

Code:
function get_bCategories(){
        $this->db->where('type', 'business');
        $query = $this->db->get('categories');
        $result = $query->result_array();
            foreach ($result as $key => $row){
                $query = $this->db->get_where('companies',array('cat_id'=>$row['id']));
                $row['companies'] = $query->result_array();
                $result[$key] = $row;
            }
        return $result;
    }

Controller:
Code:
$data['bCategories'] = $this->categories_model->get_bCategories();

View:
Code:
<?php foreach($bCategories as $bCategory): ?>
<h3>&lt;?php echo $bCategory['name']; ?&gt;</h3>
    <ul>
       &lt;?php foreach( $bCategory['companies'] as $company): ?&gt;
       <li>&lt;?php echo $company['name']; ?&gt;</li>                                    
       &lt;?php endforeach; ?&gt;
    </ul>
&lt;?php endforeach; ?&gt;
#2

[eluser]SPeed_FANat1c[/eluser]
If I understand well you should use $result = $query->result() instead of $result = $query->result_array();

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




Theme © iAndrew 2016 - Forum software by © MyBB