SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot TCJeter's Avatar
    Join Date
    Jun 2002
    Location
    Anchorage, AK
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display data rows ONLY if field data exists...

    Hi all,

    I have a quick question about pulling data from my database. I have pages that show tours based on which departure city is in the 'dayshore_tourdepart' field of the database.

    Here's the first query that then displays results in a table for that city:
    PHP Code:
    <?php require_once('../data/connect.php'); ?>
    <?php 
    $sql_1 
    "SELECT * FROM dayshore WHERE dayshore_tourdepart = 'Anchorage' ORDER BY 'id'";
    if (!
    $queryResource mysql_query($sql_1$dbconn)) {
    trigger_error('Query error' .mysql_error(). 'SQL:' .$sql_1);
    }
    ?>
    <table width="97%" align="center" bgcolor="#eeeeee">
    <tr bgcolor="#d6d6c6">
         <td width="60" height="22"><strong>Tour#</strong></td>
        <td>&nbsp;&nbsp;<strong>Anchorage Day Tour</strong></td>
        <td>&nbsp;&nbsp;<strong>Duration</strong></td>
        <td>&nbsp;<strong>Departure</strong></td>
    </tr>
    <?php
    while ($row mysql_fetch_array($queryResourceMYSQL_ASSOC)) {
    echo 
    "<tr>\n";
    echo 
    "<td bgcolor=\"#f8f8f8\" width=\"35\" align=\"left\"><strong>$row[id]</strong></td>";
    echo 
    stripslashes("<td bgcolor=\"#f8f8f8\">&nbsp;&nbsp;<a href=\"$location?page=tourdetails&id=$row[id]\">
             
    $row[dayshore_tourname]</a>&nbsp;&nbsp;</td>");
    echo 
    "<td width=\"115\" bgcolor=\"#f8f8f8\">&nbsp;&nbsp;$row[dayshore_tourduration]&nbsp;&nbsp;</td>";
    echo 
    "<td width=\"115\" bgcolor=\"#f8f8f8\">&nbsp;&nbsp;$row[dayshore_tourdepart]&nbsp;&nbsp;</td>";
    echo 
    "</tr>\n";
    }
    ?>
    </table>
    So....How can I hide the table and it's title cells if there are no tours that exist in the database, that have that city in the specific field??

    If I put the
    Code:
    <table width="97%" align="center" bgcolor="#eeeeee">
    <tr bgcolor="#d6d6c6">
    	 <td width="60" height="22"><strong>Tour#</strong></td>
    	<td>&nbsp;&nbsp;<strong>Anchorage Day Tour</strong></td>
    	<td>&nbsp;&nbsp;<strong>Duration</strong></td>
    	<td>&nbsp;<strong>Departure</strong></td>
    </tr>
    in the 'while' loop, won't it display that <tr> for each row returned??

    For an example of how this is broken down, please visit my site.

    Any help would be appreciated.

    Thanks,
    -TC

  2. #2
    SitePoint Zealot manoloweb's Avatar
    Join Date
    Aug 2003
    Location
    Mexico
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    <?php require_once('../data/connect.php'); ?>
    <?php 
    $sql_1 
    "SELECT * FROM dayshore WHERE dayshore_tourdepart = 'Anchorage' ORDER BY 'id'";
    if (!
    $queryResource mysql_query($sql_1$dbconn) || !mysql_num_rows($queryResource)) {
    //trigger_error('Query error' .mysql_error(). 'SQL:' .$sql_1);
    echo "No Tours Found";
    } else {
    ?>
    <table width="97%" align="center" bgcolor="#eeeeee">
    <tr bgcolor="#d6d6c6">
    <td width="60" height="22"><strong>Tour#</strong></td>
    <td>&nbsp;&nbsp;<strong>Anchorage Day Tour</strong></td>
    <td>&nbsp;&nbsp;<strong>Duration</strong></td>
    <td>&nbsp;<strong>Departure</strong></td>
    </tr>
    <?php
    while ($row mysql_fetch_array($queryResourceMYSQL_ASSOC)) {
    echo 
    "<tr>\n";
    echo 
    "<td bgcolor=\"#f8f8f8\" width=\"35\" align=\"left\"><strong>$row[id]</strong></td>";
    echo 
    stripslashes("<td bgcolor=\"#f8f8f8\">&nbsp;&nbsp;<a href=\"$location?page=tourdetails&id=$row[id]\">
        
    $row[dayshore_tourname]</a>&nbsp;&nbsp;</td>");
    echo 
    "<td width=\"115\" bgcolor=\"#f8f8f8\">&nbsp;&nbsp;$row[dayshore_tourduration]&nbsp;&nbsp;</td>";
    echo 
    "<td width=\"115\" bgcolor=\"#f8f8f8\">&nbsp;&nbsp;$row[dayshore_tourdepart]&nbsp;&nbsp;</td>";
    echo 
    "</tr>\n";
    }
    echo 
    "</table>\n";
    }
    ?>

  3. #3
    SitePoint Zealot TCJeter's Avatar
    Join Date
    Jun 2002
    Location
    Anchorage, AK
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks

    Thanks for the reply manoloweb -
    For the result, I wouldn't really want it to even print the 'No Tours Found' line, so I'll try and apply everything in your example except for that. If you take a look at that link I posted, you'll see the format of the page and how that would break the 'flow'.

    I'll be back at my working machine on the morrow and come back here if I can't make that fly.

    Thanks again,
    -TC

  4. #4
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So just wrap the stuff that you want to print if there are tours in an if statement and leave out the else. If the if evaluates to true, then your tours will be echoed. If not, nothing will happen.
    Aaron Brazell
    Technosailor



  5. #5
    SitePoint Zealot TCJeter's Avatar
    Join Date
    Jun 2002
    Location
    Anchorage, AK
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    answer

    Thanks for the direction -

    (Deleted non-working code)

    Thanks again,
    -TC

    EDIT: Whoa! Wait! I messed this one up cause I stuck it INSIDE the while loop. I've gotta fix that then I'll repost the working code - if I can get it right.

  6. #6
    SitePoint Zealot TCJeter's Avatar
    Join Date
    Jun 2002
    Location
    Anchorage, AK
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OK I think this is it...

    OK, I think I figured it out -
    I used the mysql_num_rows to see if any matched my query. If not, it shows nothing. If it returns equal to or greater than '1' then it shows the table.

    Seems to work fine now
    PHP Code:
    $sql_5 "SELECT * FROM dayshore WHERE dayshore_tourdepart = 'Skagway' ORDER BY 'id'";
    if (!
    $queryResource mysql_query($sql_5$dbconn)) {
     
    trigger_error('Query error' .mysql_error(). 'SQL:' .$sql_5);
     }
    $numRows mysql_num_rows($queryResource);
    if(
    $numRows >= 1) {
      echo 
    "<table width=\"97%\" align=\"center\" bgcolor=\"#eeeeee\">";
      echo 
    "<tr bgcolor=\"#d6d6c6\">";
      echo 
    "<td width=\"60\" height=\"22\"><strong>Tour#</strong></td>";
      echo 
    "<td>&nbsp;&nbsp;<strong>Skagway Day Tour</strong></td>";
      echo 
    "<td>&nbsp;&nbsp;<strong>Duration</strong></td>";
      echo 
    "<td>&nbsp;<strong>Departure</strong></td>";
      echo 
    "</tr>\n";
     while (
    $row mysql_fetch_array($queryResourceMYSQL_ASSOC)) {
      echo 
    "<tr>\n";
      echo 
    "<td bgcolor=\"#f8f8f8\" width=\"35\" align=\"left\"><strong>$row[id]</strong></td>";
      echo 
    stripslashes("<td bgcolor=\"#f8f8f8\">&nbsp;&nbsp;<a href=\"$location?page=tourdetails&id=$row[id]\">
    $row[dayshore_tourname]</a>&nbsp;&nbsp;</td>");
      echo 
    "<td width=\"115\" bgcolor=\"#f8f8f8\">&nbsp;&nbsp;$row[dayshore_tourduration]&nbsp;&nbsp;</td>";
      echo 
    "<td width=\"115\" bgcolor=\"#f8f8f8\">&nbsp;&nbsp;$row[dayshore_tourdepart]</td>";
      echo 
    "</tr>\n";
     }
     echo 
    "</table><br />\n";
    }
    ?> 


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
  •