8 Replies - 3277 Views - Last Post: 29 January 2014 - 02:12 PM Rate Topic: -----

#1 CONNERtheCODER   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 13

PHP Calendar Creation

Posted 09 November 2013 - 03:39 PM

I am making a web application that involves reservations. I need a calendar that displays the currently booked dates as blocked out or at least some way to notice "Hey, that day, or block of days, is booked" and show the entity that booked it. I am doing this in PHP and mySQL. I have looked all over the web for some free open-source and have yet to find any I can use. Does anyone have any ideas where I can find something like this? If not, I suppose I'd just have to make one myself. If so, where do I even begin?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Calendar Creation

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: PHP Calendar Creation

Posted 09 November 2013 - 03:58 PM

I just did a quick search and there are many php calendars available. Why are you unable to use one of them?

PHP-Calendar

The third link that appeared was this tutorial if you want to build one yourself.
Was This Post Helpful? 0
  • +
  • -

#3 CONNERtheCODER   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 13

Re: PHP Calendar Creation

Posted 20 November 2013 - 09:28 AM

I was looking for something simple that I could just place in my project, change a few lines of code, and have it work. However, nothing I found was that simple. I will check out the first link you sent, though. I did eventually find one that works, but I am not set on using it yet. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: PHP Calendar Creation

Posted 20 November 2013 - 11:00 AM

Of course, it is possible to do all this yourself. Let's say that you are retrieving information from a database for a particular month:

  • You'd need to work out on what day the first of this month falls, so that this becomes the first cell of an html-table that has content.
  • The table could have six rows, rather than working out where the last day falls (and how many calendar-weeks the month covers).
  • Create an html table. A cell might contain a variable number of spans to display bookings or other notices.
  • An amount of css will be required for the calendar to display nicely.
  • PHP constructs the table, inserting the bookings, etc.,in the appropriate cells.
  • (A better approach would be to construct the full table and insert the bookings, etc., as a separate step.)
  • You'll need buttons, and other events, to make the calendar functional, possibly to navigate back and forward between months.
  • This Javascript event-code could make Ajax calls to retrieve information without having to reload the entire page.


It is an amount of work, but doable.

Anyway, good luck ;)
Was This Post Helpful? 0
  • +
  • -

#5 CONNERtheCODER   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 13

Re: PHP Calendar Creation

Posted 27 January 2014 - 07:43 PM

I have looked for open source calendars but none of them do the particular job I am wanting them to do. I want a calendar like the picture. On the side a list of "units" and for the days in which the unit is "reserved" or "unavailable" I want it to be blocked out. I also want to be able to color code it. For now, though, that is a nicety. It would also be able to change the month view. It would open from the current day and one could go forward or back from that date by a single day or by a month. My problem is I have no idea how to begin doing something like this. I have never attempted something of this nature before. Some tips, resources, or anything to aid me in starting would be greatly appreciated.

Sorry, I forgot to attach the image...Attached Image
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: PHP Calendar Creation

Posted 27 January 2014 - 07:45 PM

I have merged this topic with the same one you started a while ago.
Was This Post Helpful? 0
  • +
  • -

#7 CONNERtheCODER   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 13

Re: PHP Calendar Creation

Posted 27 January 2014 - 07:49 PM

Thank you, andrewsw. You are very thorough. I had forgotten I had even posted that.

Also, I must say I did eventually use one from one of your sources. I have just decided that it's likely best to make my own because of all the customization and specifics involved.
Was This Post Helpful? 0
  • +
  • -

#8 CONNERtheCODER   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-June 13

Re: PHP Calendar Creation

Posted 29 January 2014 - 12:36 PM

I have made the calendar structure, however, I am stumped at trying to display the bookings. I do not know how to traverse through the table, placing data inside the rows, once the table has already been made. I thought about doing it as the table is being constructed but I ran into a problem there as well. I couldn't figure out how to place info about the booking (names of guest, for example) across the dates for which it would be booked. So I am here asking for help with that issue.

How can I get the booked dates to show as they do in the img? Here is my code so far:

