Welcome Guest, Not a member yet? Register   Sign In
result_array() turns nothing but 'array' text.
#1

[eluser]iamsen47[/eluser]
Solution here.
http://ellislab.com/forums/viewthread/223400/#1026697

Explaination?
maybe when I figure it out.


I'm trying to load some data from this table called 'prob_ref', from this col called 'ref_code' where the 'tab_code' col's value is 'APPN'. There are 5 such rows out of 73 rows.

All I get is 73 lines of the word 'Array'.

row() and row_array() returns the top most row while result() returns a "Message: Object of class stdClass could not be converted to string" error.

What am I doing wrong?

This data will eventually be used to fill out options in a dropdown menu, with the corresponding 'tab_code' for the corresponding dropdown. But I can't get anything to work. Eventually, ref_code will be the option value while ref_desc will be the option text.

model
Code:
function get_tickets($tab = FALSE)
{
  if($tab === FALSE)
  {
   $q = $this->db->select('ref_code')
        ->from('prob_ref')
        ->where('tab_code',$tab);
   return $q->get()->result_array();
  }
}

controller
Code:
public function index()
{
  $tab='APPN';
  $data['form'] = $this->form_model->get_tickets();
  
  $this->load->view('form/index', $data);

}

view
Code:
<?php foreach ($form as $form_item): ?>

&lt;?php echo $form_item?&gt;<br>

&lt;?php endforeach ?&gt;
#2

[eluser]DarkManX[/eluser]
$form is an array of data-objects. $form_item is a single object and you try to print it. no way Wink
you gotta print an attribute of this object, like $form_item->ref_code
#3

[eluser]iamsen47[/eluser]
Could you explain or show me how that is done?

I tried doing
Code:
&lt;?php echo $form_item->ref_code?&gt;<br>

and all it generated an error "Trying to get property of non-object".

I tried
Code:
&lt;?php echo $form_item->['ref_code']?&gt;<br>
and all it generated was a single letter 'M', which I suppose is from the value which is 'MRS'.
#4

[eluser]DarkManX[/eluser]
http://ellislab.com/codeigniter/user-gui...sults.html

you can look up the function you use - result_array()
it return an array of objects, like:
Code:
array(
0 => Object(
  'name' => 'user 1'
),
1 => Object(
  'name' => 'user 2'
)
)

to but out second users name:
Code:
&lt;?= $result_array[1]->name; ?&gt;

you can just var_dump() your vars to see how you can get to the data you need.
#5

[eluser]davidbehler[/eluser]
Actually result_array() returns an array of arrays, one for each row in the result.

So this should work for you:
Code:
&lt;?php foreach ($form as $form_item): ?&gt;

&lt;?php echo $form_item['ref_code']?&gt;<br>

&lt;?php endforeach ?&gt;
#6

[eluser]DarkManX[/eluser]
oh sure, sry! Big Grin
#7

[eluser]iamsen47[/eluser]
Darkman

I rewrote the code as such earlier based on something I saw on stackoverflow

Code:
{
  
   $q = $this->db->select('ref_code, ref_desc')
        ->from('prob_ref')
        ->where('tab_code',$tab);
   $a = $q->get()->result_array();
  
   foreach($a as $row)
   {
    $b = array(
     'ref_code' => $ref_code,
     'ref_desc' => $ref_des
    );
   }
   return $b;
}

Right now I'm getting an undefined variable error. Sorry I'm trying my best but I really do not understand what's wrong or how to fix it. I've read the user guide but it's too technical for me with too little examples I can learn from.


David,
the problem with result_array() is that I'm only getting 'M' instead of 'MRS' in ref_code or 'Medical Record System' in ref_desc, and also that there's only 1 result when there should be 5 results corresponding to the 'APPN' tab_code.
#8

[eluser]iamsen47[/eluser]
I rewrote it again and now it displays all the rows with the APPN value.

I still don't know what I got wrong or right though.

Code:
function get_tickets2()
{
  $sql = "SELECT ref_code, ref_desc FROM prob_ref WHERE tab_code = ?";
  $q = $this->db->query($sql, array('APPN'));
  
  if($q->num_rows() > 0)
  {
   foreach($q->result() as $row)
   {
    $data[] = $row;
   }
   return $data;
  }
}

Code:
&lt;?php foreach ($rows as $r): ?&gt;

&lt;?php echo $r->ref_code; ?&gt;<br>
&lt;?php echo $r->ref_desc; ?&gt;<br>

&lt;?php endforeach; ?&gt;
#9

[eluser]DarkManX[/eluser]
Code:
foreach($a as $row)
   {
    $b = array(
     'ref_code' => $ref_code,
     'ref_desc' => $ref_des
    );
   }

well this stuff... $ref_code and $ref_des are not set, thats why you get the undefined error. what do you except to be in these vars? you but nothing there


result() returns an array with all matched data rows as objects
result_array() return an array with all matched data rows as array

so with result() you would want to grab like that $result_return[0]->column;
with the result_array() you have to grab it like that $result_return[0]['column']

just var_dump the stuff you get from the functions, then you can see which kind of stuff you have in these vars and then you can access the data.
#10

[eluser]NeoArc[/eluser]
Try this, if no data is found you wont get errors in the view.

Code:
function get_tickets2()
{
   $data = array(); //default value

  $sql = "SELECT ref_code, ref_desc FROM prob_ref WHERE tab_code = ?";
  $q = $this->db->query($sql, array('APPN'));
  if($q->num_rows())
  {
    $data = $q->result();  
  }
  return $data;
}




Theme © iAndrew 2016 - Forum software by © MyBB