SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get specific row from result set?

    Hi guys,

    I am writing a comment system which gets all comments from an mySQL DB, loops through them using a while loop then spits them out on screen e.g.

    while($allFollowUps = mysql_fetch_assoc($followUpResourceID))
    {
    //output results to screen
    }

    The while loop works great when I went to process ALL the followups, but there are occasions where I need to access just the last 2 comments / results in the returned set.

    How can I go about accessing specific rows directly from the array? e.g. is it possible to do this via integers?

    I really appreciate your help,

    regards

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Easiest solution is to modify your SQL query just to return the two rows that you want
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    SELECT message FROM forum ORDER BY id DESC LIMIT 2
    my mobile portal
    ghiris.ro

  4. #4
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mark and Ernie, thats a good solution I hadnt thought about approaching it that way!

    regards

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,441
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    IMHO it would be better to instead of having MySQL get a bunch you know you don't want only to have PHP use the last 2, rewrite your sql query to only get the last 2 to begin with. Something using LIMIT

  6. #6
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, that worked great!

  7. #7
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again,

    I have a slight problem still, although its now correctly showing the two last result rows, they display the wrong way around...

    comment added: 50 minutes ago
    comment added: 53 minutes ago

    but it should show

    comment added: 53 minutes ago
    comment added: 50 minutes ago

    is this fixable?

    kind regards

  8. #8
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while($allFollowUps = mysql_fetch_assoc($followUpResourceID))
    {
    $array[]=$allFollowUps
    }
    rsort($array);
    foreach($array as $allFollowUps)
    {
    //output results to screen
    }

  9. #9
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your reply! Ive added that code, but its still coming out the same order

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    If that is the case then change rsort to sort to reverse the order.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  11. #11
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looks like the copying of the array isnt working either.

  12. #12
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't believe
    It DOES change array's order
    PHP Code:
    $array[]="50 minutes ago";
    $array[]="53 minutes ago";
    foreach(
    $array as $allFollowUps)
    {
    echo 
    $allFollowUps."<br>";
    }
    echo 
    "--------------<br>";
    rsort($array);
    foreach(
    $array as $allFollowUps)
    {
    echo 
    $allFollowUps."<br>";

    will produce
    Code:
    50 minutes ago
    53 minutes ago
    --------------
    53 minutes ago
    50 minutes ago
    there is simething wrong with your script or data. may be minutes ago calculations
    or just simple mistype even

  13. #13
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But there are many fields per row, the string "50 minutes ago" is one of 6 fields per row I should have made that more clear, my apologies

  14. #14
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh sorry my bad

  15. #15
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    krsort() will do what I meant

  16. #16
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks dude, krsort seems to be the solution

  17. #17
    SitePoint Enthusiast ScubaDvr2's Avatar
    Join Date
    Jun 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    listen, if the time posted is in the database you should just do this:
    SELECT message FROM forum ORDER BY time_posted DESC LIMIT 2 where time_posted is the field name that holds the information. I assume it doesn't actually say '50 minutes ago' in the database right? It's a timestamp?

  18. #18
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He does it already. Take a look at previous posts.

  19. #19
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    He does it already. Take a look at previous posts.
    correct, and you found me a good solution


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •