[solved] printing an array of objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jriano
Forum Newbie
Posts: 4
Joined: Mon Jul 10, 2006 2:21 pm

[solved] printing an array of objects

Post 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>";
		}
Last edited by jriano on Tue Jul 11, 2006 3:54 pm, edited 2 times in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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 ;)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jriano
Forum Newbie
Posts: 4
Joined: Mon Jul 10, 2006 2:21 pm

Post 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
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post 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.
jriano
Forum Newbie
Posts: 4
Joined: Mon Jul 10, 2006 2:21 pm

Post by jriano »

jamiel, I don'tunderstand what you mean
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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>";
                }
jriano
Forum Newbie
Posts: 4
Joined: Mon Jul 10, 2006 2:21 pm

Post 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
Post Reply