douzhan5262 2012-10-17 15:00
浏览 66
已采纳

在if语句中使用由ms_sql查询生成的数组的第一个值

Ok, so basically I have a query that returns an array, and then it gets looped:

$result = mssql_query("SELECT * FROM Segments ORDER BY Squares");

    if (!$result) {
    echo 'query failed';
    exit;

              }

        while ($row = mssql_fetch_array($result)) {
    $txtsquares = $row["Squares"];
    echo $txtsquares;

When echoed the variable $txtsquares is equal to an array value, for example lets say 1 2 3 4 5 6 7 8.

I need this array/loop. But I would like to grab the first value of this array and use it in an if statement, like so:

value="<?php echo $txtsquares; ?>"
<?php if ($txtsquares == 1) { ?> checked="checked" <?php }
else{ ?> checked="" <?php } ?>/>

However obviously this is wrong, because the value will never equal 1 because its an array. Can anyone point me in the correct direction? I am new at PHP so sorry if it is an easy question, I have Googled it but without much luck.

  • 写回答

2条回答 默认 最新

  • doutao1282 2012-10-17 15:22
    关注

    If you need to use the first element of $txtsquares try this:

    value="<?php echo $txtsquares; ?>"
    <?php if ($txtsquares[0] == 1) { ?> checked="checked" <?php }
    else{ ?> checked="" <?php } ?>/>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata