SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing Content Every 24Hours

    So lets just say I have a database and each record has a number, 1 through 100. Day one will show 1 to 4, the next day will show 5-8, etc.... Then when it runs out of numbers it loops back to the begining. And if there is 102, then it would be 101, 102, 1, 2....

    I'm trying not to use a crontab so It will be portable. but if it is needed, then I guess I'll have to.

    I'm completely confused as to how to do this so even a little help will lead me to new paths and may take me a long way. I just wish I had time to sleep so my brain will work properly..

    Thanks guys (and girls?)

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT content FROM table WHERE ID = (((TO_DAYS(CURDATE()) - TO_DAYS('2007-01-04')) % 100) + 1)
    Replace 2007-01-04 with whatever date is your "day 1".

    The % operator returns the remainder from integer division. The remainder when dividing by 100 will always be in the range 0-99, so I added 1, producing a range of 1-100.

    The idea is to take the number of days between the current date and "day 1" and get that remainder. This will loop within your range.

    0 / 100 = 0 remainder 0
    1 / 100 = 0 remainder 1
    2 / 100 = 0 remainder 2
    ...
    99 / 100 = 0 remainder 99
    100 / 100 = 1 remainder 0
    101 / 100 = 1 remainder 1

  3. #3
    SitePoint Addict
    Join Date
    Sep 2005
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Find the starting record by dividing the number of days in the month into the number of records. Then limit your result to the number of records you want
    $days = date("t");
    $startrecord = floor($days/$numrecords);

    $query = "select * from articles order by xxx limit $startrecord,4";

    This won't loop back but will return a unique set of records.

    Good luck with your project.
    PHP Shopping Cart Software Easy Ecommerce Shopping Cart Script.
    PHP Super Cart is 100% template driven.

  4. #4
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, maybe I didn't explain it right.... I might not want to to change every day exactly... I want the ability to change the time interval, so maybe every 5 mintues? every hour? 3 hours? 4 days?

    Thanks for the help though guys.. Good job!

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I have a hard time believing you need the ability to arbitrarily select a rotation interval of arbitrary units at arbitrary times in the future for your application. Surely you can decide on what it is you want?

    Anyway, you don't need a webmaster forum, you just need algebra. Whatever you choose, convert the current time to some multiple of the units you choose, subtract the time of "day zero" of the rotation in those same units, take the modulo of dividing that difference by 100, add 1, and you'll always get a range of 1-100 repeating to infinity.

    You can do it in PHP, you can do it in the SQL query, you can do it in any turing-complete language in existence since it's just math.

  6. #6
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope talked it over..... Okay to each day it is..... Still really confused though....

    Wouldn't setting a day one not allow it to repeat once it gets to the final record? Say there are 10 records and I want to show 3 at a time. What happens on the fourth day?

    Also using the ID doesn't work, there are 4 spots on the page that will do this, so the id for spot 1 might go 1,2,5,6,12,etc.. so i'm looking to find the first 4 records (with 'order by id') found. Then the next four....

    My brain is in a jumble....

  7. #7
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would I do this in php??

    (((TO_DAYS(CURDATE()) - TO_DAYS('2007-01-04')) % 100) + 1)

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by manipura View Post
    How would I do this in php??

    (((TO_DAYS(CURDATE()) - TO_DAYS('2007-01-04')) % 100) + 1)
    PHP Code:
    $num = (floor(time() - strtotime('1/4/2007')) / (60*60*24) % 100) + 1
    That gives you a number in the range 1-100, infinitely repeating. Your question about whether setting a start date doesn't allow it to repeat is not a problem. The modulus operator wraps the result when you reach the end of the range based on the start date. This code will work forever.

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Hakone
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would it be bad for search engine, as the content keep on changing.
    Just curious.

  10. #10
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Oh, as for groups of 4 items, changing once per day..

    PHP Code:
    $start = (floor(time() - strtotime('1/3/2007')) / (60*60*24) % 25);
    $item_1 = ($start 25) % 100) + 1;
    $item_2 = ($start 50) % 100) + 1;
    $item_3 = ($start 75) % 100) + 1;
    $item_4 = ($start 100) % 100) + 1
    Four numbers in the range 1-100, repeating every 25 days infinitely.

  11. #11
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by argv1900 View Post
    Would it be bad for search engine, as the content keep on changing.
    Just curious.
    Yes, rotating content is a bad plan if you want traffic from search engines. 96% of your content is not available at any given time.

  12. #12
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Code:
    SELECT content FROM table WHERE ID = (((TO_DAYS(CURDATE()) - TO_DAYS('2007-01-04')) % 100) + 1)
    So what would happen if I deleted the one with the id of 3? or any other one?

  13. #13
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by manipura View Post
    So what would happen if I deleted the one with the id of 3? or any other one?
    You'd have to renumber the rows and change your algorithm since the pool available has changed size. You can avoid renumbering by using that algorithm to determine the LIMIT clause of the query instead of a number, but you still have to change the algorithm since the pool size dropped and you'll eventually reach a value that doesn't exist.

    Why would you delete one? Anyway.. I think there's enough here that you can figure out what to do. Now decide on your requirements (this comes before design) and design something that meets it (this comes before coding)

  14. #14
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just if I delete one before it, then the current one will change... Not a big deal at all.. I'll live without it but I still feel like pointing it out.

    Thank you very much dan.


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
  •