• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange "trying to get property of non-object" error

#1
[eluser]Loquela[/eluser]
The query below seems to work fine, but when I print the result it always throws TWO "Trying to get property of non-object" errors before printing the rows out!

Any ideas why?


Code:
function menu_topics($data)
    {
        $this->db->select('pp_topics.topic_id,pp_topics.topic,pp_topic2subject.topic_id,pp_topic2subject.subject_id');
        $this->db->from('pp_topics');
        $this->db->join('pp_topic2subject', 'pp_topic2subject.topic_id = pp_topics.topic_id ');
        $this->db->where('pp_topic2subject.subject_id', $data['subject_id']);

        $query = $this->db->get();
        
        if($query->num_rows() > 0) {
            foreach($query->result() as $topics) {
                $data[] = $topics;
            }
            return $data;
        }    
    }

Code:
<?php foreach($topics as $r): ?>
<p>&lt;?php echo $r->topic; ?&gt;</p> &lt;!-- Error on this line, but rows print successfully after error --&gt;
&lt;?php endforeach; }?&gt;

#2
[eluser]Loquela[/eluser]
If it helps a var_dump of the object $topics gives:

Code:
array(3) {
["subject_id"]=>  
string(2) "17"
["subjects"]=>  array(8) {
[0]=>  object(stdClass)#18 (2) {
["subject_id"]=>  string(2) "21"
["subject"]=>  string(23) "Fairy tales and stories" }
[1]=>  object(stdClass)#19 (2) {
["subject_id"]=>  string(2) "17"
["subject"]=>  string(9) "Geography" }
[2]=>  object(stdClass)#20 (2) {
["subject_id"]=>  string(2) "16"
["subject"]=>  string(7) "History" }
[3]=>  object(stdClass)#21 (2) {
["subject_id"]=>  string(2) "25"
["subject"]=>  string(27) "Personal, social and health" }
[4]=>  object(stdClass)#22 (2) {
["subject_id"]=>  string(2) "34"
["subject"]=>  string(8) "Religion" }
[5]=>  object(stdClass)#23 (2) {
["subject_id"]=>  string(2) "15"
["subject"]=>  string(7) "Science" }
[6]=>  object(stdClass)#24 (2) {
["subject_id"]=>  string(2) "33"
["subject"]=>  string(12) "TEST SUBJECT" }
[7]=>  object(stdClass)#25 (2) {
["subject_id"]=>  string(2) "35"
["subject"]=>  string(6) "Tester" } }
[0]=>  object(stdClass)#26 (3) {
["topic_id"]=>  string(1) "5"
["topic"]=>  string(6) "Forces"
["subject_id"]=>  string(2) "17" } }

#3
[eluser]Loquela[/eluser]
Sorted. I was mixing my variables and objects up!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.