2017-02-22 39 views
0

我的網站上else邏輯我哪裏行是使用下面的代碼生成的頁面:如果取決於幾個行

<?php 
while($row=mysql_fetch_assoc($sql)) { 
    if($row['type']=='1'){ $tipo="one";} 
    if($row['type']=='2'){ $tipo="two";} 
    if($row['type']=='3'){ $tipo="three";} 
    if($row['type']=='4'){ $tipo="four";} 
    if($row['type']=='5'){ $tipo="five";} 
    if($row['type']=='6'){ $tipo="six";} 
    if($row['type']=='7'){ $tipo="seven";} 
    if($row['type']=='8'){ $tipo="eight";} 
    if($row['type']=='9'){ $tipo="nine";} 
    if($row['type']=='10'){ $tipo="ten";} 
?> 
<tbody> 
    <tr> 
     <td> 
      <?php 
    if($row['type']=='9') { 
     echo 'Text One'; 
    } 
    else { 
     echo 'Text two'; 
    } ?> 
     </td> 
    </tr> 
</tbody> 
<?php 
} 
?> 

我需要一個代碼,這將做下一件事之後:

如果如果有一行並且$row['type']!=='9'回顯文本「不是九」,但是如果有多行並且對於其中至少有一行$row['type']=='9' echo文本「both」else echo「非他們「

我無法弄清楚如何做到這一點。你能幫我一下嗎?您要

嘗試這樣的事情是什麼

+1

你在你的問題中有你的答案......你的解釋只是需要在PHP中重寫。使用僞代碼(https://en.wikipedia.org/wiki/Pseudocode)可以幫助你正式確定你想要的以及如何對它進行編碼。 如果您仍然爲此而煩惱,請添加您的代碼,我們將能夠爲您提供幫助。 – NaeiKinDus

回答

1

使用標誌變量:

<?php 

    $row_cnt=0; 
    $type_9=false; 

    while($row=mysql_fetch_assoc($sql)) { 

    if($row['type']=='1'){ $tipo="one";} 
    if($row['type']=='2'){ $tipo="two";} 
    if($row['type']=='3'){ $tipo="three";} 
    if($row['type']=='4'){ $tipo="four";} 
    if($row['type']=='5'){ $tipo="five";} 
    if($row['type']=='6'){ $tipo="six";} 
    if($row['type']=='7'){ $tipo="seven";} 
    if($row['type']=='8'){ $tipo="eight";} 
    if($row['type']=='9'){ $tipo="nine";} 
    if($row['type']=='10'){ $tipo="ten";} 
    $rowcnt++; 
    ?> 



    <?php 
    if($row['type']=='9') {  
     $type_9=true; 
    } 
    ?> 



    <?php 
    } 
    ?> 
    <tbody><tr><td> 
    <?php 
    if($row_cnt == 1) 
    { 
     if($type_9) 
     echo "nine"; 
     else 
     echo "not nine"; 
    } 
    else if($rowcnt > 1) 
    { 
     if($type_9) 
     echo "both"; 
     else 
     echo "non of them"; 
    } 

    ?> 
    </td></tr></tbody> 
+0

謝謝!它讓我想到了正確的方向:) –

0

我省略了HTML標籤,但我認爲你可以很容易地添加這些。

$tipo = array(); 
$multiple = false; 
$present = false; 
$numbers = array ( 
      '1' => "one", 
      '2' => "two", 
      '3' => "three", 
      '4' => "four", 
      '5' => "five", 
      '6' => "six", 
      '7' => "seven", 
      '8' => "eight", 
      '9' => "nine", 
      '10' => "ten", 
     ); 

while ($row = mysql_fetch_assoc($sql)) 
     $tipo[] = $numbers[ $row['type'] ]; 

$multiple = (count($tipo) > 1) ? true : false; 
$present = (in_array('9', $tipo)) ? true : false; 

if($multiple) { 
    if ($present) 
     echo 'both'; 
    else 
     echo 'none of them'; 
} else { 
    if ($present) 
     echo 'nine'; 
    else 
     echo 'not nine'; 
}