0

我正在尝试根据 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>';
                        }
                    ?>

谢谢!

4

5 回答 5

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" />';
于 2012-12-08T14:15:18.457 回答
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 循环,所以我没有触及它;也许您可以进一步解释您要达到的目标?

于 2012-12-08T14:19:46.450 回答
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>';
    }
}
?>
于 2012-12-08T14:21:38.787 回答
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"
        }
    },
...
于 2012-12-08T14:31:17.757 回答
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" />';
}
?>
于 2016-06-20T17:53:25.197 回答