...

View Full Version : Limiting number of entries displayed



jonw118
06-07-2005, 05:35 AM
OK, I admitally have very little knowledge in the PHP development area.

I am trying to implement a script for a web site that I am using to enter news headlines on the front page... but I want to limit it 3 news items on the front page... but I can't see in the script where to limit this.

Can someone look at it and let me know if you see how it may possible (if it is possible ?!?!).

Thanks so much in advance for your help.

Here's the script:

<?php


// first, lets get a count of how many news items are in the database
$news_count = "SELECT count(*) as count FROM news";
$result = mysql_query($news_count);
$row=mysql_fetch_array($result);
$num_news=$row['count'];

// if there aren't any news items, then lets generate a user-friendly notice that there aren't any
if ($num_news <1){
print("<p class=\"warn\">There Are Currently No News Entries In The Database</p>\n");
}

// if there are any news items, lets go forward with the display
else{

// the addLinks function will place links within the news content summary
function addLinks($summary) {
$summary = preg_replace("/(?<!quot;|[=\"]|:\/\/)\b((\w+:\/\/|www\.).+?)(?=\W*([<>\s]|$))/i", "<a href=\"$1\">$1</a>", $summary);
return preg_replace("/href=\"www/i", "href=\"http://www", $summary);
}

$action=$_REQUEST['action'];
$nid=$_REQUEST['nid'];

// define the query
// if the $action variable is set, that means we're displaying a full news item and we should select the specific news entry
if(isset($action)){
$query = "SELECT * FROM news WHERE rec_id=$nid";
}
// if the $action variable isn't set, then we're displaying all of the items in summary format
else{
$query = "SELECT * FROM news ORDER BY rec_id DESC";
}

// get the results
$result = mysql_query($query);

// define the variables for display and start the loop
while ( $row = mysql_fetch_array($result)){
$rec_id = $row["rec_id"];
$title = $row["title"];
$subject = $row["subject"];
$summary = $row["summary"];
$full_text = $row["full_text"];
$date_added = $row["date_added"];






// shorten the ID
$id=$rec_id;

// these two lines convert the date from the lame-o mySQL format of 0000-00-00 and convert it
// it will now display in an American style month-name, date, and year format such as January 10, 2003
list($year, $month, $day)= explode("-", $date_added);
$american_date=date("l, F d, Y", mktime(0,0,0,$month,$day,$year));

// start the display
// I have doubts as to whether this is actually tabular data or whether it is best placed in a DIV
// but for now, a table it is
print("<div class=\"news_result\">\n");
print("<h2>$title</h2>\n");

// if the $action variable is not set, display the summary
if(!isset($action)){

if ((strlen($full_text)) > 100){
print("<div><strong>Summary:</strong> $summary</div>");
}
else{
print("<div>$summary</div>\n");
}
}



// if the $action variable *IS* set, display the full text
else{
print("<p>$full_text</p>\n");
}

if(!isset($action)){
if ((strlen($full_text)) > 45){
print("<div><a href=\"news.php?action=full&amp;nid=$id\">&gt;&gt;Full Article</a></div>\n");
}
}
else{
print("<div><a href=\"news.php\">Back To News Main</a></div>\n");
}

print("</div>\n");

}
}
?>

SeeIT Solutions
06-07-2005, 06:26 AM
I think you need to change this line


$query = "SELECT * FROM news ORDER BY rec_id DESC";

to


$query = "SELECT * FROM news ORDER BY rec_id DESC LIMIT 3";

jonw118
06-07-2005, 06:31 AM
Right on!

That's exactly what I needed to do.

Thanks so much for the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum