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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';
}

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.