【问题标题】:Changing a while loop so echo doesn't print more than once更改 while 循环,使 echo 不会多次打印
【发布时间】:2014-09-08 18:54:30
【问题描述】:

我有一些代码可以在 while 循环中回显一行文本。但我不希望它作为一个循环,并且如果没有 while() 似乎无法让它工作。我的代码如下 - 我怎样才能让回声只打印一次?

while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
  do($row['InGarage'] == $comp and $row['BeingServiced'] == $unco and $row['ReadyForCollection'] == $unco) {
    echo "Vehicle is in Garage<br />";
}
  do($row['InGarage'] == $comp and $row['BeingServiced'] == $comp and $row['ReadyForCollection'] == $unco) {
    echo "Vehicle is being serviced<br />";
}
  do($row['InGarage'] == $comp and $row['BeingServiced'] == $comp and $row['ReadyForCollection'] == $comp) {
    echo "Vehicle is ready for collection<br />";
}
}

更新:使用 if

while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {   
}

if($row['InGarage'] == $comp and $row['BeingServiced'] == $unco and $row['ReadyForCollection'] == $unco) {
    echo "Vehicle is in Garage";
} 
  if($row['InGarage'] == $comp and $row['BeingServiced'] == $comp and $row['ReadyForCollection'] == $unco) {
    echo "Vehicle is being serviced";
}
  if($row['InGarage'] == $comp and $row['BeingServiced'] == $comp and $row['ReadyForCollection'] == $comp) {
    echo "Vehicle is ready for collection";
}

【问题讨论】:

  • 除了非常粗鲁之外,Marc 是对的。这可以通过doing and if语句来实现。
  • 当我使用 if 语句时,什么都不会打印,而与循环一样,它会打印很多次......我将把我的新代码放在问题的正文中
  • When I use an if statement, nothing prints ;因为您将所有内容都放在循环体之外:)
  • 在您更新的代码中,您甚至在到达 if() 语句之前就关闭了循环。
  • @the_pete 我不想让它循环播放,这是我的问题

标签: php


【解决方案1】:

do 运算符不是必需的,但需要 while 来遍历数据库中的所有结果。与其使用do,不如使用if 更明智。您还可以重构条件以创建稍短的条件:

while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
    if ($row['InGarage'] == $comp) {
        if ($row['BeingServiced'] == $comp) {
            if ($row['ReadyForCollection'] == $unco) {
                echo "Vehicle is being serviced<br />";
            }
            elseif ($row['ReadyForCollection'] == $comp) {
                echo "Vehicle is ready for collection<br />";
            }
        }
        elseif ($row['BeingServiced'] == $unco and $row['ReadyForCollection'] == $unco) {
            echo "Vehicle is in Garage<br />";
        }
    }
}

重构您的条件将使您的脚本不必进行额外的比较。在您的原始脚本中,您每次都测试$row['InGarage'] == $comp

if($row['InGarage'] == $comp and ... ) {
    echo "Vehicle is in Garage";
}
elseif($row['InGarage'] == $comp and ... ) {
    echo "Vehicle is being serviced";
}
elseif($row['InGarage'] == $comp and ... ) {
    echo "Vehicle is ready for collection";
}

先测试一次,再测试其他条件,效率更高:

if($row['InGarage'] == $comp) {
    if ($row['BeingServiced'] == $unco and $row['ReadyForCollection'] == $unco) {
        (etc.)
    }
}

【讨论】:

  • 我更喜欢嵌套,因为它使代码更容易和更清晰(对我来说)阅读。如果您喜欢排长队,请选择另一种方法。此外,原始代码在每个if 上测试第一个条件$row['InGarage'] == $comp。它只需要测试一次。
  • 它仍然没有回声...我设置了一个回声以确保已建立连接
  • @ialarmedalien 如何在找不到 SQLSRV 行时添加另一个 if 语句来回显另一行?我试过了,但没有成功
  • 您可以将整个内容嵌套在一个“if-else”循环中——if($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) { ...code to iterate through rows... } else { echo "No data retrieved from the database!"; }。您是否在此代码之前进行任何错误检查以确保连接正常?
  • 是的,当建立连接时,我有一个回声要打印。现在可以正常使用了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 2022-11-02
  • 1970-01-01
  • 2021-11-24
相关资源
最近更新 更多