goodrichdesigns Posted March 21, 2008 Share Posted March 21, 2008 I'm very new to using PHP and MySQL and I'm having real difficulty with some code. Here it is. <?PHP if ($player->avatar == '') { echo "No avatar"; } else { print'<img src="'.$player['avatar'] .'" alt="'.$player['username'] .'">'; } ?> (The current code just meses up my whole page) Basically, I have a field in my database called avatar which is in the $player table. If there is an url within the field, I wish for that to be printed out in HTML as an image. If the field is blank, I want it to output as reading 'No avatar'. Someone please help, I'm a programming noob in need of much wisdom from another! lol Link to comment https://forums.phpfreaks.com/topic/97177-need-help-printing-an-image-url-from-a-mysql-database/ Share on other sites More sharing options...
Jeremysr Posted March 21, 2008 Share Posted March 21, 2008 That looks right, except at first you call it $player->avatar and then you call it $player['avatar']. The code shouldn't mess up your page though... Link to comment https://forums.phpfreaks.com/topic/97177-need-help-printing-an-image-url-from-a-mysql-database/#findComment-497272 Share on other sites More sharing options...
Trownt Posted March 21, 2008 Share Posted March 21, 2008 Have you tried making the img tag self closing by adding a / before the >? print'<img src="'.$player['avatar'] .'" alt="'.$player['username'] .'" />'; Depending on the browser and Document Type, this could be messing up the html rendering. Link to comment https://forums.phpfreaks.com/topic/97177-need-help-printing-an-image-url-from-a-mysql-database/#findComment-497275 Share on other sites More sharing options...
goodrichdesigns Posted March 21, 2008 Author Share Posted March 21, 2008 Hi thanks for the help however it still doesn't seem to work print'<img src="'.$player->avatar .'" alt="'.$player['username'] .'">'; ...doesn't work and neither does... print'<img src="'.$player['avatar'] .'" alt="'.$player['username'] .'" />'; Link to comment https://forums.phpfreaks.com/topic/97177-need-help-printing-an-image-url-from-a-mysql-database/#findComment-497466 Share on other sites More sharing options...
goodrichdesigns Posted April 2, 2008 Author Share Posted April 2, 2008 Hi all, I'm still struggling with this script. It's for an assignment which is due in on Tuesday, can anyone pllleeease help? <?PHP if ($player->avatar == 0) echo "No avatar"; else print'<img src='.$player['avatar] .'" alt="'.$player['username'] .'">'; ?> The default value for the avatar field of my database is 0. If the value is 0 I want text to be outputted on the page reading 'No Avatar'. If there is something else e.g. an URL, I want this to be printed as an image on the page. Link to comment https://forums.phpfreaks.com/topic/97177-need-help-printing-an-image-url-from-a-mysql-database/#findComment-507565 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.