user3172732 user3172732 - 4 years ago 152
SQL Question

result_array() ActiveRecord in Codeigniter

please look this query:

$this->db->select('users.user_id, user_info.name, user_info.fam ,user_type.type_name ');
$this->db->from('user_info');
$this->db->join('users', 'users.user_id = user_info.user_id');
$this->db->join('user_type', 'users.usr_typ_id = user_type.user_type');

$data= $this->db->get()->result_array();
var_dump($data);

echo "<hr>";

foreach ($data as $row);
{
echo $row->user_id ."|";
echo $row->name ."|";
echo $row->fam ."|";
echo $row->type_name ."|".'<br>';
echo "<hr>";
}


in the result:

the var_dump($data) returns following line

array(2) { [0]=> array(4) { ["user_id"]=> string(8) "92090001" ["name"]=> string(5) "parsa" ["fam"]=> string(5) "arian" ["type_name"]=> string(13) "Administrator" } [1]=> array(4) { ["user_id"]=> string(8) "92090002" ["name"]=> string(6) "pendar" ["fam"]=> string(6) "pajouh" ["type_name"]=> string(7) "Blogger" } }


but in "foreach" part shows following error for each line:

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/user_model.php
Line Number: 47


I will be thankful if somebody can help me.

Answer Source

Here you used result_array(). so it returns result with array with stdobject. So you have to print below type.

$i=0;
foreach ($data as $row);
{
    echo $row[$i]->user_id ."|";
    echo $row[$i]->name ."|";
    echo $row[$i]->fam ."|";
    echo $row[$i]->type_name ."|".'<br>';
    echo "<hr>";
    $i++;
}

If you are using only result() that time you can use your code.

foreach ($data as $row);
{
    echo $row->user_id ."|";
    echo $row->name ."|";
    echo $row->fam ."|";
    echo $row->type_name ."|".'<br>';
    echo "<hr>";
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download