Page 1 of 1

Creature image

Posted: Fri Dec 03, 2010 4:22 pm
by djand3y93
Hi,
I am trying to show the creature's avatar but i can't figure it up.
I want it to be like this: creature name (from database)+.jpg

Here is the code, i try this for the player name:

Code: Select all

<?php
include 'connect.php';

$playerinfo = "select * from players where name = 'player1'";
$playerinfo2 = mysql_query($playerinfo) or die ("Could not select players");
$playerinfo3 = mysql_fetch_array($playerinfo2);

echo "Player1 email is " . $playerinfo3['email'];
echo "<br>";
echo "Player1 avatar is " . <img src=$playerinfo3['name'].jpg />";

?>

Re: Creature image

Posted: Fri Dec 03, 2010 4:46 pm
by Nyaar
Well, that's not the creature part but here's the problem.
echo "Player1 avatar is " . <img src=$playerinfo3['name'].jpg />";
should be
echo "Player1 avatar is <img src=\"".$playerinfo3['name'].".jpg\" />";
Note that the img tag needs quotes around the source in the final code. Also, you only need to concatenate the variable into the string. The html code is fine in the string itself and besides, you have a total of 3 quote marks so one of them isn't being closed.
Also, the slashes escape the quotes. It allows us to print quotes even though they're used to tell what we should echo.

If escaping characters looks too confusing, here's an alternative.
$avatar = $playerinfo3['name'].".jpg";
echo 'Player1 avatar is <img src="'.$avatar.'" />';
(In that case, the quotes in the middle are " then ' followed by ' then ". You shouldn't space them out or I would have.)

Hope I helped some. Not the best at explaining things but feel free to ask more questions if you need to.
As a final note, make sure that picture exists or it still won't show up.

Re: Creature image

Posted: Fri Dec 03, 2010 5:13 pm
by djand3y93
Thanks, I think that the second way is the simplest one.Thanks!

Re: Creature image

Posted: Sat Dec 04, 2010 12:33 am
by Jackolantern
Nyaar is right. While technically you can make it where the array is figured automatically due to the double-quotes, there is an issue. The ".jpg" after the array reference is likely confusing PHP when together with the array index operator (the [ ]). That is because a period is the string concatenation operator, and I don't even know if PHP allows for "in-string resolution" (what you were doing where you don't exit out of the string when displaying variables) with array references. All of these things taken together equals one confused PHP parser.

Re: Creature image

Posted: Sat Dec 04, 2010 2:30 pm
by djand3y93
Now I have another problem, i have the images in the folder called "images" and I want to display them.
How could I do it?
Thanks in advice!

Re: Creature image

Posted: Sat Dec 04, 2010 3:43 pm
by Nyaar
$avatar = $playerinfo3['name'].".jpg";
echo 'Player1 avatar is <img src="images/'.$avatar.'" />';

Re: Creature image

Posted: Sat Dec 04, 2010 6:58 pm
by Ravinos
I do mine just pulling from the players database after they pick their avatar or upload their own. In the player's or creature's table add 'avatar' as a field with the full link to their image i.e. 'images/playeravatarimage.jpg'

Then call
$playeravatar = "$playerinfo3[avatar]";
$creatureavatar = "$creatureinfo3[avatar]";

echo "Player1's avatar is <img src="$playeravatar">";
echo "Creature's avatar is <img src="$creatureavatar">;

of course you will want to set your width and height for the image. If you allow users to upload their own images you don't want them destroying your page layout.