Parsaria
Parsaria

Reputation: 1099

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.

Upvotes: 1

Views: 12884

Answers (4)

Puttu
Puttu

Reputation: 98

You need access $row->user_id and other data in for loop as like this "$row['user_id']"

Upvotes: 1

Parsaria
Parsaria

Reputation: 1099

these tow are way are tested an i get result:

first one is:

$data= $this->db->get()->result();

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

the second one:

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

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

}

Upvotes: 0

krutssss
krutssss

Reputation: 964

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>";
}

Upvotes: 2

Bora
Bora

Reputation: 10717

You should use result() instead of result_array()

$data = $this->db->get()->result();

result() is returns the query result as an array of objects

result_array() is returns the query result as a pure array

Or if you continue with result_array(), you should change access type like following:

Use $row['name'] instead of $row->name

Documentation: http://ellislab.com/codeigniter/user-guide/database/results.html

Upvotes: 2

Related Questions