Welcome Guest, Not a member yet? Register   Sign In
Looping recursivley repeatedly
#1

[eluser]HunterJoe1977[/eluser]
So I am may not be doing this right, in fact I am pretty sure I am not at this point, but it seems to me that every single time I query a database, I end up with either an array of stdClassObjs or an array of arrays, and I then have to loop through them twice to get the data I need or want. Let me give you an example to demonstrate from my Model:

MODEL :
Code:
function getListingID($unique_ID) {
        $sql = "SELECT id FROM tbl_name WHERE uniqueID = " . $unique_ID;
        $q = $this->db->query($sql);
        
        $list = array();
        if($q->num_rows() == 1){
            foreach($q->result() as $row) {
                foreach($row as $k => $v) {
                    $list[$k] = $v;
                }                
            }
            $id = $list['id'];
            
            return $id;
        } else {
            return false;
        }
    }

All I want is the id of the row from the tbl_name table, but for some reason if I use this code instead, I get an fatal error (Fatal error: Cannot use object of type stdClass as array):

Code:
function getListingID($unique_ID) {
        $sql = "SELECT id FROM tbl_name WHERE uniqueID = " . $unique_ID;
        $q = $this->db->query($sql);
        
        $list = array();
        if($q->num_rows() == 1){
            foreach($q->result() as $row) {
                $id = $row['id'];
            }            
            return $id;
        } else {
            return false;
        }
    }

Can someone please explain to me why in CI I have to loop through everything twice, or what it is I am not doing right that is causing me to have to do this? Sorry for being such a noob, and thank you.
#2

[eluser]Kenneth Vogt[/eluser]
The issue is that you are trying to iterate through an object as if it is an array. Check out the php docs on this at http://php.net/manual/en/language.oop5.iterations.php
#3

[eluser]dnc[/eluser]
Yes I agree, you are trying to return an array when obj expected.

Try this and please let me know if it works as I cannot test it on my local server:

Code:
function getListingID($unique_ID) {
        $sql = "SELECT id FROM tbl_name WHERE uniqueID = " . $unique_ID;
        $q = $this->db->query($sql);
        
        $list = array();
        if($q->num_rows() == 1){
          foreach($q->result() as $row) {
             $id = $row->id; // Here is change
            }            
            return $id;
        } else {
            return false;
        }
    }
#4

[eluser]Aken[/eluser]
Gotta love when people recommend something unnecessary. Why use a loop when you're expecting ONE result?

Code:
$q = $this->db->query($sql);

if ($q->num_rows() === 1)
{
    return $q->row()->id;
}
else
{
    return FALSE;
}

// Or, you can do a ternary if/else statement to save room.
return ($q->num_rows() === 1) ? $q->row()->id : FALSE;

http://ellislab.com/codeigniter/user-gui...sults.html
#5

[eluser]Tim Post[/eluser]
Doup, didn't notice the alternate (ternary) example before replying Smile

#6

[eluser]HunterJoe1977[/eluser]
ok, Aken, thank you for that response. I understand I was using an array reference for an object. I am not used to working with objects, so I had forgotten about the object reference operator (i.e. -> or whatever it is called - if it even has a name).

Now, this works with one row, but what about returning multiple rows from a database, I normally would end up with and array of objects, but if I use result_array() I get and array of arrays instead, in that case I do need to recursively loop through the array and extract each array's items like the coed below, correct?

EXAMPLE:
Code:
function getFeatures($id) {
        $feature_data = array();
        
        $feature_sql = 'SELECT feature_tbl.feature_id, feature_tbl.feature_name FROM feature_map INNER JOIN feature_tbl ON feature_map.listing_id = '.$id.' AND feature_map.feature_id = feature_tbl.feature_id';
        $f = $this->db->query($feature_sql);
        $count = 1;
        foreach ($f->result_array() as $key=>$value) {
            foreach ($value as $k=>$v) {
                $feature_data[$k . '_' . $count] = $v;
            }
            $count++;
        }
        
        return $feature_data;
    }

Or is there a better way? What I was missing in the first post was the row() method, is there something similar that I am missing to deal with the code above, or is it correct?
#7

[eluser]Aken[/eluser]
Do you need to manipulate the data you retrieve from the database? If not, there's no reason you need to loop through anything. Just return the resulting array of objects or array of arrays - whichever format you want is the one you should use.
#8

[eluser]dnc[/eluser]
[quote author="Aken" date="1338353923"]Gotta love when people recommend something unnecessary. Why use a loop when you're expecting ONE result?

Code:
$q = $this->db->query($sql);

if ($q->num_rows() === 1)
{
    return $q->row()->id;
}
else
{
    return FALSE;
}

// Or, you can do a ternary if/else statement to save room.
return ($q->num_rows() === 1) ? $q->row()->id : FALSE;

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





Your right, sorry `bout that, didn't really look at his complete code. I shouldn't have posted when I didn't have time.




Theme © iAndrew 2016 - Forum software by © MyBB