0

I am returning data from two tables in CodeIgniter with the function below

public function test()
{
    $this->db->select('*');
    $this->db->from('WHOUSE1.DLY_BWR_DLY_PERFORMANCE');
    $this->db->join('WHOUSE1.DATE_DIM', 'WHOUSE1.DATE_DIM.DATE_KEY = WHOUSE1.DLY_BWR_DLY_PERFORMANCE.BDP_DATE');
    $query = $this->db->get();
    return $query->result_array();
}

Using var_dump I am getting the result below

array (size=3226)
0 => 
array (size=121)
  'BDP_ID' => string '945149' (length=6)
  'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
  'BDP_DATE' => string '20040807' (length=8)
  'BDP_DAY_CODE' => string '6' (length=1)
  'BDP_TAKE' => string '4923.78' (length=7)
  'BDP_PAYOUT' => string '3779.22' (length=7)
  'BDP_ACTUAL_SLIPPAGE' => string '636' (length=3)

1 => 
array (size=121)
  'BDP_ID' => string '945150' (length=6)
  'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
  'BDP_DATE' => string '20040809' (length=8)
  'BDP_DAY_CODE' => string '1' (length=1)
  'BDP_TAKE' => string '2848.3' (length=6)
  'BDP_PAYOUT' => string '4190.34' (length=7)
  'BDP_ACTUAL_SLIPPAGE' => string '280' (length=3)

But what I will like to get is this

  array (size=3226)
  0 => 
  object(stdClass)[27]
  'BDP_ID' => string '945149' (length=6)
  'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
  'BDP_DATE' => string '20040807' (length=8)
  'BDP_DAY_CODE' => string '6' (length=1)
  'BDP_TAKE' => string '4923.78' (length=7)
  'BDP_PAYOUT' => string '3779.22' (length=7)
  'BDP_ACTUAL_SLIPPAGE' => string '636' (length=3)

  1 => 
  object(stdClass)[29]
  'BDP_ID' => string '945150' (length=6)
  'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
  'BDP_DATE' => string '20040809' (length=8)
  'BDP_DAY_CODE' => string '1' (length=1)
  'BDP_TAKE' => string '2848.3' (length=6)
  'BDP_PAYOUT' => string '4190.34' (length=7)
  'BDP_ACTUAL_SLIPPAGE' => string '280' (length=3)

I can't seem to get a way of converting the array into object(stdClass) Any help will be appreciated as am new to CodeIgniter.

Rebe24
  • 91
  • 1
  • 4
  • 12
  • Does this answer your question? [How to convert an array to object in PHP?](https://stackoverflow.com/questions/1869091/how-to-convert-an-array-to-object-in-php) – steven7mwesigwa Sep 28 '20 at 18:51

4 Answers4

1

Array to stdClass can be done in php this way.

stdClass:: __set_state(array());

Or a nicer way.

$a = (object) array();
Beaudinn Greve
  • 810
  • 12
  • 17
1

Use this code in Your Model ( change your table name and select, distinct fileds )

        $this->db->select('DISTINCT(subcategory)');
        $this->db->from('tbl_property');  
        $this->db->where('status','1');                          
        $data           = $this->db->get()->result(); 
        $sub_id         = array();
        foreach ($data as $row)
        {
            array_push($sub_id,$row->subcategory);
        }
        $this->db->from('tbl_subcategory');  
        $this->db->where_in('id',$sub_id);                          
        $data1          = $this->db->get()->result();
        return $data1;
0

use

return $query->result();

This function returns the query result as an array of objects, or an empty array on failure. Typically you'll use this in a foreach loop, like this:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}

Edit:

If you just want to print you can use var_dump() or print_r().

var_dump($obj);
print_r($obj);

If you want an array of all properties and their values use get_object_vars().

$properties = get_object_vars($obj);
print_r($properties);
Ing. Michal Hudak
  • 5,338
  • 11
  • 60
  • 91
  • The function is fails when I use $query->result(); Is there anything else I can do? Thank you. – Rebe24 Nov 10 '13 at 00:00
  • I am getting a blank screen, no error is display even though my Apache environment is set to display all error. – Rebe24 Nov 10 '13 at 00:12
0

According to the same documentation for Codeigniter I detail:

result_array()

This function returns the query result as a pure array, or an empty array when no 
result `is produced. Typically you'll use this in a foreach loop, like this:`

and ..

result()

This function returns the query result as an array of objects, or an empty array 
on failure.

You should return your data in this way

return $query->result();
John
  • 544
  • 4
  • 19
  • Hi John, my function fails using $query->result(); Is there a way to re-write function test() so as not to fail. I only use result_array(); to pull out data so I can explain what I wanted to do – Rebe24 Nov 10 '13 at 00:46
  • Look, public function test() { $this->db->join('WHOUSE1.DATE_DIM', 'WHOUSE1.DATE_DIM.DATE_KEY = WHOUSE1.DLY_BWR_DLY_PERFORMANCE.BDP_DATE'); return $this->db->get('WHOUSE1.DLY_BWR_DLY_PERFORMANCE')->result(); } – John Nov 10 '13 at 00:48
  • Well, still the same. But can't understand why you voted me down when you couldn't even help. No offense meant. – Rebe24 Nov 10 '13 at 01:00
  • because, the answer comes in codeigniter manual, although I did not vote negative to your question :( – John Nov 10 '13 at 01:05