Creature image

Location of the Videos
Post Reply
djand3y93
Posts: 16
Joined: Tue Nov 02, 2010 6:20 pm

Creature image

Post 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 />";

?>
User avatar
Nyaar
Posts: 15
Joined: Tue Nov 16, 2010 6:16 am

Re: Creature image

Post 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.
djand3y93
Posts: 16
Joined: Tue Nov 02, 2010 6:20 pm

Re: Creature image

Post by djand3y93 »

Thanks, I think that the second way is the simplest one.Thanks!
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creature image

Post 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.
The indelible lord of tl;dr
djand3y93
Posts: 16
Joined: Tue Nov 02, 2010 6:20 pm

Re: Creature image

Post 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!
User avatar
Nyaar
Posts: 15
Joined: Tue Nov 16, 2010 6:16 am

Re: Creature image

Post by Nyaar »

$avatar = $playerinfo3['name'].".jpg";
echo 'Player1 avatar is <img src="images/'.$avatar.'" />';
User avatar
Ravinos
Posts: 281
Joined: Tue Sep 15, 2009 4:14 pm

Re: Creature image

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

Return to “Older Browser MMO Videos”