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.

1

4 Answers 4

1

Array to stdClass can be done in php this way.

stdClass:: __set_state(array());

Or a nicer way.

$a = (object) array();
Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

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);

2 Comments

The function is fails when I use $query->result(); Is there anything else I can do? Thank you.
I am getting a blank screen, no error is display even though my Apache environment is set to display all error.
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();

4 Comments

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
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(); }
Well, still the same. But can't understand why you voted me down when you couldn't even help. No offense meant.
because, the answer comes in codeigniter manual, although I did not vote negative to your question :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.