Welcome Guest, Not a member yet? Register   Sign In
Non-Object Error?
#1

[eluser]Bl4cKWid0w[/eluser]
In the foreach loop that gets my news articles, I'm getting an error after each of the bodies of the articles. This is the error I'm getting:
Quote:A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/main_view.php

Line Number: 69

This is the code for my news:
Code:
<?php
        
         foreach($newsquery->result() as $row){
             $query = $this->db->query("SELECT * FROM news_comments WHERE articleID = '$row->newsID'");
                    $commentslink = anchor('index.php/news/'.$row->newsID.'/comments', "<img src='system/application/views/images/comments.png' width='20' height='14' border='0' alt='Comments'> (".$query->num_rows().")");
                                     echo "<h1>$row->newsTitle  $commentslink</h1>
                                       <p>$row->newsBody</p>";
                        $query2 = $this->db->query("SELECT * FROM members WHERE memberID = '".$row->newsAuth."'");
                            $result = $query2->row();
                 //Line 69//           $footer = "<h2>Posted by ".$result->memberName." on ".$row->newsDate."</h2>";
                                echo $footer;
        }
        
        ?&gt;
#2

[eluser]Randy Casburn[/eluser]
Just a guess, but I think your intent was to use the second query result?

So the line should read:

Code:
$footer = "<h2>Posted by ".$result2->memberName." on ".$row->newsDate."</h2>";

$result2

maybe??

Randy
#3

[eluser]thinkigniter[/eluser]
Your using an object and trying to echo it.

That's like... using a pencil case to write on a piece of paper.

From The Manual
Standard Query With Single Result
Code:
$query = $this->db->query('SELECT name FROM my_table LIMIT 1');

$row = $query->row();
echo $row->name;

The above row() function returns an object. Example: $row->name


Standard Query With Single Result (Array version)
Code:
$query = $this->db->query('SELECT name FROM my_table LIMIT 1');

$row = $query->row_array();
echo $row['name'];
The above row_array() function returns an array. Example: $row['name']
#4

[eluser]Randy Casburn[/eluser]
Well my first post was just plain dumb. I didn't even see: $result = $query2->row(); that! So you can ignore my first post.

@cjunky -- if you take a second look you'll the code is doing precisely what you prescribe.

@Bl4cKWid0w - (I'm always afraid to answer your posts because of that handle {brrr}) - Your use of variable names confounds things a little $row here and $result there has confused us a little.

For instance, $row from the foreach loop should be treated as an array since the foreach loop construct places the contents of multidimensional arrays into an array and not into an object. So when you are addressing the contents of the foreach array members, you really should address them with array notation and not object notation. This would help.

Randy




Theme © iAndrew 2016 - Forum software by © MyBB