Log in

View Full Version : Resolved Display div tag when data is available



john1991
01-03-2011, 08:08 AM
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:




<!-- 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.

fastsol1
01-03-2011, 01:07 PM
<!-- 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>";
}


?>

john1991
01-04-2011, 05:01 PM
Perfect! Thank you for pointing that out!