Welcome Guest, Not a member yet? Register   Sign In
How can I get stdClass Object instead of an array
#1

[eluser]DaTran[/eluser]
I have the basics of passing an array of $data into the view thus turning them into objects to echo.

But im having trouble getting my array to come out like my working tutorial samples.

So my question is how can I get a stdClass Object array instead of a normal array

This is my print_r() of the two, maybe it'll give you a better understanding of my suffering.

The working
Code:
Array
(
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 4
                    [name] => Claymore
                    [price] => 25000
                )

            [1] => stdClass Object
                (
                    [id] => 7
                    [name] => Pink Glads
                    [price] => 80000
                )

        )

)

Non working
Code:
Array
(
    [records] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [min_bid] => 25000
                    [name] => Claymore
                )

            [1] => Array
                (
                    [id] => 2
                    [min_bid] => 26000
                    [name] => Pink Glads
                )

            [2] => Array
                (
                    [id] => 3
                    [min_bid] => 12000
                    [name] => Claymore
                )

        )

)

This is my controller function for both of them:
Code:
function index()
    {
        
        if($query = $this->model_auction->retreave())
        {
            $data['records'] = $query;
        }
        print_r($data);
        $this->load->view('view_auction', $data);
    }

and as you would expect a simple snippet of my viewing:
Code:
<?php foreach($records as $row): ?>
    <tr>
        <td>&lt;?php echo $row->item_name; ?&gt;</td>
        <td>&lt;?php echo $row->min_bid; ?&gt;</td>
    </tr>
&lt;?php endforeach; ?&gt;

Finally the problem the model page:
Code:
$query = $this->db->get('auction');
            
            $new_array = array();
            
            $i = 0; // Preset for multi-demi arrays
            foreach($query->result() as $row)
            {
                // Set new variables into new array
                $new_array[$i]['id'] = $row->id;
                $new_array[$i]['min_bid'] = $row->min_bid;
                
                // Get item name from database
                $this->db->where('id', $row->items_id);
                $item_query = $this->db->get('items');

                $item = $item_query->row_array();
                $new_array[$i]['name'] = $item['name'];
                $i++; // adds 1 and gets ready for new array in multi-demi array
            }
            return $new_array; // return multi-demi array


Sorry for so much code, im very new so please be kind.
#2

[eluser]Aken[/eluser]
Code:
$query = $this->db->get('auction');
            
            $new_array = array();
            
            foreach($query->result() as $row)
            {                
                // Get item name from database
                $this->db->where('id', $row->items_id);
                $item_query = $this->db->get('items');

                $item = $item_query->row_array();

                // Add a stdClass object to the final array.
                $new_array[] = (object) array(
                    'id' => $row->id,
                    'min_bid' => $row->min_bid,
                    'name' => $item['name']
                );

            }
            return $new_array; // return multi-demi array

You should also learn about SQL JOIN commands, which would allow you to attach the item name to the first query, and not need to run a second query inside each loop iteration. I could've written the code for you, but where's the fun in that? Smile
#3

[eluser]DaTran[/eluser]
Wow that was amazing!!! thank you, and thanks in advance for giving me a new topic to learn.
#4

[eluser]pickupman[/eluser]
Code:
$this->db->select('auction.*, items.name'); //all columns in auction table and name from items table
$this->db->join('items','items.id = auction.items_id');
$query = $this->db->get('auction');

//Make sure we have something to work with
if($query->num_rows() > 0){
            
            $new_array = array();
            
            $i = 0; // Preset for multi-demi arrays
            $result = $query->result(); //little speed boost
            foreach($result as $row)
            {
                // Set new variables into new array
                $new_array[$i]= (object)array('id' => $row->id,
                     'min_bid' => $row->min_bid,
                     'name'    => $row->name);
                
                ++$i; // slightly faster syntax
            }
     return $new_array; // return multi-demi array
   }
return FALSE; //no results found




Theme © iAndrew 2016 - Forum software by © MyBB