michaelmcgurk
michaelmcgurk

Reputation: 6509

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.

Upvotes: 0

Views: 549

Answers (3)

JCOC611
JCOC611

Reputation: 19709

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;
}

Upvotes: 0

PEdroArthur
PEdroArthur

Reputation: 884

inside your while loop you should put something like

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

before echoing it.

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 400932

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';
    }
}

Upvotes: 2

Related Questions