当没有从SQL数据库中检索到的行时,则不显示div

时间:2013-03-27 21:00:31

标签: php jquery sql html

我在jquery中尝试了空选择器,但它不起作用。内容仍在显示中。我正在从SQL数据库中检索一些行..如果没有数据库,那么我不想显示该div。

<div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
  {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
while($row = mysql_fetch_array($result))
  {
  echo $row['Notice'];
  }
?>
</p>
</marquee>
</div>
</div>
</div>

3 个答案:

答案 0 :(得分:1)

这是你的整个代码吗?你在使用ajax吗?如果您使用的是没有ajax的纯PHP,只需将输出div设置为“if”条件?

如果您想使用jQuery,请尝试:

if (  ($("div.scrollableArea p").text()).length > 0  )
{  
$("div.scrollableArea p").show();
}
else
{
$("div.scrollableArea p").hide();
}

但我猜你可以在没有jQuery的情况下做到这一点。

答案 1 :(得分:0)

您需要将HTML放在php if子句中。

简单示例:

<div id="scrollingText">
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysqli_connect_errno($con)) {
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
while($row = mysql_fetch_array($result))
{
   echo $row['Notice'];
 }
?>
</p>
</marquee>
</div>
</div>
<?php } else { ?>
<div class="errordiv">Display this on error</div>
<?php } ?>
</div>

答案 2 :(得分:0)

谢谢你们帮助我做了一些改变。以下代码做到了:

<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
  {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysql_num_rows($result) > 0) 
{
?><div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php while($row = mysql_fetch_array($result))
  {
  echo $row['Notice'];
  }
 ?> </p>
</marquee>
</div>
</div>
</div>  
<?php } ?>