Noob At PHP trying to display an image from a folder

Hey all. I am using MAMP as my local server and I have a folder with 5 images in it named image1-5.png. We have to make a simple grading forum where you put in the grade and you get the letter grade back. I just wanted to diplay image 1 for a’s image 2 for b’s and so on but Im having trouble with the syntax to call the image and a conditonal statment to display the right image for the associated grade. Her is my code:

<?php $score="0"; $letter=""; $score=$_REQUEST['score']; IF ($score>92) {$letter="A";} ELSE IF ($score>89) {$letter="A-";} ELSE IF ($score>86) {$letter="B+";} ELSE IF ($score>82) {$letter="B";} ELSE IF ($score>79) {$letter="B-";} ELSE IF ($score>76) {$letter="C+";} ELSE IF ($score>72) {$letter="C";} ELSE IF ($score>69) {$letter="C-";} ELSE IF ($score>66) {$letter="D+";} ELSE IF ($score>62) {$letter="D";} ELSE IF ($score>59) {$letter="D-";} ELSE {$letter="F";} ECHO "Your Letter Grade Is: $letter"; ?>

Sorry Im such a noob thanks for the help

One way would be to assign another string to your image name in each of your if statements. For example:

[php]
ELSE IF ($score>89) {$letter=“A-”; $image=“a.jpg”;}
[/php]

Then just display the image using an image tag in your echo statement.

Alright Ive added in what you provided but my out put is the letter grade and just the path to the image and not the actual image itself. I dont know if this jsut has to do with where im placing the images in the folder or if i have to create a directory for this to work. Here are my additions to the code:

<?php $score="0"; $letter=""; $image=""; $score=$_REQUEST['score']; IF ($score>92) {$letter="A"; $image="images\image1.png";} ELSE IF ($score>89) {$letter="A-"; $image="images\image1.png";} ELSE IF ($score>86) {$letter="B+"; $image="images\image2.png";} ELSE IF ($score>82) {$letter="B"; $image="images\image2.png";} ELSE IF ($score>79) {$letter="B-"; $image="images\image2.png";} ELSE IF ($score>76) {$letter="C+"; $image="images\image3.png";} ELSE IF ($score>72) {$letter="C"; $image="images\image3.png";} ELSE IF ($score>69) {$letter="C-"; $image="images\image3.png";} ELSE IF ($score>66) {$letter="D+"; $image="images\image4.png";} ELSE IF ($score>62) {$letter="D"; $image="images\image4.png";} ELSE IF ($score>59) {$letter="D-"; $image="images\image4.png";} ELSE {$letter="F"; $image="images\image5.png";} ECHO "Your Letter Grade Is: $letter <br/> $image"; ?>
Thanks again!

You still need to use an <img tag to display the image, as was stated.

Try making it this s you need to add an img tag to your code

[php]
ECHO "Your Letter Grade Is: $letter

";
[/php]

You should add an alt tag too.

Edit: I just saw Valkrider’s response ignore this reply. Sorry

Is this on a Windows OS? As in running on IIS?

What’s with the \ separators?

Alright this worked great the images are now displaying. Thanks
What is an alt tag thought and why do we include a period before and after the $image like above.

Windows yes but Im using MAMP. I couldnt get IIS running properly. We are creating html forms to input the data and having php code request the date and use it for whatever function. Idk if that answers your question Im still learning all of this.

You don’t need those periods You’re asking about. They are completely unnecessary.

Just Google alt image tag and it will explain it better than me putting it all here. You need to learn HTML and CSS as well as PHP. There are lots of good resources already on the web as well as loads of videos on YouTube.

Sponsor our Newsletter | Privacy Policy | Terms of Service