PHP: easy random picture code [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My code is random but its does not print out the picture just the text. When I add the IMAGE tag. Its go all wrong. Where do I go wrong?
$plaatje[] = 'afbeelding1.jpg';
$plaatje[] = 'afbeelding2.jpg';
$plaatje[] = 'afbeelding3.jpg';
$plaatje[] = 'afbeelding4.jpg';
$nummer2 = mt_rand(1,4);
echo "$plaatje[$nummer2]";

Use array_rand() - Picks one or more random entries out of an array
echo $plaatje[array_rand($plaatje)];
With an image tag, something like:
<img src="<?php echo $plaatje[array_rand($plaatje)]; ?>" />
or
echo "<img src='".$plaatje[array_rand($plaatje)]."' />";
Here is the documentation

Although you should add the image tag to show where it goes wrong, your code already has a problem as array indices are 0-based.
So you would need:
$nummer2 = mt_rand(0,3);

Related

incoporate php in html img tag [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
New to php and i have tried what I thought would work but it didnt work.
I have an image in my upload folder name 1002386235.png the number 1002386235 is the user id and I would like to display a profile picture of the user with the help of php. With this I can display the image. What I am trying to achieve is replace the 1002386235.png with something like $uid.png, $uid being a variable with the user id in it. I thought the following would work but it didnt
<?php
$uid = "1002386235";
?>
<img src="upload/".$uid.".png">
You need to wrap the variable $uid within php tags and since your html is not inside php tags, the concatenation dot operators are not required. Also, you need to echo the variable inside your html.
Change this line:
<img src="upload/".$uid.".png">
To this:
<img src="upload/<?php echo $uid; ?>.png">

Pass php value to html [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to pass a simple php value to html but I cannot format it in the right way. How can I do this?
<?php
$html = '
<img src="'$val'.svg" alt="15"> //Does not work
';
$val = 5;
?>
You would try to format it this way:
<?php
$val = 5;
$html = "<img src='${val}.svg' alt='15'> ";
?>

Echo steam avatar and steam name in php & html code without login [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to make a code who echo a users name and his avatar from steam, but i dont know how, i tryied this: http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=52A66B13219F645834149F1A1180770A&steamids=76561197960435530 but i dont know how to get just the username and the avatar from that info im getting, any help?
You must get the content of this file through file_get_contents and parse that json format to php array. After just get element personaname and avatar and use it with your HTML code
An example of how you could parse that json.
$json = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=52A66B13219F645834149F1A1180770A&steamids=76561197960435530');
$parsed = json_decode($json);
foreach($parsed->response->players as $player){
echo $player->personaname . '<br>';
echo "<img src='" . $player->avatar . "'>";
}
If you have any questions, feel free to ask!

find and replace special characters in PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Please tell me how to do a find and replace in PHP.
I nee to find Entry="OK" and replace with OK in a xml file.
I tried str_replace but confused because of the = and " in it.
Even though I'm not sure of the environment you're working in, here's a php sample that I think does what you want:
<html>
<?php
$teststr = "Entry=\"OK\"";
print $teststr . "<br>";
$newanswer = str_replace($teststr, "Entry=OK", $teststr);
print $newanswer . "<br>";
?>
</html>
Try this way
str_replace('Entry="OK"','OK',$var);
You can use str_replace its working
$r = 'Entry="OK" tyyuu hhh';
echo str_replace($r,'Entry="OK"','OK');
if any error found then u use preg_replace with corresponding patteren

Add text directly next to PHP variable? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a variable inside an inline style -
style='color:$custom_color;font-size:$custom_icon_size;'
And am trying to add the letters px directly after the $custom_icon_size. This obviously does not work? Any ideas what is the correct method?
thanks
you need to use {} around your PHP vaariable
style='color:$custom_color;font-size:${custom_icon_size}px;'
Assuming that you're echoing it, you have two options:
echo "style='color:$custom_color;font-size:{$custom_icon_size}px;'";
echo "style='color:" . $custom_color . ";font-size:" . $custom_icon_size . "px;'";
$style = 'color:'.$custom_color.'; font-size: '.$custom_icon_size.'px';
echo 'style="'.$style.'"';

Categories