2017-06-26 55 views
-1

我有一个关于if语句的php相关问题。我有这段代码:PHP - 如果值等于

<?php if(count($targetValue) > 0): ?> 
    <?php foreach($targetValue as $key=>$val): ?> 
     <div class="<?php echo $key ?>"> 
      <span> 
       <?php echo $val ?> 
      </span> 
     </div> 
     <br> 
    <?php endforeach; ?> 
<?php endif;?> 

$ val可以返回多达10个预定义的不同值。我需要知道如何检查一个值是否等于一个单词,然后显示出来。

例如:如果$ VAL ==“平达”则显示X.之后,如果$ VAL ==“Mosterd”则显示Y.

对于每一个选项,$ VAL返回我想表现出不同的图片。

问候

UPDATE:

我有现在这样的代码:

<?php $targetValue = explode(",", $_product->getResource()->getAttribute('nf_allergie')->getFrontend()->getValue($_product)); ?> 

      <?php if(count($targetValue) > 1): ?> 
       <?php foreach($targetValue as $key=>$val): ?> 
        <div class="<?php echo $key ?>"> 
         <span> 
          <?php 
           switch($val){ 
            CASE "Ei": 
             echo 'X'; 
             break; 
            CASE "Gluten": 
             echo 'X'; 
             break; 
            CASE "Lupine": 
             echo 'X'; 
             break; 
            CASE "Melk, inclusief lactose": 
             echo 'X'; 
             break; 
            CASE "Mosterd": 
             echo 'X'; 
             break; 
            CASE "Noten": 
             echo 'X'; 
             break; 
            CASE "Pinda": 
             echo 'X'; 
             break; 
            CASE "Schaaldieren": 
             echo 'X'; 
             break; 
            CASE "Selderij": 
             echo 'X'; 
             break; 
            CASE "Sesamzaad": 
             echo 'X'; 
             break; 
            CASE "Soja": 
             echo 'X'; 
             break; 
            CASE "Sulfiet": 
             echo 'X'; 
             break; 
            CASE "Vis": 
             echo 'X'; 
             break; 
            CASE "Weekdieren": 
             echo 'X'; 
             break; 
            default: 
             echo "Leeg"; 
           } 
          ?> 
         </span> 
        </div> 
        <br> 
       <?php endforeach; ?> 
      <?php endif;?> 

但只有第一个结果将与X返回,看起来像在foreach并不好工作?

这是它返回: The ones with Name: in front of it are the actual results to check if it works

只是那些名称:在它前面的是实际的结果,以检查是否正常工作

+0

什么是你的问题? http://stackoverflow.com/help/how-to-ask – Twinfriends

+0

我不能得到它的工作:) – n00bly

回答

0

你可以使用一个三元操作

<?php if(count($targetValue) > 0): ?> 
    <?php foreach($targetValue as $key=>$val): ?> 
     <div class="<?php echo $key ?>"> 
      <span> 
       <?php echo (($val== 'pinda') ? 'X' : $val) ;?> 
      </span> 
     </div> 
     <br> 
    <?php endforeach; ?> 
    <?php endif;?> 

如果你有更多的价值,你可以使用开关

<?php if(count($targetValue) > 0): ?> 
    <?php foreach($targetValue as $key=>$val): ?> 
     <div class="<?php echo $key ?>"> 
      <span> 
       switch($val){ 
        CASE 'Pinda': 
        echo 'X'; 
        break; 
        CASE 'Mosterd': 
        echo 'Y'; 
        break;     
        CASE 'Salad': 
        echo 'Z'; 
        break; 
        DEFAULT: 
        echo $val; 
        break 
       } 
       <?php echo (($val== 'pinda') ? 'X' : $val) ;?> 
      </span> 
     </div> 
     <br> 
    <?php endforeach; ?> 
    <?php endif;?> 
+0

谢谢,我该怎么做所有的返回值?例如,$ val返回以下值:Pinda,Mosterd,沙拉,面包。然后,我想要展示Pinda图像,Mosterd图像,沙拉图像和面包图像。我需要做多个foreach,还是可以在一个? – n00bly

+0

答案已更新为更多的价值以及如果我的回答是正确的,请将其标记为已接受...看看这里如何 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – scaisEdge

+0

我根据你的回答更新了我的问题 – n00bly

1

你可以这样做:

$possibleValues = array(

    1 => "Mosterd", 
    2 => "Pinda", 
    3 => "Cookie" 

); 

//add more items to the array as you please 

foreach($targetValue as $key => $val) { 

    //return the value of the value associated with the key 

    echo $possibleValues[$key]; 

}