0

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!

5 Answers 5

2

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" />';
2
  • Perfect, that works when I parse it into the foreach at the bottom, rather than trying to assign it to $fresh. I'll mark this answer as correct when stackoverflow lets me, in around 8 minutes. Thanks again!
    – Alex Ryans
    Commented Dec 8, 2012 at 14:20
  • It's worth noting in the "prettier" example you havent set the title and alt text correctly. ;)
    – Luke
    Commented Dec 8, 2012 at 14:55
1

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?

1
  • Sorry, I should have explained further. I'm attempting to create a dynamic movie database using a mixture of the Rotten Tomatoes and TMDb APIs, and the foreach specified in this post relates to the gathering of reviews from Rotten Tomatoes. As Rotten Tomatoes classifies movies as either 'Fresh' or 'Rotten' in the reviews, I'm attempting to return a corresponding image depending on which of the two classifications the review is. The foreach, therefore, cycles through the reviews, prints them out and assigns the corresponding image.
    – Alex Ryans
    Commented Dec 8, 2012 at 14:26
0

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>';
    }
}
?>
0

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"
        }
    },
...
0

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" />';
}
?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.