IF / ELSE在PHP中回显图像

时间:2012-12-08 14:13:26

标签: php if-statement

我试图根据IF / ELSE语句的结果回显一个特定的图像,但是我不能完全理解IF / ELSE语句的措辞。我是PHP的相对新手,所以我确信它在代码中只是一个小错误,但如果有人能提供任何帮助,我将不胜感激!

我目前正处于以下阶段:

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

谢谢!

5 个答案:

答案 0 :(得分:2)

你不能将if语句分配给一个值。

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

另一种更漂亮的方式是:

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

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

答案 1 :(得分:1)

是的,你的代码非常错误,但我可以看到你正在尝试做什么。

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

你的主要错误是括号的错误定位,以及IF语句不会在PHP中返回值的事实。

那就是说,我不确定你为什么要在下面做你的foreach循环,所以我没有触及那个;也许你可以进一步解释你想要实现的目标?

答案 2 :(得分:0)

这可能会帮助您朝着正确的方向前进:

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

答案 3 :(得分:0)

我认为这就是你想要的......

<?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:查看API并修复了一些事情。

ETAx2:对于那些想要从API中查看JSON返回示例的人...

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

答案 4 :(得分:0)

通过反复试验,我找到了以下解决方案。

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