如果是其他逻辑取决于几行

时间:2017-02-22 10:03:49

标签: php

在我的网站上,我有使用以下代码生成行的页面:

<?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'而不是echo text&#34; nine&#34;,如果有一行且$row['type']!=='9'回音文字&#34;不是九&#34;,但是如果有多行,并且至少有一行$row['type']=='9'回显文字&#34;两者都是&#34;否则回声&#34;非他们&#34;

我无法弄清楚如何做到这一点。你能帮我一点吗?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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';
} 
相关问题