Author Topic: Help needed displaying last 10 records  (Read 385 times)

0 Members and 1 Guest are viewing this topic.

Offline creativesTopic starter

  • Irregular
  • Posts: 15
    • View Profile
Help needed displaying last 10 records
« on: March 18, 2010, 06:34:24 AM »
Hi guys, I have a mysql database consisting of the following tables and fields:-

Table name: People
Fields: ID, name, profileurl

I want to display on my homepage the 10 most recent people I add, but as clickable links ie showing name linking to profile url.  With the most recent at the top of the list.

I also have another table: updates
Fields: title, url

I also want to show the last 25 updates displaying title as clickable link to the url

If anyone can give me a php code to display this, that would be great, thanks so much!

Offline creativesTopic starter

  • Irregular
  • Posts: 15
    • View Profile
Re: Help needed displaying last 10 records
« Reply #1 on: March 23, 2010, 04:32:40 PM »
Bump....can anyone help me with this?

Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: Help needed displaying last 10 records
« Reply #2 on: March 23, 2010, 04:37:28 PM »
SELECT field, field, .. FROM table WHERE field = value ORDER BY id DESC LIMIT 10
Developer from Belgium, Vlaams-Brabant

Offline creativesTopic starter

  • Irregular
  • Posts: 15
    • View Profile
Re: Help needed displaying last 10 records
« Reply #3 on: March 25, 2010, 03:15:32 PM »
thanks for that!  But I am still unsure of what to do exactly to get the list to show on my homepage. Is there any chance you could type the full code, how I use it etc.

My homepage where I want the small list to appear is a html page, will I have to save this as a php file?  And what exactly do I need to put into the hompage code, to show the list?

I presume I need to use the connect to database first?  Do I use an additional snippet code before this code, or is this read from another file? I presume I just put this bit of code where I want the list:-

<? php SELECT field, field, .. FROM table WHERE field = value ORDER BY id DESC LIMIT 10 ?>

Sorry for all the questions, I am totally new to php stuff.

Offline creativesTopic starter

  • Irregular
  • Posts: 15
    • View Profile
Re: Help needed displaying last 10 records
« Reply #4 on: March 28, 2010, 05:06:56 PM »
bump please help

Offline GetPutDelete

  • Enthusiast
  • Posts: 70
  • Gender: Male
  • Likes being RESTful
    • View Profile
    • My YouTube Tutorials
Re: Help needed displaying last 10 records
« Reply #5 on: March 28, 2010, 05:11:55 PM »
<?php

$res 
mysql_query("select * from People order by ID desc limit 0, 10");
while(
$row mysql_fetch_array($res)) {

   echo 
'<a href="' $row['profileurl'] . '">' $row['name'] . '</a><br />';

}

?>


That should get you started, there is a ton of documentation about mysql select in PHP which is newbie friendly, give W3 Schools a go, it's a good starting point.
« Last Edit: March 28, 2010, 05:12:56 PM by GetPutDelete »
My YouTube PHP Tutorials @ http://www.youtube.com/user/awesomePHP
My Webby @ http://awesomephp.wordpress.com/

Check it out...

Offline mrMarcus

  • Devotee
  • Posts: 1,475
  • Gender: Male
  • 2011/12 Stanley Cup Champions.
    • View Profile
Re: Help needed displaying last 10 records
« Reply #6 on: March 28, 2010, 05:12:23 PM »
seems as though you're jumping in head first.  there are millions of basic tutorials on the net that show you exactly, step-by-step, how to connect to a database and display its contents on a page.

i recommend getting familiar with that before trying to take on too much else.

check this out: http://www.phpfreaks.com/tutorial/php-basic-database-handling

Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: Help needed displaying last 10 records
« Reply #7 on: March 28, 2010, 05:22:52 PM »
Quote
My homepage where I want the small list to appear is a html page, will I have to save this as a php file?
Quote
I presume I need to use the connect to database first?
Quote
I presume I just put this bit of code where I want the list?

Yes³.

Quote
Do I use an additional snippet code before this code, or is this read from another file?

No.

1. Add a field signup_date (DATETIME) to your people table

2.

function get_latest_signups($number 10$db null) {
    
$number intval($number);
    
$number === $number 10 $number;
    
    
$latest_registrations = array();
    
$query "SELECT name, profileurl FROM people ORDER BY signup_date DESC LIMIT $number";
    
$result mysql_query($query$db);
    if (
$result && mysql_num_rows($result)) {
        while (
$row mysql_fetch_array($resultMYSQL_ASSOC)) {
            
$latest_registrations[] = $row;
        }
    }
    return 
$latest_registrations;
}

$latest_signups get_latest_signups();
foreach (
$latest_signups as $user) {
    echo 
'<a href="'$user['profileurl'], '">'$user['username'], '</a>';
}
Developer from Belgium, Vlaams-Brabant