Rotational view of paginated listings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samatair
    New Member
    • Nov 2007
    • 61

    Rotational view of paginated listings

    I need to change an existing listings page with pagination.
    Each page shows 10 listings and they are ordered by the date they are added to the site.

    Now recently added listings show up in the first page. I need to get all the listings to come on the first page by rotating the showing of listings perhabs each day (each page should only show 10 listings).

    Thus first day showing first 10 listings in the first page and the next day showing the next 10 listings and this should continue in a rotational manner.

    I thought of using Random option in mysql query, but since it has pagination this option wouldn't workout.

    Is there any simple and efficient way to do this (though I am not proposing any complex solution either)

    Thanks Geeks
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Don't understand fully, but what code do you have so far and why isn't it working?

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Originally posted by samatair
      Thanks Geeks
      Since this is what you do for a profession as well, I assume you are including yourself in that "geeks" remark, right?

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        From what I understand the total list (all items in the table) varies. so this week you may have a 100, next week 200 etc and it may decrease as well. But each day you want to show the "next" 10 items, instead of a random 10 item.

        But for a random 10 item that is still ordered by date, you could do ORDER BY RAND(), your_date_field ASC LIMIT 10.
        (To order by multiple fields in MySQL just put a comma between each field).

        However if you want to completely forget the random idea, you could try a more complicated way, see below:

        If you want to cycle through the pages in order one page a day, then you have to use a different method than the sql rand(). You have to store a page number in a text file and the date (timestamp).

        Your code logic should do this:

        1. Read the text file and extract the timestamp and the page number
        2a. If the timestamp is of today, use the page number for your SQL LIMIT pagation
        2b. If the timestamp is not of today (it's in the past), write to the file with today's date and increase the page number by one*

        * Of course you need to make sure you're page number doesn't reach the max number of pages, if it does (i.e. the SQL returns empty results) reset the page number value to 1.

        I'm sure you're smart enough to write the code for the above logic, but let us know if you get stuck somewhere.



        Dan

        Comment

        • samatair
          New Member
          • Nov 2007
          • 61

          #5
          Rotational view of paginated listings Reply to Thread

          Hi dlite922,
          First i am thankful to you for providing me with two types of solution.

          The first one using RAND mysql option, it surely selects listings randomly. But when you use it in pagination each time it will selects listings randomly. i.e. when the user clicks on the 2nd page it will infact select 10 random listings, so the user will not be viewing the next 10 listings, but randomly picked listings. that shouldn't happen.

          If the pagination isn't there this would be the simple and perfect solution.

          The second ofcourse impressed me, using a text file to store the page number and increment it as each day progress and when all the pages are shown, start over again.

          In this solution I have only one problem. When the user clicks the listings page, by default it is on the first page. As with the solution on the 3rd day, the user will be landing on the 3rd page, but I need to show them that they are infact on the 1st page. And ofcourse we know that we are showing the 3rd page today and 4th tomorrow, but the user shouldn't.

          If there is a way to get around this, then this should work as breeze for me. Meanwhile, I will try making this work for me and if I get it I will post the info..

          Thank you very much for clearly explaining the process.

          Samatair

          Comment

          • samatair
            New Member
            • Nov 2007
            • 61

            #6
            Originally posted by tlhintoq
            Since this is what you do for a profession as well, I assume you are including yourself in that "geeks" remark, right?
            Yes. Ofcourse we all share it :)

            Comment

            • samatair
              New Member
              • Nov 2007
              • 61

              #7
              Originally posted by TheServant
              Don't understand fully, but what code do you have so far and why isn't it working?
              I have a normal pagination code which shows 10 listings per page and the latest added listings are shown first. now instead of showing the latest listings first, need to show listings that are randomly picked or as described by dlite922. Thanks for your interest in this.

              Comment

              • dlite922
                Recognized Expert Top Contributor
                • Dec 2007
                • 1586

                #8
                Originally posted by samatair
                Hi dlite922,
                First i am thankful to you for providing me with two types of solution.

                The first one using RAND mysql option, it surely selects listings randomly. But when you use it in pagination each time it will selects listings randomly. i.e. when the user clicks on the 2nd page it will infact select 10 random listings, so the user will not be viewing the next 10 listings, but randomly picked listings. that shouldn't happen.

                If the pagination isn't there this would be the simple and perfect solution.

                The second ofcourse impressed me, using a text file to store the page number and increment it as each day progress and when all the pages are shown, start over again.

                In this solution I have only one problem. When the user clicks the listings page, by default it is on the first page. As with the solution on the 3rd day, the user will be landing on the 3rd page, but I need to show them that they are infact on the 1st page. And ofcourse we know that we are showing the 3rd page today and 4th tomorrow, but the user shouldn't.

                If there is a way to get around this, then this should work as breeze for me. Meanwhile, I will try making this work for me and if I get it I will post the info..

                Thank you very much for clearly explaining the process.

                Samatair
                Ok, this is a challenge but I think we don't really /need/ to be on page one, just pretend it's page one right?

                so I would take the value of the page for a particular day (let's say for example day 3 thus page 3) and subtract it from itself and add one (3-3+1) when displaying it for the user.

                formula:

                current page number - day off set + 1 = viewable page number.

                I'm writing this as I go so let's check to see if it works.

                The user then presses the "next page" button and I think the following two things should happen according to that formula:

                1. In the SQL page 4 is shown, because as far as the application is concerned you were on page 3.

                2. In the display, your formula's values would be 4-3+1 = 2. Thus the user see that he's on page 2 (when he's actually on page 4)

                Question is, what happens when you go the end. Let's say there is a total of 10 pages 10-3+1 would put you at page 8. User viewed actual pages 3 through 10 which equals displayed page numbers 1 through 8. so how does he view page 1 and 2 (which to the user should display page 9 and 10).

                Your application code should have a rotational pagination (meaning whenever you reach the last page, it automatically goes back to page one). so now you have these values to work with to get page 9.

                3 = the day/page offset
                1 = the true page number

                putting this in our original formula would rend 1-3+1 = -1. AHA!!

                Here's the solution. Whenever this calculated page number the user sees is less than 1 ADD the total number of pages to it. In this case -1 + 10 = 9. Voila!

                So on for page Ten: 2-3+1= 0 + 10 = 10

                I just thought of this on-the-fly. Does anybody see a fault in it?




                Dan

                Comment

                Working...