SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Easy Pagination script

    I'm trying to create the simplest pagination script ever and just wondered what you think about this approach.

    Here's a simple script without pagination:
    PHP Code:
    $query "SELECT * FROM images";

    $result mysql_query($query) or die(mysql_error()));

    while(
    $row mysql_fetch_assoc())
    {
        
    // do stuff with the result

    Here's the same script with pagination added:
    PHP Code:
    $query "SELECT * FROM images";

    $resultsPerPage 3;

    // Import the EasyPagination class
    require_once('class.EasyPagination.php');

    // Create an instance of the EasyPagination class and run 
    // the query (this function is a replacement for mysql_query)
    $page = new EasyPagination($query$resultsPerPage);

    // Get the pagination links
    $links $page->Links();

    // print the query result (this function is a replacement for mysql_fetch_assoc)
    while($row $page->FetchResult())
    {
        
    // do stuff with the result
    }

    // Optional: get text: "Results X - Y out of N matches"
    $OptionalText $page->Info
    Everything else is just a display issue. What do you think? Is it simple enough for the users users to integrate into their scripts?

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you post the EasyPagination class?

  3. #3
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by elduderino View Post
    Could you post the EasyPagination class?
    I don't see the relevance to the question! What I am attempting is an easy way to retrofit a current script with pagination while modifying that script as little as possible. The question is "Would most people find the above code too difficult?". Seeing the class is not going to answer that question.

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To answer your question.... most people would find it 'difficult' to understand because its class based...they wouldnt understand it at all. Reason being alot of programmers dont know OOP.

    However i think with some instruction the script would be quite easy to use, in terms of plugging it in to an already existing script

    I don't see the relevance to the question!
    No relevance really, i was just interested to see it as im learning OOP myself

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Great question.

    I have to do similar myself soon.

    Is this to go inside a template?

    I am really looking to integrate something into a wysiwyg editor (though not as complex as a pagination class).

    I dont see how you can simplify it any more...

    Unless you can write some kind of Adaptor, that allows you to abstract this original instruction:

    mysql_fetch_assoc()

    to this:

    $mysql_fetch_assoc() // which is a disguised call to $page->Links();

    After all you are only giving the method the name Links() to suit yourself... they only have to add a $ sign.

    Is that worth the bother?
    Last edited by Cups; Mar 6, 2007 at 10:22. Reason: clarification

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by elduderino View Post
    most people would find it 'difficult' to understand because its class based...they wouldnt understand it at all.
    Remove three lines of code and replace with six others! Does someone need to understand OOP to do that?

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    It looks simple enough but I guess the proof of the pudding is in the eating as they say!

    Monitor the forum till someone asks 'How do I break my results up into pages?" - you know it will be asked soon enough!
    And then hit them with the concept and script (and then the proverbial cricket bat for asking the same question as a million other people!)

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I am little bit concerned with a query. What if I want to paginate a page and I'm quering table with 10000 or more results. They will all come with
    'SELECT something FROM table' without any limit. It's not the best practice imao.
    -- Jelena --

  9. #9
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    Monitor the forum till someone asks 'How do I break my results up into pages?" - you know it will be asked soon enough!
    They don't need to think about that the class does it for them. (I'm being serious here too.) They just change the $ResultsPerPage variable and the class does everything else without them seeing it.
    Quote Originally Posted by Jelena View Post
    I am little bit concerned with a query. What if I want to paginate a page which queries table with 10000 or more result. They will all come with
    'SELECT something FROM table' without any limit. It's not the best practice imao.
    Well the class breaks the query down into a COUNT(*) query and a SELECT query. Imagine we feed the following query into the class:
    Code:
    select 
          `page`
        , `title`
        , `content`
        , `embargo`
        ,  match(`title`, `content`) 
            against ('foto' IN BOOLEAN MODE) 
            as `score` 
      from
       `pages` 
    where 
          `embargo` < 1173204088 and 
           match(`title`, `content`) 
            against ('foto' IN BOOLEAN MODE) 
    order 
          by `score` DESC
    The class breaks that into two queries as follows
    Code:
    select count(*) AS `count` 
    from
       `pages` 
    where 
          `embargo` < 1173204088 and 
           match(`title`, `content`) 
            against ('foto' IN BOOLEAN MODE)
    and
    Code:
    select 
          `page`
        , `title`
        , `content`
        , `embargo`
        ,  match(`title`, `content`) 
            against ('foto' IN BOOLEAN MODE) 
            as `score` 
      from
       `pages` 
    where 
          `embargo` < 1173204088 and 
           match(`title`, `content`) 
            against ('foto' IN BOOLEAN MODE) 
    order 
          by `score` DESC 
    limit 
          2
    offset
          6
    Obviously the class user doesn't see any of this.

  10. #10
    SitePoint Zealot
    Join Date
    May 2003
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    they dont really have to understand OOP

    the difficulty you face as a practising programmer/designer who knows that there are different programming paradigms lies in your head. a mental barrier which humans apply on themselves

    if at your first job involved just modifying that script repititively, without knowing what kind of mammal OOP is, you would find it easy.

    so I would say, if that script is just "taught", in time, with repetition as with all things relating to skills and redundancy, that task becomes easy.

    the beauty in it, which is the beauty of OOP is that, the implementation of that task is hidden from them...which is what the whole dang thing is about hey?

    so be happy deploying them things called objects!
    Elgg Customisation & Theme development
    Modx Custom Development
    PHP programming

  11. #11
    SitePoint Addict CommanderZ's Avatar
    Join Date
    Apr 2006
    Location
    Czech Republic
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The class seems to be simple to use rather than simply coded - you shoud specify it somewhere.

    I like the "API", it is simple ehough even for beginners. But I wouldn't ever use it - it would collide heavily with my own DB manipulation class

  12. #12
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CommanderZ View Post
    it would collide heavily with my own DB manipulation class
    Which is something a beginner would never be using.

  13. #13
    SitePoint Addict Skookum's Avatar
    Join Date
    Sep 2006
    Location
    Idaho
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am still a beginner / novice at coding PHP and I am just now preparing for the trek of learning OOP.

    I should say that yeah that would be simple to implement. But I would also like to know how it is run. I'm not asking you to display your code, but more of saying yeah it works great but I get weird results when I do this crazy off the wall completely unorthodox thing with the object variables. You know that that will happen, especially with beginners.

    So understanding how the class works or how the code flows may give better insight as to what caused the problem, or to a different use / deviation for the class itself.

    But then again I maybe a little more devious / curious than the average beginner.

    Just my thoughts, but simplistic wise I think you just may have something there.
    Paranoia is no longer a mental illness it is a way of life - Me

  14. #14
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Suppose you have your items in an array. Isn't it just as simple as doing this:
    PHP Code:
    $arr = array(...); // items
    $perpage 50;
    $offset 100// comes from POST/GET after someone clicks on "next page" or something of the sort
    $todisplay array_slice($arr$offset$perpage);
    foreach (
    $todisplay as $item) {
      
    //display them

    ?

  15. #15
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    Suppose you have your items in an array
    Downloading a table into an array just to get a few rows is not really a very clever idea.


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
  •