开发者

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.

My curre开发者_StackOverflow中文版nt 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 :

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:

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

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

before echoing it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