SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IF there's data in a database cell...

    I want a given row in a database table to be displayed ONLY if there's content in the database table variable contained in that row, Text2. This is the script I'm using at present, but it isn't working:

    PHP Code:
    if($row['Text2'] !=''):
        echo 
    "<tr colspan='2'><td>".$row['Text2'] ."</td></tr>";
    endif; 
    That seems to be a really touchy script; it works in many situations, but it frequently blows up if I try to modify it. So maybe I just wrote it wrong.

    Otherwise, is there a better way of writing it? Below is the entire script.

    Thanks.

    PHP Code:
    <?php
    [DATABASE CONNECTION]
    list(
    $d$m) = explode("-"date("j-m")); // a m to get leading zero
    $sql "SELECT Date_Event, Event, Text2 FROM gzcalendar WHERE DAYOFMONTH(Date_Event) = $d AND MONTH(Date_Event) = $m";
    $res mysql_query($sql) or die('mysql_error() on '.$sql.' gives '.mysql_error());
    echo 
    "<table>";
    while(
    $row mysql_fetch_array($res))
    {
        
    $date_event_ex =  explode("-"$row['Date_Event']);
        echo 
    "<tr><td><p class='pflat'><strong>"$date_event_ex[0] ."</strong> ".$row['Event'] ."</p></td></tr>";


    if(
    $row['Text2'] !=''):
        echo 
    "<tr colspan='2'><td>".$row['Text2'] ."</td></tr>";
    endif;


    }     
    echo 
    "</table>";
    ?>

  2. #2
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do this:

    Code:
    if($row['Text2'] !=''): 
        echo "<tr colspan='2'><td>{$row['Text2']}</td></tr>"; 
    endif;
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  3. #3
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ($row['Text2']) :
        echo 
    "<tr colspan='2'><td>".htmlentities($row['Text2'] )."</td></tr>";
    endif; 
    If you have funny char in it it could make some funny stuff at display.
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinyl-junkie
    Do this:

    Code:
    if($row['Text2'] !=''): 
        echo "<tr colspan='2'><td>{$row['Text2']}</td></tr>"; 
    endif;
    Thanks. I was going to say, "but that doesn't work," but I checked my source code and discovered that it IS working. The whole idea is to remove spaces created by empty table cells by removing those rows entirely.

    There's still a space, but it's apparently related to some sort of padding or something, so I'll check it out.

    Thanks for the tip.

  5. #5
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Field empty

    If nothing was put in field then it can be value NULL.
    so:
    if ($value == '') ... could be false
    better anycase
    if ($value) ...
    which will be conversed as false the same for NULL and ''
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toggg
    If nothing was put in field then it can be value NULL.
    so:
    if ($value == '') ... could be false
    better anycase
    if ($value) ...
    which will be conversed as false the same for NULL and ''
    OK, thanks for the tips.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •