简体   繁体   中英

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 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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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