Jump to content

[SOLVED] Adding an arguement to this script?


Perad

Recommended Posts

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]
This is one way to do it:

[code]
<?php
function 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]

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.