/    Sign up×
Community /Pin to ProfileBookmark

Display record if record exists.

Does anyone know how I would display a record if that record exists.

For example if have this code:

[code=php]
<li><em>My Website: </em><a href=”<? echo($row_artists[‘artist_site’]);?>”><? echo($row_artists[‘artist_site’]); ?></a></li>
[/code]

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

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@chazzyAug 20.2006 — [code=php]<?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 "" ?>[/code]

This should do it pretty much. you might want to drop the interwoven start/stop tags just to make it cleaner looking.
Copy linkTweet thisAlerts:
@NickToyeauthorAug 20.2006 — I was looking for something using if statements.
Copy linkTweet thisAlerts:
@chazzyAug 20.2006 — technically that's a shortened statement.

[code=php]
$site = $row_artists['artist_site'];
if($site != null){
//print the statement
}
else{
//don't print it
}
[/code]
Copy linkTweet thisAlerts:
@NickToyeauthorAug 20.2006 — So what am I doing wrong here?

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

}
else{
<li><a href=" echo($row_artists['artist_email'])">link</a></li>
} ?>
[/code]
Copy linkTweet thisAlerts:
@NickToyeauthorAug 20.2006 — Actually it now works:

[code=php]
<? $site = $row_artists['artist_site'];
if($site == null){ ?>
<?
}
else{ ?>
<li><a href="($row_artists['artist_email'])">link</a></li>
<? } ?>
[/code]
Copy linkTweet thisAlerts:
@chazzyAug 20.2006 — that's what I was trying to point out to you.

the proper coding should look like this:

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

}
else{
echo "<li><a href="".$row_artists['artist_email']."">link</a></li>";
} ?> [/code]
Copy linkTweet thisAlerts:
@pcthugAug 20.2006 — Why use an uneeded else statement?
[code=php]
<? $site = $row_artists['artist_site'];
if($site != null){
echo "<li><a href="".$row_artists['artist_email']."">link</a></li>";
} ?>[/code]
Copy linkTweet thisAlerts:
@bokehAug 20.2006 — Personally I'd do it like this. [code=php]<?php

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

?>[/code]
I don't really understand why you would use [I]null[/I] to check for an empty string. Or the need for variable swapping.
Copy linkTweet thisAlerts:
@NickToyeauthorAug 20.2006 — Excellent, that was what I trying to do, I knew it was that simple.

Cheers.
Copy linkTweet thisAlerts:
@pcthugAug 21.2006 — Personally I'd do it like this. [code=php]<?php

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

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

Flaws exist in this example, if the [I]$row_artists['artist_site'][/I] array variable is a string containing a single, numeric zero (0), output will not occur as the string will return false.
Copy linkTweet thisAlerts:
@bokehAug 21.2006 — Flaws exist in this example, if the [I]$row_artists['artist_site'][/I] array variable is a string containing a single, numeric zero (0), output will not occur as the string will return false.[/QUOTE][code=php]if($site != null){[/code][/QUOTE]There is no difference in the functionality of your code and mine. Yours also returns false if the variable is a zero. [code=php]if(null != 0){ //false[/code] If you want to check for an empty string properly use the identicality operator and an empty string as the comparator.
[code=php]if('' !== $var) [/code]
Copy linkTweet thisAlerts:
@pcthugAug 21.2006 — [list=1]
  • [*]I never said that my [I]if[/I] 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.

  • [/list]

    Following the original concept where we check the variable's type not value
    [code=php]
    <?php

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

    ?>[/code]
    Copy linkTweet thisAlerts:
    @bokehAug 21.2006 — [list=1]
  • [*]I never said that my [I]if[/I] 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.

  • [/list]

    Following the original concept where we check the variable's type not value
    [code=php]
    <?php

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

    ?>[/code]
    [/QUOTE]

    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 [I]artist_email[/I] 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 [I]artists_email[/I] the example is good.
    ×

    Success!

    Help @NickToye spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.4,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...