Welcome Guest, Not a member yet? Register   Sign In
Repeat every year/month/week
#1

[eluser]donald.tbd[/eluser]
Hello,

This topic dosent have much to do with CI but i still thought people here can help me out! Smile

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?
#2

[eluser]malcomhfc[/eluser]
use a cron job?

http://net.tutsplus.com/tutorials/php/ma...ith-php-2/

Robbie
#3

[eluser]donald.tbd[/eluser]
I dont think you understood my question right Smile
#4

[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. Sad
#5

[eluser]bgreene[/eluser]
is this what you need: select * from table where (`repeat`=3) and (mod((curdate()-`date field`),7) = 0)
#6

[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 Big Grin
#7

[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';
//Pretend you want every event from the start date to now.  
$good_dates = array($date_start);
$current_date = $date_start;
while ($current_timestamp < date('Y-m-d')) {
    list($year,$month,$day) = explode('-',$current_date); //Break the current date into the individual(important) units for use in mktime
    $current_date = date('Y-m-d',mktime(0,0,0,$month,$day+7,$year));
    $good_dates[] = $current_date;
}

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.
#8

[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.
#9

[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.
#10

[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. Smile
Code:
//run the for while $i is smaller or equal to number of days in current week
for($i = 1; $i <= date("t", mktime(0,0,0,$month,1,$year)); $i++){
  //check if this current day is the same weekday i have in database
  if(date("N", mktime(0,0,0,$month,$i,$year)) == $item["weekday"]){
    //if it is just pass it on to an array and thats all i need
    $days[] = $i
  }
}

Actually it was so simple, i dont understand how it took me so long to complete this! Big Grin
Thanks for help people.




Theme © iAndrew 2016 - Forum software by © MyBB