Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 24

Thread: Last 10 Items

  1. #1
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    I am currently learning PHP and mySQL, and i need to know how to query a database using PHP to display the 10 newest items in the database. I need to do this one by one so i need to use the code to display the newest, the 2nd newest, 3rd etc. Any help appreciated but don't get too advanced on me - i am a learner :cheeky:

    thanks guys :classic:
    JR

  2.  

  3. #2
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    If you have an auto increment field, like a primary key, then you can use a query like the following:
    Code:
     SELECT * FROM table ORDER BY pri_key DESC LIMIT 10
    If you have a timestamp, or datatime field that is being populated using NOW() or something, you can use that same query but with date and time field instead of the primary key.
    There and Back Again :Ogre:

  4. #3
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    thanks nsr, i will try this out tommorow (well today) and tell you if i have any problems

    thanks :classic:
    JR

  5. #4
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    i have now written some php to do this, but i have no idea what i am doing and it is wrong

    It gives me a list of Resource id # damnit

    Please will some body check my code...
    PHP Code:
    <title>PHP List Test</title>
    <?php
    $query1 
    "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 0, 1";
    $query2 "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 1, 1";
    $query3 "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 2, 1";
    $query4 "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 3, 1";
    $query5 "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 4, 1";
    $query6 "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 5, 1";
    $query7 "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 6, 1";
    $query8 "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 7, 1";
    $query9 "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT     8, 1";
    $query10 "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 9, 1";
    $msg1 mysql_query($query1);
    $msg2 mysql_query($query2);
    $msg3 mysql_query($query3);
    $msg4 mysql_query($query4);
    $msg5 mysql_query($query5);
    $msg6 mysql_query($query6);
    $msg7 mysql_query($query7);
    $msg8 mysql_query($query8);
    $msg9 mysql_query($query9);
    $msg10 mysql_query($query10);
    ?>
    </head>

    <body>
    <ul>
    <li><?php echo $msg1?></li>
    <li><?php echo $msg2?></li>
    <li><?php echo $msg3?></li>
    <li><?php echo $msg4?></li>
    <li><?php echo $msg5?></li>
    <li><?php echo $msg6?></li>
    <li><?php echo $msg7?></li>
    <li><?php echo $msg8?></li>
    <li><?php echo $msg9?></li>
    <li><?php echo $msg10?></li>
    </ul>
    </body>
    </html>
    Thanks :classic:
    JR

  6. #5
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    Try this:

    PHP Code:
    <?php
    $query 
    "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 0, 10";
    $result mysql_query($query);


    while(
    $row mysql_fetch_array($result))
    {
        
    $msg .= "<li>".$row['rave']."</li>\n";
    }

    ?>
    <title>PHP List Test</title>
    </head>
    <body>
    <ul>
    <?php echo $msg?>
    </ul>
    </body>
    </html>
    There and Back Again :Ogre:

  7. #6
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    the problem is though i need each one seperate, i was only testing it in a bulleted list
    JR

  8. #7
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    You can store them in an array, if you gonna use them all on the same page. That'll save you 9 database quries.

    PHP Code:
    <?php
    $query 
    "SELECT `rave` FROM `Test` ORDER BY id DESC LIMIT 0, 10";
    $result mysql_query($query);

    $msg_array "";
    $i=0;

    while(
    $row mysql_fetch_array($result))
    {
        
    $msg_array[$i] = $row['rave'];
        
    $i++;
    }

    ?>
    <title>PHP List Test</title>
    </head>
    <body>
    <ul>
    <li><?PHP echo $msg_array[0]; ?></li>
    <li><?PHP echo $msg_array[1]; ?></li>
    <li><?PHP echo $msg_array[2]; ?></li>
    <li><?PHP echo $msg_array[3]; ?></li>
    <li><?PHP echo $msg_array[4]; ?></li>
    <li><?PHP echo $msg_array[5]; ?></li>
    <li><?PHP echo $msg_array[6]; ?></li>
    <li><?PHP echo $msg_array[7]; ?></li>
    <li><?PHP echo $msg_array[8]; ?></li>
    <li><?PHP echo $msg_array[9]; ?></li>
    </ul>
    </body>
    Sorry didn't answer you first question. You will need to use a function like mysql_fetch_array or mysql_fetch_row to get the data from result resource identifier that is returned after running the query.
    </html>
    There and Back Again :Ogre:

  9. #8
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    right thanks, i will try it out later on - i will give you the money but if it doesn't work i will get 2 big guys to come round to your house kick your *** and get my money back
    JR

  10. #9
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    LOL, why don't you wait till you have the chance to test it.

    BTW, I'm not scared of your big guys, I live in Brooklyn
    There and Back Again :Ogre:

  11. #10
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Originally posted by jravenscroft
    right thanks, i will try it out later on - i will give you the money but if it doesn't work i will get 2 big guys to come round to your house kick your *** and get my money back
    Well actually they'll have to kick his ***, and then drag him to HM's NOC, dissect the hard drive for the correct server, and use a magnet very carefully to change the field in the database.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!


Page 1 of 3 1 2 3 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:42 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com