OldWest
OldWest

Reputation: 2385

Check value on all fields in while loop w/out using a separate if statement on each field

I am trying to find a non-redundant way to check if my returned fields are blank '' ..

For example, I am currently do this to check all fields:

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

if ($row['yr'] == '') {
    $row['yr'] = "Unavailable";
}

if ($row['work_cmt'] == '') {
    $row['work_cmt'] = "Unavailable";
}

I have about 20 fields I need to check and this just seems so redundant. I have not been able to find a php function that fits this and not sure exactly what the best approach is to this.

Upvotes: 0

Views: 179

Answers (2)

rikkit
rikkit

Reputation: 1137

Use a foreach loop.

while ($row = mysql_fetch_array($result)) {
foreach($row as $key->$value) {
if ($value == '') {
$value = 'unavailable';
}
}

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 400972

You could loop over the items with foreach, tetsing each one in turn :

foreach ($row as $key => $value) {
    if ($value == '') {
        $row[$key] = 'Unavailable';
    }
}

Upvotes: 3

Related Questions