user3749630
user3749630

Reputation: 13

else condition is never reached

When I create three conditions inside while, but only two are visible.

<?php 
 $ga= mysql_query("Select * from galeri");
 while ($v=mysql_fetch_object($ga)) { 

 if ($v->video==NULL){ ?> 
 <a href="images/galeri/<?php echo $v->foto?>" data-caption="<?php echo $v->judul?> - <?php echo tgl($v->tgl) ?>" data-fit="cover"  data-thumb = "images/galeri/<?php echo $v->foto?>"></a>
 <?php  } 

 else if ($v->foto==NULL AND $v->linkfoto==NULL) { ?>
 <a href="<?php echo $v->video ?>"  data-fit="cover" data-caption="<?php echo $v->judul?> - <?php echo tgl($v->tgl) ?>"  ><?php echo $v->judul?> - <?php echo tgl($v->tgl) ?></a>

 <?php  } else  { ?>
 <img src="<?php echo $v->linkfoto?>" data-caption="<?php echo $v->judul?> - <?php echo tgl($v->tgl) ?>" data-fit="cover"   data-thumb = "<?php echo $v->linkfoto?>">
<?php } }?>

galeri Table

You can see the field "linkfoto" contains 1 in the image above

Why conditions three else no appear?

Upvotes: 1

Views: 95

Answers (3)

kero
kero

Reputation: 10658

In the row video is empty as well, so the first check $v->video==NULL will probably be true and the else will never be reached.

To resolve this I suggest using if to check if a value is set on the variable you wish to use - not check if all other variables aren't set.

if ($v->foto != NULL) {
    printf('<a href="images/galeri/%s" data-caption="%s - %s" data-fit="cover"  data-thumb = "images/galeri/%s"></a>',
        $v->foto, $v->judul, tgl($v->tgl), $v->foto);
} elseif ($v->video != NULL) {
    //etc
} else {

}

This has the advantage that if you wish to edit your code in the future, you know which variables you can use inside the if - because you checked that they are set!

Upvotes: 2

sjagr
sjagr

Reputation: 16512

Ignoring other basic formatting structures or other potential problems with the code, the line that says

else if ($v->foto=NULL && $v->linkfoto==NULL) { ?>

needs a second = sign. You're assigning NULL to $v->foto by using a single equal sign.

Correction should be:

else if ($v->foto == NULL && $v->linkfoto == NULL) { ?>

Upvotes: 3

Rick D.
Rick D.

Reputation: 130

You are doing an assignment in the else if condition, I'm guessing it should be a comparison, do:

$v->foto == NULL

instead of:

$v->foto = NULL

Upvotes: 0

Related Questions