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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!