CodeIgniter from Scratch: The Calendar Library
videos

CodeIgniter from Scratch: The Calendar Library

This entry is part 10 of 17 in the CodeIgniter From Scratch Session
« PreviousNext »

In this tenth episode of the CodeIgniter From Scratch screencast series, we will be exploring the Calendar library. We are also going to utilize the database class and jQuery AJAX. I will show you how to build a simple and CSS-styled calendar page, which will have the ability to store and display content for each day.

Final Example

Catch Up

Day 10: The Calendar Library

Other Viewing Options

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Rebornmx

    Thanks, very useful, maybe you could make a Facebook – CodeIgniter integration or a Star Rating System using CodeIgniter tutorial.

  • Achref

    Plz can’t you put the database of this project.

    • Jason

      It would be nice if the .sql file was included in this project.

  • http://www.jivainfotech.com Krishna Mohan

    Hello

    Great Job..
    Looking very nice.

    Better to place database script in the download.

    cheers :)

    Krishna Mohan L V

  • Desu

    Thank you for the available link to download the mp4 file. Its hard to me to watch the video online (in slow connection), so I need to download the file then watch on my PC. Very useful tutorial for me :D

  • http://www.brandontran.com/ BrandonLee

    Great programing! What do I need to do to get more events onto 1 cell/day? Since the key is the day, the key has to be unique. What would be an elegant way to show multiple events for 1 day?

    • Bryan Dease

      In order to pull that off in a nice way – I actually modified the calendar library a bit. The standard gives you a good bit of control – but still can be somewhat limiting. Here’s what I have come up with that shows events spanning multiple days and can also cover the same days as well.

      http://ttgdev.com/marsmedia/calendar/display/2010/06

      Bryan

      • http://joshbright.com Josh Bright

        Hey, the multiple day spanning deal on that calendar looks pretty slick, do you have any code anywhere that we can look at for that?

      • Bryan Dease

        Sure – give me a day or 2 and I will throw it together and give you a url. Trying to finish up the whole calendar :)

      • Mogetutu

        I wouldn’t mind getting a look and taking it for a test run.

        Good things happening!

      • Rendell

        hi, the link above doesn’t load. could you show me how can i have multiple events in a single day?

  • bill mc

    This is odd, I can’t display any items for day_num values 1 to 9 … no single digit days.
    I checked, the calendar events for those days are recorded in the database.
    The data does appear in $cal_data being passed to $this->calendar->generate($year, $month, $cal_data);

    But the resulting string does not include the calendar events for days 1 to 9.

    Anyone else get this?

    • bill mc

      In the model, in function get_calendar_data, you have to strip off the leading ’0′ where applicable.

      • bill mc

        My solution:

        function get_calendar_data($year, $month) {

        $query = $this->db->select(‘date, data’)->from(‘sys_calendar’)
        ->like(‘date’, “$year-$month”, ‘after’)->get();

        $cal_data = array();

        foreach ($query->result() as $key=>$row) {
        $cal_data[substr($row->date,8,2) + 0] = $row->data;
        }

        return $cal_data;

        }

        Adding a ’0′ in the cal_data assignment has the effect of converting the string ’01′ to the number ’1′ … solving the problem.

        Hope that helps someone else.

      • Edward

        Thank’s a lot. It’s solved my problem.

      • pinchu

        Nice solution, thanks.

      • Michael

        thx a lot, this solved my problem ;) hope codeigniter will fix this in the next release

      • yuyud

        excellent, it save my times and my hairs :D

  • sharoni

    Or you can use phps intval() function to the substr($row->date, 8, 2);

    cheers

  • karrot_komsai

    big Thanks for the intval()!

    Btw, entries would still not be removed unless accessed via a db manager like phpmyadmin (suppose you wish to delete a note by replacing it with a null value). The calendar would look empty but if you’ll scan the db, the entry is still there (with a null data value). maybe you could add a function that deletes the entry in case a null value is entered.

  • Russ

    I changed my database structure so that I could enter multiple things for any one given date. My calendar is only displaying the most recent entry in my table. Can someone point me in the right direction as to what needs to change in the get_calendar_data function for multiple entries to display? Thanks!

  • http://www.woony.be woony

    I’m having a problem passing my config to the library. It just doesn’t seems to load it… anything I put in config it has no effect… any ideas what I could be doing wrong?

  • Andy

    Thanks Burak,
    Another great tutorial! I have it working perfectly in firefox/ safari but for some reason ie won’t do the post? Any ideas anyone?
    I’m running Codeigniter on MAMP and IE is on windows in a Virtual Machine. Firefox in the same VM works fine.

    I thought at first it might be something to do with the prompt so I created a modal to hold a form but still no dice.

    I’m really stuck for ideas now so any help would be gratefully received!

  • Eoghan

    Many thanks Burak, a terrific turtorial.

  • http://rishi89.com/ Hrishikesh Choudhari

    For those of you who dont know what to put in the ‘calendar’ table for this database, let me tell you it is very simple.

    I am assuming that you are using WAMP, though this should work wherever you have phpMyAdmin installed.

    Start phpMyAdmin, create a database ‘ci_series’.

    Create a table ‘calendar’.

    Have 2 columns: 1st named ‘date’ with type ‘DATE’
    2nd named ‘data’ with type ‘VARCHAR’ and length about 100.

    And then save the table.

    And then just run the source code that you get from above, thanks to Burak.

    Cheers,

    Rishi.

  • http://coder9.com ci9

    @burak

    Thank you very much for this very very nice tutorial.

    God Bless you and Thank you!

  • ramesh

    thanks a lot….. may god bless tutplus network

  • http://www.code.my devlim

    the tutorial is php4 oop, i change to php 5 oop, seem like we cannot use $config word as our config variable , it generate error, must use any other than $config.

    wao! waste so much time to figure wat wrong wif my code…

  • http://www.code.my devlim

    does it work for day which range from 1-9?
    i cant get it work by entering the data, the tutorial also aim at day range from 10-31 only,

    so anyone? or i make mistake?

    • Edward

      I found the same problem. Did you find a solution?

  • Rendell

    hi, how am i suppose to put multiple events in the calendar? has anyone did this already?

    • Isaac

      Hi, im trying to do the same, if you know how, let me know please!

      Thanks!

  • Isaac

    Hi, great calendar, nice programming, but im still trying to have multiple events for every day, could you please help me.

    Thanks, have a nice day!

  • mike

    awesome tutorial. Any links for something like this that would allow multiple events on each day?

  • http://adam.smartwebz@gmail.com Adamsky

    Very good, what about the old-fashion boring people who are usung IE though?? Tried on 6.0 and 8.0, can not update the calendar… Any advices? Thanks.

  • mahe

    This is very useful, and nice

  • http://whitetigerdj.com Rod Nelson

    I can blank out a day to remove it but my date is still in the database how do i destroy it?

    also i would like to add Start time and End Time for my events. The default times would be 9pm to 1am in-case ya need that.

  • meltz

    i really learn alot from the codeigniter series tutorial, thanks

  • mike

    Got up to 8′ 30″ – the next and previous links do nothing – extremely frustrating.

  • FokeyJoe

    I understand this is a simple tutorial (and it is very good quality), but I thought it was a bit odd at about 8-10 mins in where you create a model to handle setting up the calendar, and then inserting a great chunk of template into it. Surely this breaks the MVC model?

    I am new to CodeIgniter (I come from Zend and bespoke frameworks) so I don’t know yet if this is actually expected practice when using their libraries. I would’ve thought that actually what you were doing was setting up a helper?

  • Øyvind

    I love the CI session :) Can anyone give a hint how I could limit a calendar to 40 days, and possibly showing it as 2-3 months side-by-side.

    • Peter

      See my post, i have a image too, I hope that can help u.

      cheers
      Peter

  • Peter

    Hello all, and thanks to the author of this great tutorial.
    Question: i made 2 calendars (outlook style) but i cant rid off, in the second calendar the next and prev links, i hope somebody can help me. I post in codeigniter if somebody want give some minutes to help

    post: http://codeigniter.com/forums/viewthread/178388/

    (las post is mine)

    txs in advance
    Peter

  • Ashok

    For Ci_series calender module database script is not available

  • dhilip

    Hi, i downloaded the page and configured it and it is working fine. Problem is with the single digit dates when entering the data in the box it is not showing in that div but it is storing in the database hope u understood my issue reply me the solution….Thanks

  • Levi

    The source file is down. Please re-upload it.

  • Aditya Joshi

    if i have multiple events on the same day, how can i add, can i also make cell expand when more than one event is inserted on one same day

  • http://nvip.com.ua sv2109

    Thank you very much, very useful.
    You can do

    if($_SERVER['HTTP_X_REQUESTED_WITH'] == ‘XMLHttpRequest’)

    in php to check jQuery ajax request

  • http://semicolondev.com semicolondev

    Duh!

    CREATE TABLE IF NOT EXISTS `calendar` (
    `date` date NOT NULL,
    `data` text NOT NULL,
    PRIMARY KEY (`date`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

  • Ian

    Hi and thanks for the tut Burak!

    I see some comments about not being up to date with the latest codeigniter.

    I am using the very latest and have no problem using the variable $config.

    Also prefixing my classes with CI_ as in class Mycal_model extends CI_Model
    It’s the same for all the CI classes now.

    And they use __construct so it has to be parent::__construct; when initiating the parent class.

    Other than that everything worked great with version 2.0.3. I also have the latest version of PHP installed if that is why it’s working right.

    I styled it up some more and added a conditional check in the jquery that checks day_num and gives an alert if it’s a blank box and someone tries to enter data.

    And it’s worth mentioning again, the problem with days 01 – 09 is fixed with intval() as mentioned in an earlier comment.

    In the get_calendar_data function in the foreach loop it should look like this

    $cal_data[intval(substr($row->date, 8,2))] = $row->data;

  • Ian

    I wanted to add this if anyone happens to be using IIS server with PHP and can’t use these .htaccess files.

    The web.config equivalent is below. My dir structure is www/nettuts_ci/ci_day10 so my web.config is:

    Got that by using URL Rewrite (a nifty little extension for IIS put out by MS to try and compete with Apache) Ha! yeah right. But what ever, it works).

    • Ian

      Ah man! Even with the pre tags it took out the xml.

      Anyway if you have IIS take Burak’s htaccess code and put it in URL Rewrite

      http://learn.iis.net/page.aspx/470/importing-apache-modrewrite-rules/

      Forget the port binding steps and the hosts file junk. I have no idea what they are doing there but it’s not necessary.

      Select the directory root of your application (not nessesarily default website as the instructions say)

      When you import and apply it will auto put a web.config file with the correct settings in the app root you chose.

  • jason

    when i click “next”

    http://localhost/mycal/2011/12
    404 page not found

    same error when click “prev”

    Any help… Thanks

    btw, i change display -> index

  • Vadim

    thanks a lot -> (from Belarus), yuo are genius !!!

  • Sameer Anand

    hey.. i can’t see the content for the current date i.e. today. However, it is going to the database and then back the other way. but i can’t see it on the page

  • MC

    So I’m using CI 2.1.0 and got totally stuck at the .htaccess part of this tutorial. Everything was working smoothly until then. I’ve spent a day trying to figure it out to no avail.

    The funniest thing is though when I download the source code and load the site from it, the file uploading and gallery work perfectly! That is without extending to CI_Controller or CI_Model, or without changing parent::Model() to parent::__construct() (which are normal changes needed for CI 2.1).

    wtf?

    What is going on here?

  • badut

    Wow it’s nice

  • http://www.sabaqonline.com Jack Sparrow

    wow… what a nice tutorial,i am very happy for such an informative tutorial.
    Thanks so much…

  • Andrea

    Thanks for your help, you sir are an artist.

    Great tutorial

  • meer

    greaaaaaaaaaaaaaaat work , its solved my problem completely
    thanks a lott:-)

  • blind

    Thank so much !.

  • mohammed

    could any one help me i have this error
    Fatal error: Call to a member function library() on a non-object in C:\wamp\www\calender_library\application\models\mycal_model.php on line 93

  • http://www.leiloport.pt João

    There are no data displaying from database, any ideas why?

    Need some help here..

    db is created, records are created yet no data returned

    • Sam

      try using intval(substr($row->date,8,2))

  • Ernesto Villanueva

    Hi, I’m watching all your videos, they are good to learn codeigniter, however i have a problem with this video.

    In the final part, when you take the value from “content” to display in prompt, I can’t make it happen,

    Here is the issueday_data = prompt(‘Teclee su comentario’,$(this).find(‘.content’).html());

    The first parameter is ok, i can see “Teclee su comentario”, but don’t see the value from content, however if I make this change:day_data = prompt(‘$(this).find(‘.content’).html(),”);
    Is working, but of course text area is blank. Why this work in one side, but not in the another one?

    Hope I explain myself.

  • Edward

    Hello everyone.

    First of all I want to say great thanks to Burak for this serial of tutorials. I learned more from this series. As about this one I found a bug. I get source cod from net.tutsplus but it was the same. Bug is: content for 1-9 days is not displaying and no possibility to do it. This data is loosing somewhere from controller to view.

    Does anyone has an idea about this?

    • http://www.facebook.com/reginaldo.gutter Reginaldo Gutter

      Hello Edward,

      I had the same problem. And that’s because Codeigniter ignores the 0 in front of the numbers from 1 to 9 when generating the calendar. I tryed concatenating a 0 when getting the day_num within the ajax we did, but inserting and getting data from the DB still wouldn’t work. So the only way I could make it to work was changing the Calendar library.

      I don’t know if it is the best thing to do. But it works just fine!

  • http://www.facebook.com/dhpratik Pratik Doshi

    hey i would like to have a CI program on star ratting. if you could do it for me. i really am in shit lacking it