Repeat every year/month/week |
[eluser]donald.tbd[/eluser]
Hello, This topic dosent have much to do with CI but i still thought people here can help me out! ![]() So im making a calendar with CI, generating and everything else was easy enough but now i have to fill in events from database. I show one month at a time. My table looks like this: id, title, content, repeat, date (date field, yyyy-mm-dd). Repeat can be 0 - 3. 0: no repeat, select all from db where repeat = 0 and date like (this-year)-(this_month)-% 1: repeat every year, select all from db where repeat = 1 and date like %-(this_month)-% 2: repeat every month, select all from db where repeat = 2 3: repeat every week, select all from db where repeat = 3 Now ive got all the data that needs displaying. With repeats 0, 1 and 2 its easy. I just use php function explode to extract the day and give it to CI calendar library to do the rest. But the problem im having is with repeat every week. For example if the date in db is 2011-05-1, the first of may 2011 that was sunday, then how do i get it to repeat every 7 days?
[eluser]malcomhfc[/eluser]
use a cron job? http://net.tutsplus.com/tutorials/php/ma...ith-php-2/ Robbie
[eluser]malcomhfc[/eluser]
OH, I'm really sorry Donald, I just re-read the question and see what your actually wanting. Unfortunately, I'm still learning php and don't think I can solve your problem. However, i learn by playing around with open source code(gotta love Phil Sturgeons work) I suggest maybe find a existing php calendar and have a look at it's source? Sorry again, should of properly read the question the first time. ![]()
[eluser]bgreene[/eluser]
is this what you need: select * from table where (`repeat`=3) and (mod((curdate()-`date field`),7) = 0)
[eluser]donald.tbd[/eluser]
Well thats a bit closer but not exactly, Ill try to explain again. For example i have a row in db: id - 1 title - title repeat - 3 (repeat every week) date - 2009-05-01 This row means that i have to display a link at every sunday (cos 2009-05-01 was sunday) where date is bigger than 2009-05-01. That means that in this months case the array i should pass on to calendar should look like this: $data = array( 5 => 'http://example.com/', 12 => 'http://example.com/', 19 => 'http://example.com/', 26 => 'http://example.com/' ); Ive been trying to figure it out or find an anwser on the internet but cant seem to do it, seems like an hard task ![]()
[eluser]misplacedme[/eluser]
I'm not sure if it'll be what you need, but you can use the mktime function to figure out what happens in 7 days. If you make a loop that builds every weekly date and throw it into an in clause, you should be able to make it work. Ex: Code: $date_start = '2011-01-01'; If you're using the db active record, you can just throw the array into $this->db->in_array('colname',$good_dates) to get the matching entries.
[eluser]donald.tbd[/eluser]
Yeah, thats one possibility, I actually tried something similar but the problem is that if i want to look at year 2100 the page will not load because the loop will take too long.
[eluser]misplacedme[/eluser]
Ah, I can see this not working when you are way in the future. In that case, modify the script to find the day of the week difference between the start date, and the date that the display range starts on. Then, add the difference to the display range start date to get the first day that matches, and use that as the first current date in my example code.
[eluser]donald.tbd[/eluser]
So this is what i came up with. From db i also select what day of the week it is like this: Code: SELECT (WEEKDAY(date)+1) AS weekday And then i take my current month and run it through a for statement and find out what days match my "weekday" i got from db. I put them into an array and later pass them on to the calendar. Thats it. ![]() Code: //run the for while $i is smaller or equal to number of days in current week Actually it was so simple, i dont understand how it took me so long to complete this! ![]() Thanks for help people. |
Welcome Guest, Not a member yet? Register Sign In |