简体   繁体   中英

IF/ELSE to echo image in PHP

I am trying to echo a specific image based on the results of a IF/ELSE statement, however I can't quite work out the phrasing of the IF/ELSE statement. I'm a relative newbie to PHP, so I'm sure that it's just a little error in the code somewhere, but if anyone could offer any assistance, I'd be grateful!

I'm currently at the stage below:

<?php
     $fresh = if ($reviews['reviews']['freshness']) = 'fresh' {
            echo '<img src="assets/images/fresh.png" class="rating" title="Fresh" alt="Fresh" />';
        } else {
            echo '<img src="assets/images/rotten.png" class="rating" title="Rotten" alt="Rotten" />';
        }
?>

<?php
                        foreach($reviews['reviews'] as $rv){
                            if ($tmp++ < 10);
                            echo $fresh;
                            echo '<li>' . $rv['quote'] . '</li>';
                        }
                    ?>

Thank you!

you cant assign if statement to a value.

if ($reviews['reviews']['freshness'] == 'fresh') {
            echo '<img src="assets/images/fresh.png" class="rating" title="Fresh" alt="Fresh"/>';
        } else {
            echo '<img src="assets/images/rotten.png" class="rating" title="Rotten" alt="Rotten" />';
    }

another prettier way would be:

if ($reviews['reviews']['freshness'] == 'fresh') {
   $image = "fresh";
}
else {
    $image = "rotten";
}

echo '<img src="assets/images/' . $image . '.png" class="rating" title="Rotten" alt="Rotten" />';

Yup, your code is pretty wrong, but I can see what you're trying to do.

<?php
if ($reviews['reviews']['freshness'] == 'fresh') {
    $image = '<img src="assets/images/fresh.png" class="rating" title="Fresh" alt="Fresh" />';
} else {
    $image = '<img src="assets/images/rotten.png" class="rating" title="Rotten" alt="Rotten" />';
}
?>

Your main mistake there is the incorrect positioning of brackets, and the fact that the IF statement does NOT return a value in PHP.

That said, I'm not sure why you're doing your foreach loop underneath, so I've not touched that; perhaps you could explain further what you're trying to achieve?

This might help you in the right direction:

<?php
if ($reviews['reviews']['freshness'] == 'fresh'){
    echo '<img src="assets/images/fresh.png" class="rating" title="Fresh" alt="Fresh" />';
}
else{
    echo '<img src="assets/images/rotten.png" class="rating" title="Rotten" alt="Rotten" />';
}

while($reviews['reviews']){
    for($i=0;i<10;i++{
        echo // What do you actually want to print out?
        echo '<li>'.$reviews['reviews']['quote'].'</li>';
    }
}
?>

I think this is what you're wanting...

<?php
    for ($tmp = 0; $tmp < 10 && $tmp < count($reviews); $tmp++) {
        if ($reviews[$tmp]['freshness'] == 'fresh') {
            echo '<img src="assets/images/fresh.png" class="rating" title="Fresh" alt="Fresh" />';
        } else {
             echo '<img src="assets/images/rotten.png" class="rating" title="Rotten" alt="Rotten" />';
        }
        echo '<li>' . $reviews[$tmp]['quote'] . '</li>';
    }
 ?>

ETA: Looked at the API and fixed a couple things.

ETAx2: For those wanting to see an example of the JSON return from the API...

{
"total": 41,
"reviews": [
    {
        "critic": "Joe Baltake",
        "date": "2010-07-27",
        "freshness": "fresh",
        "publication": "Passionate Moviegoer",
        "quote": "'Toy Story 3': Alternately affecting, hilarious and heartbreaking and the most original prison-escape movie ever made",
        "links": {
            "review": "http://thepassionatemoviegoer.blogspot.com/2010/07/perfectimperfect.html"
        }
    },
    {
        "critic": "Rafer Guzman",
        "date": "2010-07-06",
        "freshness": "fresh",
        "publication": "Newsday",
        "quote": "It's sadder and scarier than its predecessors, but it also may be the most important chapter in the tale.",
        "links": {
            "review": "http://www.newsday.com/entertainment/movies/toy-story-3-andy-grows-up-1.2028598"
        }
    },
    {
        "critic": "Richard Roeper",
        "date": "2010-06-30",
        "original_score": "5/5",
        "freshness": "fresh",
        "publication": "Richard Roeper.com",
        "quote": "The best movie of the year so far.",
        "links": {
            "review": "http://www.richardroeper.com/reviews/toystory3.aspx"
        }
    },
...

Through trial and error, I found the following solution.

<?php
  $ID=$row_RecordsetLast['ID'];

$image = '../../pics/'.$ID.'.jpg';


if (file_exists($image)) {
    echo '<img src="../../pics/' . $ID . '.jpg" alt="" width="110" height="161" />';
} else {
    echo '<img src="guest.png" alt="" width="110" height="161" />';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM