For my jQuery Calendar I need to edit this line of code to insert a new event.
[
{"id":1, "start": new Date(year, month, day, 12), "end": new Date(year, month, day, 13, 35),"title":"Lunch with Mike"},
{"id":2, "start": new Date(year, month, day, 14), "end": new Date(year, month, day, 14, 45),"title":"Dev Meeting"},
]
file_put_contents would do an awesome job if the “]” wasn’t needed at the last line, so perhaps there is a way to lookup “]” and replace it with:
{"id":3, "start": new Date(year, month, day, 14), "end": new Date(year, month, day, 14, 45),"title":"New Thing"},
]
Also, my reservations can be unset, so I also need to find a way to find that specific line (search by ID) and delete it untill the next line…But how can I achieve that?
(This code should be triggered when I’m reserving or unreserving an item.)
Perhaps, if this is even possible, this is not the right way?
What if I set a trigger on the calendar section of the site: before the calendar view is created, the event file will be filled with all the information out of the database, saved, and the calendar then will show all the events.
But how intensive is this for the page? If the database is filled with 1000 reservations, 1002 lines has to be created over and over again every time you check the calendar page. Would this be a good idea?
What are my options here? And which one is the best/right way?
Thanks in advance.
If you have not much data, call json_decode, add your content, call json_encode on the changed object to make it to a string and save it to the file again ..
Generally: I would save each line as a json string, but not all in one big object. So simply you have one json object per line:
You could append an entry while using the “a” flag in fopen(), or FILE_APPEND in file_put_contents(). This is very fast. To read out the data, iterate through each line and call json_decode(). This is faster, too.