Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sort Comment From Bottom To Top [Like Facebook Comments]

    First I apologize for the long code I can't be bothered at this point to mess with the code it's already frustrating me enough (i am human do get angry..)

    Anyway,
    What I am trying to do is display the latest comments in the database I have tried DESC this displays comments from top to below which is alright but not what I need I also tried ASC and that's not displaying the most recent comments as I have a LIMIT.

    I need it to work exactly how the facebook comment system works if that makes more sense but nothing is working :/

    If this doesn't make sense let me know hope it does

    Here's the code

    $select_comments2 = $db->query("
    SELECT * FROM " . TABLE_PREFIX . "groups_comments WHERE post_id='$escape_post_id_row' AND post_id='$_GET[pid]' ORDER BY comment_id DESC LIMIT 10");

  • #2
    Regular Coder
    Join Date
    Aug 2006
    Location
    Richmond, CA
    Posts
    221
    Thanks
    3
    Thanked 11 Times in 10 Posts
    Firstly, we don't know anything of the table and how the data is inserted. Can you give us a little more?

    Secondly, you are saying "post_id=" twice. Either $escape_post_id_row and $_GET[pid] are the same, which makes it redundant, or they are different, meaning nothing will ever be returned.

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,149
    Thanks
    2
    Thanked 333 Times in 325 Posts
    To order the n newest records in ascending order, your query would be -
    Code:
    "SELECT * FROM (
    	SELECT * FROM ".TABLE_PREFIX."groups_comments
    		WHERE post_id='$escape_post_id_row'
    		ORDER BY comment_id DESC LIMIT 10
    } ORDER BY comment_id"
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Daniel I made my own simple pagination to save messing with third party code and did it to the best of my knowledge So it also needs to get the post_id so it knows which post to display all comments on as it selects it with the $_GET parameter I do need to sanitize it though trying to get the functionality working first..

    @CFMaBiSmAd thanks I try and let you know if it works

  • #5
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Get this error
    Every derived table must have its own alias

    Seems like a simple fix I try googling it

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,149
    Thanks
    2
    Thanked 333 Times in 325 Posts
    The subquery needs an alias (the 'a') in the following (also, there was a } instead of a ) ) -
    Code:
    "SELECT * FROM (
    	SELECT * FROM ".TABLE_PREFIX."groups_comments
    		WHERE post_id='$escape_post_id_row'
    		ORDER BY comment_id DESC LIMIT 10
    ) a ORDER BY comment_id"
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I do appreciate your help and I apologize for the late reply been busy lately

    But I am getting an SQL
    1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''.TABLE_PREFIX.'groups_comments' WHERE post_id='10' ORDER BY comment_id DESC' at line 2

    thanks so far

  • #8
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Edit: nevermind solved the issue

    and thanks loads your query worked!

    If there's anything I can do to repay you let me know


  •  

    Posting Permissions

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