chrisuk Posted March 11, 2007 Share Posted March 11, 2007 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! Link to comment https://forums.phpfreaks.com/topic/42248-solved-show-table-only-if-there-is-data-to-show/ Share on other sites More sharing options...
paul2463 Posted March 11, 2007 Share Posted March 11, 2007 <?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 } ?> Link to comment https://forums.phpfreaks.com/topic/42248-solved-show-table-only-if-there-is-data-to-show/#findComment-204930 Share on other sites More sharing options...
Orio Posted March 11, 2007 Share Posted March 11, 2007 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. Link to comment https://forums.phpfreaks.com/topic/42248-solved-show-table-only-if-there-is-data-to-show/#findComment-204934 Share on other sites More sharing options...
paul2463 Posted March 11, 2007 Share Posted March 11, 2007 thanks Orio, thats what I did, but forgot to mention exactly what I had done to the code..silly me Link to comment https://forums.phpfreaks.com/topic/42248-solved-show-table-only-if-there-is-data-to-show/#findComment-204936 Share on other sites More sharing options...
chrisuk Posted March 11, 2007 Author Share Posted March 11, 2007 That's great! I genuinely didn't realise it would be that simple, I thought I would have to be breaking in and out of PHP left, right and centre!! Many thanks Link to comment https://forums.phpfreaks.com/topic/42248-solved-show-table-only-if-there-is-data-to-show/#findComment-205028 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.