2012-12-08 31 views
-1

我想基於IF/ELSE語句的結果回顯特定圖像,但是我無法完全解決IF/ELSE語句的表述。我是PHP的相對新手,所以我確信這只是代碼中的一個小錯誤,但如果任何人都可以提供任何幫助,我會很感激!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>'; 
         } 
        ?> 

謝謝!

回答

2

你不能將語句賦值給一個值。

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

完美的,當我解析到底部的foreach,而不是試圖將它分配給$新鮮時,它的工作原理。我會在大約8分鐘內通過stackoverflow讓我回答這個問題。再次感謝! –

+0

值得注意的是,「漂亮」的例子中你沒有正確設置標題和替代文字。 ;) – Luke

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循環,所以我沒有觸及它;也許你可以進一步解釋你想要達到的目標?

+0

對不起,我應該進一步解釋。我試圖用Rotten Tomatoes和TMDb API的混合來創建一個動態的電影數據庫,而且這篇文章中提到的foreach與收集Rotten Tomatoes的評論有關。 由於腐爛的西紅柿將電影分類爲評論中的「新鮮」或「腐爛」,我試圖根據評論的兩個分類中的哪一個返回相應的圖像。 因此,該foreach循環遍歷評論,打印出來並分配相應的圖像。 –

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