简体   繁体   English

While循环-根据ID值中断循环

[英]While loop - break loop based on ID value

I'd like to loop through and output all the records in my table but if the $id were to equal 4 or 6 I need it to append a string to the ID value. 我想遍历并输出表中的所有记录,但是如果$ id等于46 ,则需要在ID值后附加一个字符串。

My current code: 我当前的代码:

$sql = "SELECT * FROM publications";
$sqlres = mysql_query($sql);

while ($row = mysql_fetch_array($sqlres)) {

$id = $row['id'];

echo $id;

}

How do I achieve this? 我该如何实现?

Many thanks for any pointers. 非常感谢您的指导。

why not just test for the value of $id , and echo some additional information if that value is 4 or 6 : 为什么不仅仅测试$id的值,如果该值是46 ,则回显一些其他信息:

while ($row = mysql_fetch_array($sqlres)) {
    $id = $row['id'];
    echo $id;
    if ($id == 4 || $id == 6) {
        echo 'something more';
    }
}

You can test for the values and echo something more like P.Martin said, or you can append it to the variable: 您可以测试值并回显类似P.Martin所说的内容,也可以将其附加到变量中:

while ($row = mysql_fetch_array($sqlres)) {
  $id = $row['id'];
  $id = ($id==4 || $id==6)? $id."string": $id;
  echo $id;
}

inside your while loop you should put something like 在您的while循环中,您应该放置类似

if ($id == 4 || $id == 6)
   $id = $id . $myString;

before echoing it. 在回声之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM