Jump to content

Help needed displaying last 10 records


creatives

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/195664-help-needed-displaying-last-10-records/
Share on other sites

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.

<?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.

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

My homepage where I want the small list to appear is a html page, will I have to save this as a php file?

I presume I need to use the connect to database first?

I presume I just put this bit of code where I want the list?

 

Yes³.

 

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 = 0 === $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($result, MYSQL_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>';
}

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.