SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting rid null data output

    i'm trying to output pictures from my upload table but there will be some rows where it will have a null 'path' and the output will be a question mark. how can i recode it so i'll have the null 'path' not show up on the output?

    Code:
    $query = "SELECT stories.name, stories.entry, DATE_FORMAT(stories.date_entered, '%M %e, %Y %T') AS d, stories.title, upload.path 
    FROM stories LEFT OUTER JOIN upload
    ON stories.date_entered = upload.date_upload
    ORDER BY date_entered DESC LIMIT $start, $display";
    $result = @mysql_query($query);
    $num = mysql_num_rows($result);
    
    //Retrieve and print every record:
    echo '<br/>';
    while ($row = mysql_fetch_array($result)) {
    echo '<div class="post"><h2 class="title"><a href="#">' .$row['title']. '</a></h2>';
    echo '<p class="byline"><small>Posted on ' .$row['d']. ' by ' .$row['name']. '</small></p>';
    echo '<img src="'.$row['path'].'"/>';
    echo '<div class="entry"><p>' .$row['entry']. '</p></div>';
    echo '</div>';

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,334
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    COALESCE(upload.path,'') AS path

    this replaces the NULL with an empty string

    you could also use any other default string, like 'n/a' or 'missing' or 'no path'

    and of course you'd want to suppress this -- echo '<img src="'.$row['path'].'"/>';
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks r937 for the reply. the coalesce() works perfect, but the question marks still appears because of echo '<img src="'.$row['path'].'"/>';
    how can i suppress this so that only the ones with pictures in the db will appear and the one's with empty strings won't appear? will i have to make an if statement for this?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,334
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    yeah, you'd use some kind of IF condition, but i can't help you with that because i don't do php
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it's a regular array you can do:

    PHP Code:
    if (!empty ($row['path']) )
    {
         
    '<img src="'.$row['path'].'"/>'


  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome! thanks jream and r9 for your help!


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
  •