Results 1 to 3 of 3

Thread: Display div tag when data is available

  1. #1
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Display div tag when data is available

    Hi,

    I have a database containing news and event, php echo’s the data ok within a div tag,

    What I am trying to do is have php echo the data along with the div tag only if there is data in the database to display, if not the div tag is not echoed?

    Any ideas? here is my source:

    HTML Code:
    <!-- only display this div if data is true -->
    
    	<div class="divright">
    	<h2>What's comming up?</h2>
    
    <div class="rightcontent"> 
    
    <?php
    
    // display from database "events" records
    $query="SELECT *, DATE_FORMAT(date, '%d-%m-%Y') as date FROM `events` ORDER BY date DESC "; // query string stored in a variable decending (DESC)
    $rt=mysql_query($query); // query executed 
    echo mysql_error(); // if any error is there that will be printed to the screen
    while($nt=mysql_fetch_array($rt)){
    echo "<h1>$nt[date]</h1> <h2>$nt[title]</h2> <p>$nt[event]</p>"; // name class and tag will be printed
    
    }
    
    ?>
    
    </div>
    Thank you for any help or suggestions.
    Last edited by john1991; 01-04-2011 at 04:55 PM.

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    254
    Thanks
    1
    Thanked 20 Times in 20 Posts

    Default

    PHP Code:
    <!-- only display this div if data is true -->

        <div class="divright">
        <h2>What's comming up?</h2> 

    <?php

    // display from database "events" records
    $query="SELECT *, DATE_FORMAT(date, '%d-%m-%Y') as date FROM `events` ORDER BY date DESC "// query string stored in a variable decending (DESC)
    $rt=mysql_query($query); // query executed 
    echo mysql_error(); // if any error is there that will be printed to the screen
    $count mysql_num_rows($rt);

    if (
    $count 0)
    {
    echo 
    "<div class=\"rightcontent\">";
      while(
    $nt=mysql_fetch_array($rt))
      {
      echo 
    "<h1>$nt[date]</h1> <h2>$nt[title]</h2> <p>$nt[event]</p>"//name class and tag will be printed

      
    }
    echo 
    "</div>";
    }


    ?>

  3. The Following User Says Thank You to fastsol1 For This Useful Post:

    john1991 (01-04-2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Perfect! Thank you for pointing that out!

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
  •