Jump to content

[SOLVED] show table only if there is data to show?


chrisuk

Recommended Posts

I have a table that shows specific data (via a while loop)

 

however, when you first log in as a new user, you see the table column headers but there is obviously no data, and it looks a bit unsightly.

 

How could i take this code:

 

<?php

$data = "SELECT * from news ORDER BY id DESC LIMIT 0 , 5";
$sql= mysql_query($data) or die(mysql_error());

?>
        <strong>Latest News listings for your homepage:</strong> <br>        
        <br>
      <table width="505" border="0" cellspacing="1" cellpadding="2">
      <tr>
        <td width="76">Date</td>
        <td width="262">Title</td>
        <td width="62">Action</td>
      </tr>
  <?php 

  while ( $row = mysql_fetch_array ( $sql ) ){
  
  ?>
      <tr>
        <td class="newstext"><? echo date('d M Y',strtotime($row[news_date])); ?></td>
        <td class="newstext"><? echo $row[news_title]; ?></td>
        <td><a href="delete_news.php?id=<? echo $row[id]; ?>&ref=index.php">DELETE</a></td>
      </tr>
  <?php } ?>
    </table>

 

and only show the table IF there is something to show, otherwise the title on the page would be "no news"?

 

Thanks!

<?php

$data = "SELECT * from news ORDER BY id DESC LIMIT 0 , 5";
$sql= mysql_query($data) or die(mysql_error());
if (mysql_num_rows($sql)>0)
{
?>
        <strong>Latest News listings for your homepage:</strong> <br>        
        <br>
      <table width="505" border="0" cellspacing="1" cellpadding="2">
      <tr>
        <td width="76">Date</td>
        <td width="262">Title</td>
        <td width="62">Action</td>
      </tr>
  <?php 

  while ( $row = mysql_fetch_array ( $sql ) ){
  
  ?>
      <tr>
        <td class="newstext"><? echo date('d M Y',strtotime($row[news_date])); ?></td>
        <td class="newstext"><? echo $row[news_title]; ?></td>
        <td><a href="delete_news.php?id=<? echo $row[id]; ?>&ref=index.php">DELETE</a></td>
      </tr>
  <?php } ?>
    </table>
<?php } ?>

 

Use mysql_num_rows():

 

<?php

$data = "SELECT * from news ORDER BY id DESC LIMIT 0 , 5";
$sql= mysql_query($data) or die(mysql_error());
if (mysql_num_rows($sql))
{
?>
        <strong>Latest News listings for your homepage:</strong> <br>        
        <br>
      <table width="505" border="0" cellspacing="1" cellpadding="2">
      <tr>
        <td width="76">Date</td>
        <td width="262">Title</td>
        <td width="62">Action</td>
      </tr>
  <?php 

  while ( $row = mysql_fetch_array ( $sql ) ){
  
  ?>
      <tr>
        <td class="newstext"><? echo date('d M Y',strtotime($row[news_date])); ?></td>
        <td class="newstext"><? echo $row[news_title]; ?></td>
        <td><a href="delete_news.php?id=<? echo $row[id]; ?>&ref=index.php">DELETE</a></td>
      </tr>
  <?php } ?>
    </table>
<?php } ?>

 

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.