如何比较条件

时间:2015-06-01 14:52:46

标签: php arrays

这是我的代码

<tr>
    <th width="100"><strong><?php echo $isian2; ?></strong></th>
    <th width="140"><strong><?php echo $isian3; ?></strong></th>
    <td width="10" align="center"><strong>Option</strong></td>
  </tr>

<?php

$pageSql = "SELECT * FROM tcar";
            $pageQry = mysql_query($pageSql, $db) or die ("error paging: ".mysql_error());
            $kolomLain = mysql_fetch_array($pageQry);

 $mySql = "SELECT * FROM tcustomer ";
        $myQry = mysql_query($mySql, $db) or die ("error paging: ".mysql_error());
        $kolomData = mysql_fetch_array($myQry);

    while ($kolomData = mysql_fetch_array($myQry)) {
     <tr>
        <td> <?php echo $kolomData['id']; ?> </td>
        <td> <?php echo $kolomData['kd']; ?> </td>
?>

<?php

    if ($kolomData['kd']==$kolomLain['kd']){
    echo "<td class='cc' align='center'> A </td>     
 ";
    }
    else {
    echo "  <td class='cc' align='center'> B </td> 
     ";
  }
?>

如果代码不起作用,它只与表tcar中的第一个字段'kd'进行比较。

我很抱歉,如果它已经被另一个人问过,但实际上我正在尝试搜索,但仍然无法获得结果。

2 个答案:

答案 0 :(得分:2)

将PHP代码段更改为

$sql1 = "SELECT * FROM tcar";
$res1 = mysql_query($sql1, $db) or die ("error paging: ".mysql_error());
$kolomLainSize = mysql_num_rows($res1);

$sql2 = "SELECT * FROM tcustomer ";
$res2 = mysql_query($sql2, $db) or die ("error paging: ".mysql_error());
$kolomDataSize = mysql_num_rows($res2);

for($i = 0; $i < min($kolomLainSize, $kolomDataSize); $i++)
{
    $kolomLain = mysql_fetch_array($res1);
    $kolomData = mysql_fetch_array($res2);
    // print your data here
}
if($i == $kolomLainSize)
{
    // no more $kolomLain, but there are still some $kolomData
    while($kolomData = mysql_fetch_array($res2))
    {
        // print your data
    }
}
else
{
    // no more $kolomData, but there are still some $kolomLain
    while($kolomLain = mysql_fetch_array($res1))
    {
        // print your data
    }
}

答案 1 :(得分:0)

尝试做两次,并将行的结果放在两个不同的值中。比较之后。