Select from...where....limit....delete?

Discussion in 'PHP' started by Pandy90, Sep 7, 2010.

  1. #1
    I have my forum limited to 30 topics a page. I want it to also limit to 4 pages, therefore deleting any topics and replys after those 4 pages.

    How would I do that?

    Here is the select query atm

    $query="SELECT * FROM `topics` WHERE `forum`='$forum' AND important='0' AND sticky='0' ORDER BY `lastreply` DESC LIMIT $forum_look, $forum_count";
    
    PHP:
    EDIT: My forum already has $forum_look and a $forum_count set. It is set to show 30 topics per page, then shows a next and previous button to go to the next or previous page.

    At the moment it displays as many pages with 30 topics per page. But i want to limit it to 4 pages, which would be 120 topics, then I want it to delete any topics after that.
     
    Last edited: Sep 7, 2010
    Pandy90, Sep 7, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    not sure if i understood you correct but could something like this work?

    
    if ($forum_look>120) {
       $forum_count=0;
    }
    $query="SELECT * FROM `topics` WHERE `forum`='$forum' AND important='0' AND sticky='0' ORDER BY `lastreply` DESC LIMIT $forum_look, $forum_count";
    
    
    PHP:
     
    stephan2307, Sep 7, 2010 IP
  3. Pandy90

    Pandy90 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No sorry if I i didn't explain enough, I just can't think of a good way to explain it.

    My foum already have $forum_look and a forum $count_set. It is set to show 30 topics per page, then shows a next and previous button to go to the next or previous page.

    At the moment it displays as many pages with 30 topics per page. But i want to limit it to 4 pages, which would be 120 topics, then I want it to delete any topics after that.
     
    Pandy90, Sep 7, 2010 IP
  4. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #4
    Do you actually mean completely deleting the topics or simply not displaying any more?
     
    stephan2307, Sep 7, 2010 IP
  5. Pandy90

    Pandy90 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    To completely delete all topics after 4 pages (120 topics)
     
    Pandy90, Sep 7, 2010 IP
  6. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #6
    Don't see the point of it but I understand what are you looking for.

    
    $query="DELETE FROM `topics` WHERE `forum`='$forum' AND important='0' AND sticky='0' ORDER BY `lastreply` DESC OFFSET 120";
    
    PHP:
    I would be very careful when using this. This will permanently delete entries from the db. USE AT OWN RISK
     
    stephan2307, Sep 7, 2010 IP
  7. TheWiseGuy

    TheWiseGuy Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #7
    Did you build your own forum?
    If yes: how did you build it and can't solve a problem like this?
    If not: doesn't it come with configurable settings / administration features?

    I'd recommend you not to delete content from the database, just mark it as unpublished. In this case if something important get's deleted you can always reactivate it
     
    TheWiseGuy, Sep 7, 2010 IP
  8. Pandy90

    Pandy90 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It's not a very important forum, its just a basic chat forum, and the reason I want to do this is because topics that are over at least 120 are old. I did not build it all myself but did most of it. I know alot of php but there are still some things I have not learned.

    Thanks for your help
     
    Pandy90, Sep 7, 2010 IP
  9. TYPELiFE

    TYPELiFE Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I would recommend adding a field named 'active' to that table and toggling it to 0/1 depending on if you want it to display or not.
     
    TYPELiFE, Sep 7, 2010 IP
  10. Pandy90

    Pandy90 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    No, I actually want to delete all topics after the 4th page, I know 100% that I want them to delete, because they are old topics.

    I just want to know how to do that, not how you think i should do it, please

    Thank you
     
    Pandy90, Sep 7, 2010 IP
  11. TheWiseGuy

    TheWiseGuy Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #11
    assuming this is the query that pulls the 120 discussions you want to keep
    
    SELECT id FROM `topics` WHERE `forum`='$forum' AND important='0' AND sticky='0' ORDER BY `lastreply` DESC LIMIT 120
    
    Code (markup):
    you should run a delete query like this:
    
    DELETE FROM topics WHERE id NOT IN (SELECT id FROM `topics` WHERE `forum`='$forum' AND important='0' AND sticky='0' ORDER BY `lastreply` DESC LIMIT 120)
    
    Code (markup):
    ATTENTION: make sure you verify that the internal query retrieves the correct discussions and make a backup before 'testing' if it works correctly
     
    TheWiseGuy, Sep 8, 2010 IP