Do not echo if field is blank

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DAVID87
Forum Commoner
Posts: 29
Joined: Fri Jun 26, 2009 9:56 am
Location: Nottingham, UK

Do not echo if field is blank

Post by DAVID87 »

Hi all,

just wondered if someone could help get my code working.

I have a database set up of fixtures and results for a local club.

On the php page the fixtures list is populated from this table in the database with alternating background colours. This works great. however we would like to have the links for the results automatically added when we upload them to the database.

I have the below code that sets out the alternating colours and also puts the link on the text, howeve, this adds links to all the fixtures but if there is no link specified in the database then it loops back to the page.

How can I make it so that there is an if statement inside the echo, so that the <a> is only applied to this if results field is not blank.

Here is my current code:

***** Please wrap code in

Code: Select all

tags. *****

Code: Select all

       <?php
        while($rows=mysql_fetch_array($result)){
        if($color==1){
       echo "<tr class='rowA'>
         <td>&nbsp;</td>
         <td>$rows[day]</td>
         <td>$rows[date]</td>
         <td><a href='$rows[results]' target='_blank'>$rows[fixture]</a></td>
         <td>$rows[venue]</td>
         </tr>";
        $color="2";
        } else {
       echo "<tr class='rowB'>
         <td>&nbsp;</td>
         <td>$rows[day]</td>
         <td>$rows[date]</td>
         <td><a href='$rows[results]' target='_blank'>$rows[fixture]</a></td>
         <td>$rows[venue]</td>
         </tr>";
        $color="1";
        }   
        }
        mysql_close();
        ?>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Do not echo if field is blank

Post by McInfo »

DAVID87 wrote:How can I make it so that [...] the <a> is only applied to this if results field is not blank.
I added lines 3-6 and modified lines 12 and 21.

Code: Select all

<?php
while($rows=mysql_fetch_array($result)){
    $fixture_link=$rows['fixture'];
    if(!empty($rows['results'])){
        $fixture_link="<a href='$rows[results]' target='_blank'>$rows[fixture]</a>";
    }
    if($color==1){
        echo "<tr class='rowA'>
             <td>&nbsp;</td>
             <td>$rows[day]</td>
             <td>$rows[date]</td>
             <td>$fixture_link</td>
             <td>$rows[venue]</td>
             </tr>";
        $color="2";
    } else {
        echo "<tr class='rowB'>
             <td>&nbsp;</td>
             <td>$rows[day]</td>
             <td>$rows[date]</td>
             <td>$fixture_link</td>
             <td>$rows[venue]</td>
             </tr>";
        $color="1";
    }
}
mysql_close();
?>
If you really want to look smart, use this.

Code: Select all

<?php
$r = 0;
while($rows=mysql_fetch_array($result)){
    $fixture_link=$rows['fixture'];
    if(!empty($rows['results'])){
        $fixture_link="<a href='$rows[results]' target='_blank'>$rows[fixture]</a>";
    }
    $class = (++$r & 1) ? 'rowA' : 'rowB';
    echo "<tr class='$class'>
        <td>&nbsp;</td>
        <td>$rows[day]</td>
        <td>$rows[date]</td>
        <td>$fixture_link</td>
        <td>$rows[venue]</td>
        </tr>";
}
mysql_close();
?>
Edit: This post was recovered from search engine cache.
Post Reply