views:

116

answers:

13

I have this bit of code which loops through an array and echos out the result to the page thus:

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

        echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . $row['name'] . '</a></td>' . '<td>' . $row['provider'] . '</td>' . '<td>' . $row['media'] . "</td></tr><br />\n";

        }

It works just fine, but I was hoping to use an 'if' statement on the $row['media'] because it contains some NULL and some !NULL results.

I wanted to be able to echo a different response a little like:

if ($row['media'] != NULL){ echo 'Nope'; } else { echo $row['media']; }

Is this possible in this situation?

Thanks.

A: 

Yeah, you would just end the echo, perform the if statement, and then use another echo to finish the code off. When it is parsed, the HTML will still be usable.

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

    echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . $row['name'] . '</a></td>' . '<td>' . $row['provider'] . '</td>' . '<td>';

    if($row['media'] == NULL) { echo 'Nope'; } else { echo $row['media']} 

echo "</td></tr><br />\n";

    }
Liam Spencer
+1  A: 

The best way to accomplish this is using ternary operators:

while (whatever)
{
    echo 'foo'
        .($statement ? 'bar' : '')
        .'baz';

}
eykanal
I'm sorry, but I have to disagree. This is not true in the general case, although it is practical for short expression type situations.
Daren Thomas
+1 for actual naming the operator
ZeissS
@DarenThomas: I don't know if I agree with you. I would state it the other way around; this is useful for most situations, but if applied to a too-complex case, it will make a ridiculous mess of your code. For most situations, this can work fine. I edited it to add some whitespace, though.
eykanal
A: 

Well, a very simple solution would be to do this...

$media = $row['media'];
if ($row['media'] == NULL)
    $media = 'nope';

echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' .$row['name']. '</a></td>';
echo '<td>' . $row['provider'] . '</td>' . '<td>' . $media . "</td></tr><br />\n";
rikh
A: 

Yes, break your echo into two different echos:

echo "<tr><td><a target="_blank" href="'" ; // (etc etc)

if($row['media'] != NULL) {
  echo "NOPE";
} else {
  echo $row['media'];
}

echo " $row['url'] . '">'; // (etc etc)

The syntax isn't perfect but I'm pretty sure you'll get the idea :)

Organiccat
+1  A: 

use:

if ( is_null( $row['media'] ) ) { ... } else { ... }
killer_PL
A: 

If I understand your question then this should work just fine:

if(is_null($row['media']) echo($row['media']) else echo('Nope');
Peter Hanneman
A: 

you can always just use another variable and set it before your echo statement, then use that variable in your echo statement. if you want a one-liner, you can use shorthand syntax like this:

($row['media'] != null) ? 'Nope' : $row['media']

and insert that where you currently just have $row['media']

Scott M.
A: 

Why not do it this way?

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

$media = ($row['media'] != NULL) ? $row['media'] : "Invalid";

echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . $row['name'] . '</a></td>' . '<td>' . $row['provider'] . '</td>' . '<td>' . $media . "</td></tr><br />\n";

}
Stephen
A: 

You can do something like this in your select statement

select

CASE
WHEN media IS NULL THEN 'Nope';
ELSE media
END as media

from

table
where......

read more : link text

Bandpay
+1  A: 
while($row = mysqli_fetch_array($result)) {
    echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . 
         $row['name'] . '</a></td>' . '<td>' . $row['provider'] . 
         '</td>' . '<td>' . 
         ($row['media'] == NULL ? 'Nope' : $ros['media'] . 
         "</td></tr><br />\n";

}
M42
A: 

Have the value put into a temp variable before the echo:

$mediaVal = $row['media'];

if ($mediaVal == NULL) $mediaVal = 'Nope';

echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . $row['name'] . '</a></td>' . '<td>' . $row['provider'] . '</td>' . '<td>' . $mediaVal . "</td></tr><br />\n";

You might consider stripping each field out into a dedicated variable incase you would life to process them in a similar manner etc

davbryn
A: 

Yes you can do that. You might want to break the echo into multiple echos so its a little easier to see whats going on.

Also, you should check over your if statement. Be careful with your conditionals.

if ($row['media'] != NULL) {
    echo 'Nope';
} else {
    echo $row['media'];
}

That will output 'Nope' if $row['media'] is not null. I assume you want to output 'Nope' if $row['media'] is null. In that case you want to use == instead of !=.

birderic
A: 

You can put it in one statement:

while ( $row = mysqli_fetch_array($result) ) {
    echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . $row['name'] . '</a></td>' .
         '<td>' . $row['provider'] . '</td>' .
         '<td>' . (is_null($row['media'])?"Invalid Value":$row['media']) . "</td></tr><br />\n";
}
JCD