Display avatar from database.

Hi there. I’m new to PHP in general and picked Yii as my chosen framework of choice  8)

I'm using the Yii-app tutorial. I have a field 'avatar' that contains an avatar for each record. What class do I use to return the image in the list, show, and update views? 

Thanks and great work.

Simple. Add an avatar field to your user table. Then add this to your _post.php view.

<?php echo $post->author->avatar; ?>

It's a very crude method but it SHOULD do the rest for you. You can just wrap that in image tags and add an if empty statement to set a default avatar if you like.

In fact this will do it for you.

<img src="


<?php


if (isset($post->author->avatar)) {


    echo $post->author->avatar;


} else {


	echo "http://i39.tinypic.com/103c12s.jpg";


}


?>


;" alt="Author Avatar">

Not sure if that's the cleanest way to do it but it does work. If an avatar is set it prints the URL stored in the database. If it is not set (null) Then it prints a default avatar of your choosing. Remember though if you do this that the database HAS to default to null. If it's only blank I don't think it'll work.

Oh, and if someone would like to clean that up and do it better, Please be my guest. I'm sure there has to be a cleaner way to do this.

Akian0,

are you storing a link to the avatar into the database or the actual byte-code of the avatar?

Thanks Bios and Jonah. The avatars are saved as links and are pulled from Joomla's Community Builder component image folder. I'm creating an engine to update profiles so I need a preview of the avatars in all records.

For the heck of it:



<?php


$img = isset($post->author->avatar) ? $post->author->avatar : 'http://defaultAvatar'; 


echo CHtml::image($img)


Thanks Jonah. See, I knew there had to be a better way. >.> I just didn't know it.

Thanks for the help! That worked  ;)