Perad Posted January 1, 2007 Share Posted January 1, 2007 This is the final bug in my site and hopefully the last time i will need help. I have a "latest news" bar which needs 5 articles to float what is to the right of it. If there are less than 5 articles it cannot float the images to the right and my page breaks. What i want it to do is check to see if there are 5 articles. If there are less than 5 i want it to fill up the difference with echo "<div class=\"news1\"><span>Empty</span></div>";Could someone talk me through how i could adapt the script below to make it do this please.[code]function latestnews() { global $db, $max_items; $query = "SELECT id, title, cat_id, postdate FROM news ORDER BY postdate DESC LIMIT 5"; $result = mysql_query ($query) or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error()); echo '<div class="newscontainer">'; while ($row = mysql_fetch_assoc ($result)) { $postdate = htmlentities ($row['postdate']); $title = htmlentities ($row['title']); //Display Data echo "<div class=\"news1\"><span> <div class=\"left\">$title</div> <div class=\"right\"><a href=\"../UNC/index.php" . "?action=show&id=".$row['id']."\">More</a></div></span></div>"; } echo '</div>';}[/code] Link to comment https://forums.phpfreaks.com/topic/32465-solved-adding-an-arguement-to-this-script/ Share on other sites More sharing options...
bljepp69 Posted January 1, 2007 Share Posted January 1, 2007 This is one way to do it:[code]<?phpfunction latestnews() { $i=0; global $db, $max_items; $query = "SELECT id, title, cat_id, postdate FROM news ORDER BY postdate DESC LIMIT 5"; $result = mysql_query ($query) or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error()); echo '<div class="newscontainer">'; while ($row = mysql_fetch_assoc ($result)) { $postdate = htmlentities ($row['postdate']); $title = htmlentities ($row['title']); //Display Data echo "<div class=\"news1\"><span> <div class=\"left\">$title</div> <div class=\"right\"><a href=\"../UNC/index.php" . "?action=show&id=".$row['id']."\">More</a></div></span></div>"; $i++; } if ($i < 5) { for ($j=$i;$j<=5;$j++) { echo "<div class=\"news1\"><span>Empty</span></div>"; } } echo '</div>';}?>[/code] Link to comment https://forums.phpfreaks.com/topic/32465-solved-adding-an-arguement-to-this-script/#findComment-150859 Share on other sites More sharing options...
Perad Posted January 1, 2007 Author Share Posted January 1, 2007 Ah thank you so much, i made one slight tweak ($i < 4)But yeah, thanks a lot 8) Link to comment https://forums.phpfreaks.com/topic/32465-solved-adding-an-arguement-to-this-script/#findComment-150873 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.