Jump to content

Recommended Posts

Hi all,

 

I have to create a viewable booking calendar which only the admin can edit. It is supposed to take a start date and an end date from a database and changes the class of the cell for all dates inbetween those dates, so that the unbooked cells appear as normal and the booked cells have the class "date_has_event". To test if the calendar could replace values using foreach loops, I created the calendar, created a php array in an included file and used this foreach loop:

<?php
ob_start();
include("calendar_events.php"); // event multidimensional array

foreach($eventarray as $value){
    if($value['month'] == $thismonth && value['month'] == $thisyear){
        $raw = str_replace("<td class='normal'>".$value['day']."</td>","<td class='date_has_event'>".$value['day']."</td>",$raw);
    }
}
ob_clean();
echo $raw;
?>

 

to display this array file:

<?php
$eventarray[] = array(          
"month" => 4,           
"day" => 1,                           
); 
$eventarray[] = array(          
"month" => 4,           
"day" => 4,             
); 
$eventarray[] = array(          
"month" => 4,           
"day" => 22,                          
$eventarray[] = array(          
"month" => 5,           
"day" => 9,                        
); 
$eventarray[] = array(          
"month" => 5,           
"day" => 31,                     
); 
$eventarray[] = array(          
"month" => 6,           
"day" => 30,                           
);
$eventarray[] = array( 
"month" => 6, 
"day" => 3, 
);
$eventarray[] = array("year" => 2011,            
"month" => 7,           
"day" => 3,                           
);
$eventarray[] = array(          
"month" => 7,           
"day" => 4,                           
);
$eventarray[] = array(          
"month" => 7,           
"day" => 5,                           
); 
$eventarray[] = array(          
"month" => 7,           
"day" => 6,                           
);  
$eventarray[] = array( 
"month" => 7, 
"day" => 15, 
);
?> 

This worked and proved I could use a foreach loop, so I then removed that foreach loop, put a test value in the database (one entry with a start date of July 14th and an end date of July 21st), included a database connector to the page and changed the above foreach loop to this code:

<?php $raw = ob_get_contents(); //Start buffer

$query=mysql_query("SELECT calendar_startdate, calendar_enddate FROM calendar") or die ('Error:' . mysql_error()); // Select all calendar start and end dates
$fetch=mysql_fetch_array($query); //Turn $query into array for the foreach loop

foreach($fetch as $value){ //For each result, declare a value
$startdate = explode('-', $value["calendar_startdate"]);  //Explode the startdate from dd-mm-yyyy to array([0]=>dd [1]=>mm [2]=>yyyy)
if($startdate['1'] == $thismonth && $startdate['2'] == $thisyear){ //if start month is this month and start year is this year
$year1=$startdate['2']; // initial $date1 year
$month1=$startdate['1']; // initial $date1 month
$day1=$startdate['0']; // initial $date1 day
$array= array(); // create array to construct initial $date1
$date1 = implode("-", $array); //create initial $date1
    while ($date1<=$value["calendar_enddate"]){ //While the date loop value is less than or equal to the end date
        $raw = str_replace("<td class='normal'>".$value['0']."</td>","<td class='date_has_event'>".$value['0']."</td>",$raw); //replace the calendar td with a td with a date_has_event class

        $date1 = $date1 + 1; //increment $date1 by 1
} //End While
} //End If
} //End For Each


ob_clean(); //End Buffer
echo $raw; //Echo replaced calendar
?>

However, when I did this, the events did not show and the calendar also stopped putting a class in for today's date. Here is the complete code for constructing the table and replacing the contents where there is an event:

 <?php ob_start();
include("includes/calendar_events.php"); // event multidimensional array
require_once("includes/db.php");
$thisday = date( "d" );
$thismonth = ( int ) date( "m" );
$thisyear = date( "Y" );									  
$numdaysinmonth = cal_days_in_month( CAL_GREGORIAN, $thismonth, $thisyear );
$jd = cal_to_jd( CAL_GREGORIAN, date( "m" ),date( 1 ), date( "Y" ) );
$startday = jddayofweek( $jd , 0 );
$monthname = jdmonthname( $jd, 1 );
?>
<table class="cal" cellspacing="0">
<tr>
	<th>«</th>
	<th colspan="5"><?php echo date("F Y"); ?></th>
	<th>»</th>
</tr>
    <tr>
        <th width= "14.3%">Sun</th>
        <th width= "14.3%">Mon</th>
        <th width= "14.3%">Tue</th>
        <th width= "14.3%">Wed</th>
        <th width= "14.3%">Thu</th>
        <th width= "14.3%">Fri</th>
        <th width= "14.3%">Sat</th>
    </tr>
    <tr>
<?php
$emptycells = 0;
for( $counter = 0; $counter <  $startday; $counter ++ ) {
    echo "\t\t<td class='padding'></td>\n";
    $emptycells ++;
}
$rowcounter = $emptycells;
$numinrow = 7;

for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ ) {
    $rowcounter ++;
    
    if($counter == $thisday){
        echo "\t\t<td class='today'>$counter</td>\n"; // add class for today
    } else {
        echo "\t\t<td class='normal'>$counter</td>\n";
    }
    
    if( $rowcounter % $numinrow == 0 ) {
        echo "\t</tr>\n";
        if( $counter < $numdaysinmonth ) {
            echo "\t<tr>\n";
        }
        $rowcounter = 0;
    }
}
$numcellsleft = ($numinrow - $rowcounter);
if( $numcellsleft != $numinrow ) {
    for( $counter = 0; $counter < $numcellsleft; $counter ++ ) {
        echo "\t\t<td class='padding'></td>\n";
        $emptycells ++;
    }
}
$raw = ob_get_contents(); //Start buffer

