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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.