PHP检查一个值是否等于另一个结果集中的任何值?

时间:2017-02-20 14:13:20

标签: php sql-server

我有一个在搜索结果中打印出行的while循环,搜索结果中的一列是“Area Name”。 现在我想检查该区域名称是否也在另一个结果集中,如果是,则使产品可以点击。

这是我搜索结果的while循环:

<table id="" class="table table-striped table-bordered display" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>Customer Code</th>
        <th>Customer Name</th>
        <th>Customer Type</th>
        <th>Name</th>
        <th>Postcode</th>
    </tr>
</thead>
<tbody>
    <?php while($customer = sqlsrv_fetch_array($querySearchResults, SQLSRV_FETCH_ASSOC)) : ?>
        <tr>
            <td><?php echo $customer['CardCode'] ?></td>
            <td>
                //If $customer['Name'] = to any of the rows in other query 
                    //This is Clickable
                    <?php echo $customer['CardName']; ?>
                //Else its not.
            </td>
            <td><?php echo $customer['CardType'] ?></td>
            <td><?php echo $customer['Name'] ?></td>
            <td><?php echo $customer['ZipCode'] ?></td>
        </tr>
    <?php endwhile; ?> 
</tbody>
</table>

这是另一个查询:

$userID = $user['loggedInUserId'];
$queryName = "
    SELECT UserID, Name
    FROM Table
    WHERE UserID = '$userID'";
$getName = sqlsrv_query($sapconn2, $queryName);
$Name = sqlsrv_fetch_array($getName, SQLSRV_FETCH_ASSOC);

在这种情况下,if语句需要什么?

1 个答案:

答案 0 :(得分:1)

假设您的示例代码解释了实际情况,您的other Query似乎只检索了一行。因此,在您的主代码中,再次假设您在运行之前已经完成了other Query,只需要执行

<?php while($customer = sqlsrv_fetch_array($querySearchResults, SQLSRV_FETCH_ASSOC)) : ?>
    <tr>
        <td><?php echo $customer['CardCode'] ?></td>
        <td>
<?php
if ($customer['Name'] == $Name['name']) {
    //This is Clickable
    //echo $customer['CardName'];
}
?>

        </td>
        <td><?php echo $customer['CardType'] ?></td>
        <td><?php echo $customer['Name'] ?></td>
        <td><?php echo $customer['ZipCode'] ?></td>
    </tr>
<?php endwhile; ?> 

至于使其可点击,我们需要了解更多关于您想要去的网址以及您希望在锚标记中发送给它的参数,例如

这一行

<?php echo $customer['CardName']; ?>

可能像

<?php echo "<a href='folder/file.php?param=something'>{$customer['CardName']} </a>" ?>