$query=mysql_query("SELECT calendar_startdate, calendar_enddate FROM calendar") or die ('Error:' . mysql_error()); // Select all calendar start and end dates
$fetch=mysql_fetch_array($query); //Turn $query into array for the foreach loop

foreach($fetch as $value){ //For each result, declare a value
$startdate = explode('-', $value["calendar_startdate"]);  //Explode the startdate from dd-mm-yyyy to array([0]=>dd [1]=>mm [2]=>yyyy)
if($startdate['1'] == $thismonth && $startdate['2'] == $thisyear){ //if start month is this month and start year is this year
$year1=$startdate['2']; // initial $date1 year
$month1=$startdate['1']; // initial $date1 month
$day1=$startdate['0']; // initial $date1 day
$array= array(); // create array to construct initial $date1
$date1 = implode("-", $array); //create initial $date1
    while ($date1<=$value["calendar_enddate"]){ //While the date loop value is less than or equal to the end date
        $raw = str_replace("<td class='normal'>".$value['0']."</td>","<td class='date_has_event'>".$value['0']."</td>",$raw); //replace the calendar td with a td with a date_has_event class

        $date1 = $date1 + 1; //increment $date1 by 1
} //End While
} //End If
} //End For Each

/*foreach($eventarray as $value){
    if($value['month'] == $thismonth && $value['year'] == $thisyear){
        $raw = str_replace("<td class='normal'>".$value['day']."</td>","<td class='date_has_event'>".$value['day']."</td>",$raw);
    }*/

ob_clean(); //End Buffer
echo $raw; //Echo replaced calendar
?>

 

Any ideas :shrug:?

Link to comment
https://forums.phpfreaks.com/topic/241814-complicated-looping-events-calendar/
Share on other sites

I've found an error in the new foreach loop, where the array to construct $date1 was blank, but it didn't solve my problem. Here is the new code:

<?php $raw = ob_get_contents(); //Start buffer

$query=mysql_query("SELECT calendar_startdate, calendar_enddate FROM calendar") or die ('Error:' . mysql_error()); // Select all calendar start and end dates
$fetch=mysql_fetch_array($query); //Turn $query into array for the foreach loop

foreach($fetch as $value){ //For each result, declare a value
$startdate = explode('-', $value["calendar_startdate"]);  //Explode the startdate from dd-mm-yyyy to array([0]=>dd [1]=>mm [2]=>yyyy)
if($startdate['1'] == $thismonth && $startdate['2'] == $thisyear){ //if start month is this month and start year is this year
$year1=$startdate['2']; // initial $date1 year
$month1=$startdate['1']; // initial $date1 month
$day1=$startdate['0']; // initial $date1 day
$array= array($day1, $month1, $year1); // create array to construct initial $date1
$date1 = implode("-", $array); //create initial $date1
    while ($date1<=$value["calendar_enddate"]){ //While the date loop value is less than or equal to the end date
        $raw = str_replace("<td class='normal'>".$value['0']."</td>","<td class='date_has_event'>".$value['0']."</td>",$raw); //replace the calendar td with a td with a date_has_event class

        $date1 = $date1 + 1; //increment $date1 by 1
} //End While
} //End If
} //End For Each
ob_clean(); //End Buffer
echo $raw; //Echo replaced calendar
?>

Here is what I would do. Your existing calendar logic already has the following code to determine and output class='today' or class='normal' -

 

    if($counter == $thisday){
        echo "\t\t<td class='today'>$counter</td>\n"; // add class for today
    } else {
        echo "\t\t<td class='normal'>$counter</td>\n";
    }

 

Why not just modify that logic to do what you are trying to do. If you get your $eventarray data from the query and put it into the following format -

 

$eventarray['2011-07-03'] = array(); // empty array used here assuming you will eventually put event information into the array() and use it in the calendar
$eventarray['2011-07-04'] = array();
$eventarray['2011-07-05'] = array();
$eventarray['2011-07-06'] = array();
$eventarray['2011-07-15'] = array();

 

all you would need to do is change the  if($counter == $thisday){ ... logic to the following -

 

    // make the yyyy-mm-dd date to match against the keys in the $eventarray
$date = sprintf("%04d-%02d-%02d",$thisyear,$thismonth,$counter);
if(isset($eventarray[$date])){
	// there is an event(s) for today
        echo "\t\t<td class='date_has_event'>$counter</td>\n";
} else {
	// there is no event for today
        echo "\t\t<td class='normal'>$counter</td>\n";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.