dongliao1949 2017-02-20 14:13
浏览 86
已采纳

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

I have a while loop which is printing out rows from search results, one column in the search result is 'Area Name'. Now I want to check if that area name is also in another result set, and if it is, make the product clickable.

This is my while loop for search results:

<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>

This is the other Query:

$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);

What would the if statement need to be in this case?

  • 写回答

1条回答 默认 最新

  • donglu7286 2017-02-20 14:25
    关注

    Assuming your sample code explains the real situation, your other Query only seems to retrieve one row. So in your main code, again assuming you have completed the other Query before that is run just needs to do

    <?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; ?> 
    

    As to making it clickable, we would need to know more about the url you want to go to and the parameters you want to send to it in an anchor tag, but for example

    This line

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

    Could be something like

    <?php echo "<a href='folder/file.php?param=something'>{$customer['CardName']} </a>" ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来