makzumi / laravel-calendar

Flexible Calendar for Laravel 4
58 stars 31 forks source link

Foreach loop for the $events variable #1

Closed lstables closed 10 years ago

lstables commented 10 years ago

How can I loop through a events table to show the events within the calendar.

In the docs you have:

     $events = array(
        "2014-04-09 10:30:00" => array(
            "Event 1",
            "Event 2 <strong> with html</stong>",
        ),
        "2014-04-12 14:12:23" => array(
            "Event 3",
        ),
        "2014-05-14 08:00:00" => array(
            "Event 4",
        ),
    );

But I want to loop through a model called Events

so ideally I would setup $events = Events::all(); then do a foreach for this, but how would one do it and add it the array above so these dates/events then show within the calendar?

makzumi commented 10 years ago

Hey, I don't know what your table looks like, but you need to get your events from your table, and process the collection into a new array that is grouped by date. The timestamp has to be the key. I use this helper function to group them by date.

function array_group_by($value, $items) {
    $groupedArray = array();
    foreach ($items as $key => $values) {
        $item = $values[$value];
        $groupedArray[$item][] = $values;
    }
    return $groupedArray;
}

So, something like this:

$events = Event::get()->toArray();
$events = array_group_by('event_date', $events);

Assuming you table is something like:

id > int
title > varchar
...
event_date > timestamp

Hope this helps,

Thanks!

lstables commented 10 years ago

My table is

  id  > int
  title > varchar
  date > varchar  as Im using datepicker with specific date format.

But Im getting Call to undefined function array_group_by()

makzumi commented 10 years ago

That table should work, you need to include that function in your project cause it is not part of laravel. The code for array_group_by is in my above post.

lstables commented 10 years ago

Yes I moved the array_group_by to a helpers.php file and now I get Array to string conversion error

makzumi commented 10 years ago

Important, You need to format your date back to Y-m-d H:i:s, if not it will fail. You should use a timestamp, and format your date for your view (in the required format). And reformat it for saving. Is the helper file loaded? You can load it in

app/start/global.php at the bottom

require app_path().'/helpers.php';
makzumi commented 10 years ago

I would not recommend storing dates as varchar.

lstables commented 10 years ago

Yes i understand that and just changing my code, bear with me.

lstables commented 10 years ago

So I changed my code to insert a timestamp.

I now have id > int title -> varchar date > datetime

But still get array to string conversion error

lstables commented 10 years ago

And my helpers.php is loaded as I use it for some other functions.

makzumi commented 10 years ago

Hard to tell without looking at the error/code... You need to pass an array to the array_group_by, not a collection.

makzumi commented 10 years ago

That is the only thing i can think of without looking at your code :S

lstables commented 10 years ago
    $events = Events::get()->toArray();
    $events = array_group_by('date', $events);

    $cal = Calendar::make();
    /**** OPTIONAL METHODS ****/
    $cal->setDate(Input::get('cdate')); //Set starting date
    $cal->setBasePath('admin/employees/edit/'); // Base path for navigation URLs
    $cal->showNav(true); // Show or hide navigation
    $cal->setView(Input::get('cv')); //'day' or 'week' or null
    $cal->setStartEndHours(8,20); // Set the hour range for day and week view
    $cal->setTimeClass('ctime'); //Class Name for times column on day and week views
    $cal->setEventsWrap(array('<p>', '</p>')); // Set the event's content wrapper
    $cal->setDayWrap(array('<div>','</div>')); //Set the day's number wrapper
    $cal->setNextIcon('<i class="fa fa-chevron-right"></i>'); //Can also be html: <i class='fa fa-chevron-right'></i>
    $cal->setPrevIcon('<i class="fa fa-chevron-left"></i>'); // Same as above
    $cal->setDayLabels(array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')); //Label names for week days
    $cal->setMonthLabels(array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')); //Month names
    $cal->setDateWrap(array('<div>','</div>')); //Set cell inner content wrapper
    $cal->setTableClass('table'); //Set the table's class name
    $cal->setHeadClass('table-header'); //Set top header's class name
    $cal->setNextClass('btn'); // Set next btn class name
    $cal->setPrevClass('btn'); // Set Prev btn class name
    $cal->setEvents($events); // Receives the events array
    /**** END OPTIONAL METHODS ****/

    $calendar = $cal->generate();

    return View::make('employees.edit', compact('employee','title','calendar'));
makzumi commented 10 years ago

that looks good to me. can you var_dump the $events? the 1st one

lstables commented 10 years ago

https://www.dropbox.com/s/wjq6lugpm50phhh/Screen%20Shot%202014-06-01%20at%2018.13.03.png

https://www.dropbox.com/s/bam5snn00ewwa6b/Screen%20Shot%202014-06-01%20at%2018.13.15.png

lstables commented 10 years ago

dd($events); produces -

 array (size=1)
  0 => 
     array (size=3)
        'id' => string '6' (length=1)
        'title' => string 'Test Event' (length=10)
      'date' => string '2014-06-04 00:00:00' (length=19)
makzumi commented 10 years ago

looks good, you can paste code here: laravel.io/bin

lstables commented 10 years ago

Controller http://laravel.io/bin/4PM9D

Model - http://laravel.io/bin/1WeNP

makzumi commented 10 years ago

everything looks good, try this:

....
$calendar = $cal->generate();
return $calendar;
makzumi commented 10 years ago

if you see the calendar, then your error is in the view.

lstables commented 10 years ago

Nope, still same error plus I need to pass the $calendar in the view

makzumi commented 10 years ago

can you dd the $events the 2nd time (when grouped)?

lstables commented 10 years ago

https://www.dropbox.com/s/i92inr1w955oxek/Screenshot%202014-06-01%2018.20.08.png

lstables commented 10 years ago
    $events = Events::get()->toArray();
    $events = array_group_by('date', $events);
    dd($events);

Produces:

array (size=1) '2014-06-04 00:00:00' => array (size=1) 0 => array (size=3) 'id' => string '6' (length=1) 'title' => string 'Test Event' (length=10) 'date' => string '2014-06-04 00:00:00' (length=19)

makzumi commented 10 years ago

That is your problem, take note of the required format of the array passed:

"2014-04-09 10:30:00" => array(   #KEY TIMESTAMP
            "Event 1",   #STRING  
            "Event 2 <strong> with html</stong>",  #STRING
        ),

You need to further process your $events.

makzumi commented 10 years ago

It has to be like this, because eveyone's table structure will be different.

lstables commented 10 years ago

Sorry I dont follow you, what do you mean?

lstables commented 10 years ago

I changed this $events = array_group_by('date', $events); to $events = array_group_by('title', $events);

That removes the error, but nothing is showing up on today's date as an event.

lstables commented 10 years ago

Any idea's?

makzumi commented 10 years ago

one sec, i almost have an example for you

lstables commented 10 years ago

Brilliant!

makzumi commented 10 years ago

This should work:

        $events = Events::get()->toArray();
        $events = array_group_by('date', $events);
        $new_events = [];
        foreach ($events as $key => $value) {
            foreach ($value as $v) {
                $new_events[$key][] = $v['title'];
            }
        }
        //CALENDAR CODE
        //...
        $cal->setEvents($new_events);
        $calendar = $cal->generate();
lstables commented 10 years ago

Yep worked a treat, legend thank you! A great package this is, I would suggest you add some more documentation for people like me maybe use that as an example too.

Thanks again :)

makzumi commented 10 years ago

awesome!