Welcome Guest, Not a member yet? Register   Sign In
Why Does My Query Not Work ?
#1

[eluser]vincej[/eluser]
Hi ! I have a real simple query:

Code:
function getChild(){
        $message="No Available Data";                      /*Used in line 60*/
        $id=$this->attribute('id');
        $this->db->select('uri','id','title');
        $this->db->where('parent_id',$id);
        $Q = $this->db->get('default_pages');
        if ($Q->num_rows() > 0){
           foreach ($Q->result_array() as $row){
               $child[] = $row;}
                return var_dump($child);
        }
        else{return $message;}

    }

For some bizaar reason which I can not pin point I am only getting the first value out - the "uri", ID and Title are missing.

Code:
array
  0 =>
    array
      'uri' => string 'roofing/rubber/euroshake-heavy' (length=30)

However, if I remove URI from the select - I get ID ... the next one along. My gut feeling is that there is something wrong with the way I am formulating the result array. I have checked the CI guide - but can not see what I am doing wrong.

The data is there in the DB and the query is good - but what am I doing wrong with the array - I want all three values ?


Many Thanks !

#2

[eluser]Syllean[/eluser]
You're not altering the results array, so why not return it directly?

Code:
if ($Q->num_rows() > 0)
{
    return $Q->result_array();
}

Alternatively, you will need to do your loop like this:
Code:
if ($Q->num_rows() > 0)
{
    $i=0;
    foreach ($Q->result_array() as $row)
    {
        $child[$i]['uri'] = $row['uri'];
        $child[$i]['id'] = $row['id'];
        $child[$i]['title'] = $row['title'];
    }
    return $child;
}
I don't see any real benefit to the longer syntax unless you're going to add another key->value pair or manipulate the existing values in some way.

edit: added a counter to prevent the $child array key from incrementing for each item.
#3

[eluser]vincej[/eluser]
You are a rock star ! Thank you ! Thank you !

I've been away from CI for 6 months and clearly I have forgotten more than I knew.

Cheers !
#4

[eluser]Pert[/eluser]
It's because select only takes 1 attribute:

Code:
$this->db->select('uri','id','title');
// correct
$this->db->select('uri, id, title');

If you add second attribute to select method, it should be boolean, if set to false CI won't try to add quotes around your table and column names.




Theme © iAndrew 2016 - Forum software by © MyBB