Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display only 5 entries from table.

Status
Not open for further replies.

matrixx1

IS-IT--Management
Feb 16, 2006
66
US
Hello...Not a PHP coder in the least bit. I have designed a funeral home website and on each page I want to display the names of the latest 5 deceased (which are in a MySQL db). Look at this page on the left column (where is says "latest obits." This is what i'm trying to do and don't know how to write the code. How can I achieve this?

FYI: To display ALL the obits under "Obituaries and Visitations", this is the code

<?php

include("connect_to_db.php");

?>

<?php

$query = "SELECT id, deceased_name, deceased_date, DATE_FORMAT(deceased_date, '%M %D, %Y') as datetext";
$query .= " FROM scales_obits ORDER BY deceased_date DESC";

$listings = mysql_query($query);
if (!listings) {
echo("<p>Error retrieving listings from lookup table<br>".
"Error: " . mysql_error());
exit();
}

echo("<table border=\"0\" width=\"88%\" class=\"obit\">");

while ($listing = mysql_fetch_array($listings)) {

$deceased_name = $listing["deceased_name"];
$deceased_date = $listing["datetext"];
$id = $listing["id"];

echo("<tr><td width=\"50%\"><a href=\"obitdetail.php?id=".$id."\"><strong>".$deceased_name."</strong></a></td><td>&nbsp;</td>");
echo("<tr><td><strong>Died:</strong> ".$deceased_date."</td><td><a href=\"obitdetail.php?id=".$id."\">View Obituary</a></td></tr>");
echo("<tr><td colspan=\"2\"><br></td></tr>");

}

echo("</table>");
?>



_________________________________________


Any help is greatly appreciated!
 
I have designed a funeral home website and on each page I want to display the names of the latest 5 deceased

good Lord. are you sure? that seems really quite morbid.

anyway, an easy approach would be to order the dataset from the query by whatever date you are measuring (DESCENDiNG) and then adding LIMIT 5 to the end of the query. this will make sure you only get five results.

the other way would be to get the entire dataset and in your loop do something like this

Code:
$c= 0 ;
while ($row = mysql_fetch_assoc($results) && $c < 5):
 //dostuff
 $c++
endwhile;

i prefer the first method.
 
Code:
SELECT id, deceased_name, deceased_date, DATE_FORMAT(deceased_date, '%M %D, %Y') as datetext";
$query .= " FROM scales_obits ORDER BY deceased_date DESC LIMIT 5

________________________________
Top 10 reasons to procrastinate:
1)
 
sorry, amended:
Code:
SELECT id, deceased_name, deceased_date, DATE_FORMAT(deceased_date, '%M %D, %Y') as datetext FROM scales_obits ORDER BY deceased_date DESC LIMIT 5

________________________________
Top 10 reasons to procrastinate:
1)
 
Thanks guys....I am in business now!!! I really appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top