Click to See Complete Forum and Search --> : Display record if record exists.


NickToye
08-20-2006, 06:44 AM
Does anyone know how I would display a record if that record exists.

For example if have this code:


<li><em>My Website: </em><a href="<? echo($row_artists['artist_site']);?>"><? echo($row_artists['artist_site']); ?></a></li>


But if this particular record does not have any data in the field - artist_site, then I don't want it to be displayed.

chazzy
08-20-2006, 07:38 AM
<?php
$site = $row_artists['artist_site'];
($site == null) ? ?> <li><em>My Website: </em><a href="<? echo($row_artists['artist_site']);?>"><? echo($row_artists['artist_site']); ?></a></li> <?php : echo "" ?>
This should do it pretty much. you might want to drop the interwoven start/stop tags just to make it cleaner looking.

NickToye
08-20-2006, 09:38 AM
I was looking for something using if statements.

chazzy
08-20-2006, 09:47 AM
technically that's a shortened statement.


$site = $row_artists['artist_site'];
if($site != null){
//print the statement
}
else{
//don't print it
}

NickToye
08-20-2006, 09:59 AM
So what am I doing wrong here?


<? $site = $row_artists['artist_site'];
if($site == null){

}
else{
<li><a href=" echo($row_artists['artist_email'])">link</a></li>
} ?>

NickToye
08-20-2006, 10:02 AM
Actually it now works:


<? $site = $row_artists['artist_site'];
if($site == null){ ?>
<?
}
else{ ?>
<li><a href="($row_artists['artist_email'])">link</a></li>
<? } ?>

chazzy
08-20-2006, 11:37 AM
that's what I was trying to point out to you.

the proper coding should look like this:


<? $site = $row_artists['artist_site'];
if($site == null){

}
else{
echo "<li><a href=\"".$row_artists['artist_email']."\">link</a></li>";
} ?>

pcthug
08-20-2006, 04:10 PM
Why use an uneeded else statement?

<? $site = $row_artists['artist_site'];
if($site != null){
echo "<li><a href=\"".$row_artists['artist_email']."\">link</a></li>";
} ?>

bokeh
08-20-2006, 04:56 PM
Personally I'd do it like this. <?php

if($row_artists['artist_site'])
{
echo '<li><a href="'.$row_artists['artist_email'].'">link</a></li>';
}

?>I don't really understand why you would use null to check for an empty string. Or the need for variable swapping.

NickToye
08-20-2006, 05:08 PM
Excellent, that was what I trying to do, I knew it was that simple.

Cheers.

pcthug
08-21-2006, 03:14 AM
Personally I'd do it like this. <?php

if($row_artists['artist_site'])
{
echo '<li><a href="'.$row_artists['artist_email'].'">link</a></li>';
}

?>I don't really understand why you would use null to check for an empty string. Or the need for variable swapping.
Flaws exist in this example, if the $row_artists['artist_site'] array variable is a string containing a single, numeric zero (0), output will not occur as the string will return false.

bokeh
08-21-2006, 03:27 AM
Flaws exist in this example, if the $row_artists['artist_site'] array variable is a string containing a single, numeric zero (0), output will not occur as the string will return false.if($site != null){There is no difference in the functionality of your code and mine. Yours also returns false if the variable is a zero. if(null != 0){ //false If you want to check for an empty string properly use the identicality operator and an empty string as the comparator.
if('' !== $var)

pcthug
08-21-2006, 05:27 AM
I never said that my if statement wouldn't return false on the encounter of a literal zero.
My script was simply a more efficient interpretation of chazzy's previously posted solution.

Following the original concept where we check the variable's type not value

<?php

if(!is_null($row_artists['artist_site']))
{
echo '<li><a href="'.$row_artists['artist_email'].'">link</a></li>';
}

?>

bokeh
08-21-2006, 06:12 AM
I never said that my if statement wouldn't return false on the encounter of a literal zero.
My script was simply a more efficient interpretation of chazzy's previously posted solution.

Following the original concept where we check the variable's type not value

<?php

if(!is_null($row_artists['artist_site']))
{
echo '<li><a href="'.$row_artists['artist_email'].'">link</a></li>';
}

?>
Your example makes the assumtion that the returned value is null if the field is empty but it most likely will be an empty string in which case your code won't work. My example covered the widest range of possibilities and I very much doubt that a field named artist_email would contain a sole string zero unless it were an error or used to represent negation. So, to sum up, I would say my example is good in this instance as it checks for all negative possibilities, i.e. null or empty string and since we don't know which but do know null, empty strng and zero cannot be an artists_email the example is good.