Page 1 of 1

[solved] printing an array of objects

Posted: Tue Jul 11, 2006 12:02 pm
by jriano
hello guys, is there something wrong with this code? there are 3 articles, but it just prints the last one 3 times!

Code: Select all

		while($row=mysql_fetch_array($result ,MYSQL_ASSOC)){
			$tempArticle->id=$row['id'];
			$tempArticle->category=$row['category'];
			$tempArticle->title=$row['title'];
			$tempArticle->body=$row['body'];
			$tempArticle->author=$row['author'];
			$tempArticle->created=$row['created'];
			$tempArticle->updated=$row['updated'];
			$tempArticle->status=$row['status'];
		
			$this->article[]=$tempArticle;
			$n++;				
		}
		foreach($this->article as $art){
			echo "<h1>$art->title</h1><p>$art->body</p>";
		}

Posted: Tue Jul 11, 2006 12:36 pm
by pickle
Maybe you're only getting one row in your result?

Also, using [syntax="php"][/syntax] tags is better than [syntax=php][/syntax] tags for PHP code because we'll get syntax highlighting as well ;)

Posted: Tue Jul 11, 2006 12:48 pm
by jriano
thanks pickle, but actually if i include this line inside the while loop it prints the right data

Code: Select all

echo "<h1>$tempArticle->title</h1><p>$tempArticle->body</p>";
So I think the problem is on how I assign the tempArticle object to the article[] array of objects, but I dont see why

Posted: Tue Jul 11, 2006 12:50 pm
by jamiel
It will because you are calling $tempArticle which you just set in the previous loop and not using your newly formed array at all.

Posted: Tue Jul 11, 2006 12:53 pm
by jriano
jamiel, I don'tunderstand what you mean

Posted: Tue Jul 11, 2006 12:57 pm
by jamiel
Why do you need an array of objects?

Anyways, var_dump($this->articles) before your loop. Lets see what you've done before then.

Posted: Tue Jul 11, 2006 12:58 pm
by Luke
looks like you are replacing the values in your object by the next record. I would imagine that the record being shown is the third one, right? You need to do something more like:

Code: Select all

while($row=mysql_fetch_array($result ,MYSQL_ASSOC)){
                        $tempArticle = new article; // ADD THIS LINE
                        $tempArticle->id=$row['id'];
                        $tempArticle->category=$row['category'];
                        $tempArticle->title=$row['title'];
                        $tempArticle->body=$row['body'];
                        $tempArticle->author=$row['author'];
                        $tempArticle->created=$row['created'];
                        $tempArticle->updated=$row['updated'];
                        $tempArticle->status=$row['status'];
               
                        $this->article[]=$tempArticle;
                        $n++;            
                }
                foreach($this->article as $art){
                        echo "<h1>$art->title</h1><p>$art->body</p>";
                }

Posted: Tue Jul 11, 2006 1:23 pm
by jriano
The Ninja Space Goat: yes your change does it! so that means that the elements of the array article[] have pointers to articleTemp but no copies? I was thinking that article[1....n] each n would have a copy of what tempArticle was at the moment of the assignement!
Thanks to all you guys