<div class="calendar">
<?php
    $con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    $months = array('January', 'February', 'March', 'April', 'May', 'June', 
            'July', 'August', 'September', 'October', 'November', 'December');

    $month = 11;
    $year = 2013;
    $num_days = date('t', mktime(0, 0, 0, $month, 1, $year));
    echo $monthName = $months[$month - 1];

    //calendar header
    echo "<table class='body_scroll'>
        <thead><tr><th>Unit</th><th>B</th>";
    for ($i = 1; $i <= $num_days; $i++) 
        echo "<th>".$i."</th>";
    echo "</tr></thead><tbody>";

    //calendar units and availability
    $result = mysqli_query($con, "SELECT id, name AS unit FROM units WHERE active = 1");
    while ($row = mysqli_fetch_assoc($result))
    {
        echo "<tr>";
        echo "<td>".$row['unit']."</td>";
        for ($i = 1; $i <= $num_days; $i++) echo "<td>".$i."</td>";

        /*$result2 = mysqli_query($con, "SELECT r.id AS id, CONCAT(g.fname,' ',g.lname) AS name,
                                    r.arrival AS arrival, r.departure AS departure 
                                    FROM reservations r JOIN guests g ON r.guest = g.id
                                    WHERE r.unit = ".$row['id']."");
        for ($i = 1; $i <= $num_days; $i++)
        {
            while ($row2 = mysqli_fetch_assoc($result2))
            {   
                echo "<td><label></label></td>";
            }
        }*/
        echo "</tr>";
    }
    echo "</tbody></table>";
    mysqli_close($con);
?>
</div>



The $month and $year variables are just set for testing purposes right now and you can see the commented out section is where I was toying with the blocked out dates.

To iterate further, I do not know how to properly "block out" those particular <td>s for the "booked" days. I thought of having multiple parallel arrays with the returned information. (I could do a structure but I'm not even sure how that's done in PHP) and then as I hit a <td> with a matched arrival date I could change the css of the <td>s following until it hits the proper departure date. But I just don't know how that would work out syntactically.

This post has been edited by CONNERtheCODER: 29 January 2014 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2764
  • View blog
  • Posts: 8,071
  • Joined: 15-January 14

Re: PHP Calendar Creation

Posted 29 January 2014 - 02:12 PM

PHP creates the HTML markup, so you need to build the table markup as you go. PHP doesn't have any interaction with the web page once it's been sent to the browser, it only creates the markup to send. Javascript is what you use for interacting with an existing page in the browser.

So, in your case with the PHP code, you'll have one loop that will loop over the rows, the units. That loop will create and output a single table row. Inside that loop you'll have another loop that will loop over each day, and for each unit/day you'll determine what to output for that. It looks like for some events that stretch across multiple days you'll need to detect that in the first day and output a table cell that has a colspan attribute to cover multiple days. But each iteration of the inner loop will have the logic that decides what, if anything, to output for that unit for that day. Some days you won't output anything, for the case where you have a multi-day event that started on a previous day.

For the sake of performance and efficiency, it will be best to first gather all of the data for the units for the selected date range in an array. That will be faster than hitting the database for each day to figure out if there's an event for that unit for that day.

It looks like you have your code mostly structured correctly, you have the outer loop for the units and the inner loop for days. You could do something like this to get all of the data for all units:

mysqli_query($con, "SELECT r.id AS id, r.unit, CONCAT(g.fname,' ',g.lname) AS name,
r.arrival AS arrival, r.departure AS departure
FROM reservations r JOIN guests g ON r.guest = g.id ORDER BY r.unit, r.arrival");


That will give you the data for all units. You will probably want to add a WHERE clause to filter by date, so that you only get data that will fall within the date range. I typically draw that out in a timeline on paper to figure out how to get that data. You have a start and end date for your calendar display, and arrival and departure dates. So, you'll need to get events where the arrival is before the start date and the departure is after the start date, or the arrival is between the start and end dates, or the departure is between the start and end dates. Once you have that database result, you can loop through it and build an array of data for each unit. If you use the unit IDs as the array keys, then you can look up the data directly as you're going through each row of the calendar. e.g.:

$unit_data = array();
while ($row2 = mysqli_fetch_assoc($result2)) {
  if (!isset($unit_data[$row['unit']])) {
    // make a new array for this unit
    $unit_data[$row['unit']] = array();
  }
 
  // get the name and dates for the current event and add it to the unit_data array for this unit
}


The difficult part is the multi-day events. How you decide to represent that will determine how you structure the unit data array. One way would be to create an entry in that array for each day, exactly like the calendar is set up, and each time you find an event you figure out all of the days that apply to it, and loop through those days to add that event for each day element in the array. Or you could just store the actual events and just list the name and start dates, and then when you're displaying the calendar you would have to figure out how many days to stretch the event for (how many columns the cell should span). Either of those approaches would work, so do what makes the most sense to you. Calendars are one of the things that I almost always need to break out a pencil and paper and draw some things out to help me understand what I'm doing. But I would recommend that you first get all of the data using the loop like above and build an array that has everything you need, then loop through your calendar like you have now and each time in the inner loop you look up the data for that unit and that day to decide what kind of table cell you need to print. You can also store things in the database like color information for each event so that you can set the background color of the cells to match what you chose when you created the event.

This post has been edited by ArtificialSoldier: 29 January 2014 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1