I have database with columns event_date, subject and description in my mysql table. currently, I am using following code to show calendar to highlight dates on which event is scheduled and on click on that specific date, description is shown..

But with this code, on page load, it shows current month bydefault. Instead of that I want to show the next month bydefault in which event is scheduled.

You can visit this link to see... here current month of April is showing and after selecting month of May, it shows events.. then no event in june...But in July. So i want to show calendar with May till 28 May 2015, then it should show July Month instead of June, as there is no event scheduled in month of June.

current Code is as follows:

<?php
date_default_timezone_set('Asia/kolkata');

error_reporting(0);
include("config.php");

 /// get current month and year and store them in $cMonth and $cYear variables
if($_REQUEST["month"]>0){
    $cMonth = "0".intval($_REQUEST["month"]);
if ($_REQUEST["month"]>9) {
    $cMonth = intval($_REQUEST["month"]);
}
    $cYear = intval($_REQUEST["year"]);
}else
{
    $cMonth = date("m");
    $cYear = date("Y");
}

  // generate an array with all dates with events
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE `event_date` LIKE '".$cYear."-".$cMonth."-%'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);

while ($row = mysql_fetch_assoc($sql_result)) {
    $events[$row["event_date"]]["subject"] = $row["subject"];
    $events[$row["event_date"]]["description"] = $row["description"];
}

// calculate next and prev month and year used for next / prev month navigation links and store them in respective variables
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = intval($cMonth)-1;
$next_month = intval($cMonth)+1;

// if current month is December or January month navigation links have to be updated to point to next / prev years
if ($cMonth == 12 ) {
    $next_month = 1;
    $next_year = $cYear + 1;
} elseif ($cMonth == 1 ) {
    $prev_month = 12;
    $prev_year = $cYear - 1;
}

if ($prev_month<10) $prev_month = '0'.$prev_month;
if ($next_month<10) $next_month = '0'.$next_month;
?>

  <table width="100%">
  <tr>
     <td class="mNav"><a href="javascript:LoadMonth('<?php echo $prev_month; ?>', '<?php echo $prev_year; ?>')"><<</a></td>
     <td colspan="5" class="cMonth"><?php echo date("F, Y",strtotime($cYear."-".$cMonth."-01")); ?></td>
     <td class="mNav"><a href="javascript:LoadMonth('<?php echo $next_month; ?>', '<?php echo $next_year; ?>')">>></a></td>
  </tr>
  <tr>
    <td class="wDays">Sun</td>
    <td class="wDays">Mon</td>
    <td class="wDays">Tue</td>
    <td class="wDays">Wed</td>
    <td class="wDays">Thu</td>
    <td class="wDays">Fri</td>
    <td class="wDays">Sat</td>
    </tr>
<?php 
    $first_day_timestamp = mktime(0,0,0,$cMonth,1,$cYear); // time stamp for  first day of the month used to calculate 
    $maxday = date("t",$first_day_timestamp); // number of days in current month
    $thismonth = getdate($first_day_timestamp); // find out which day of the week the first date of the month is
    $startday = $thismonth['wday'] ; // 0 is for Sunday and as we want week to start on Mon we subtract 1

for ($i=0; $i<($maxday+$startday); $i++) {

    if (($i % 7) == 0 ) echo "<tr>";

    if ($i < $startday) { echo "<td> </td>"; continue; };

    $current_day = $i - $startday + 1;
    if ($current_day<10) $current_day = '0'.$current_day;

 // set css class name based on number of events for that day
   if ($events[$cYear."-".$cMonth."-".$current_day]<>'') {
        $css='withevent';
        $click = "onclick=\"LoadEvents('".$cYear."-".$cMonth."-".$current_day."')\"";
    } else {
        $css='noevent';         
        $click = '';
    }

    echo "<td class='".$css."'".$click.">". $current_day . "</td>";

    if (($i % 7) == 6 ) echo "</tr>";
}
?> 
</table>

Recommended Answers

All 4 Replies

this can be a logic

    $cYear='2015';//set current year
    //cycle over month
    for ($i=1; $i <=12 ; $i++) { 
        //make query for the month
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE `event_date` LIKE '".$cYear."-".$i."-%'";
        $result_eventsOfMonth = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
        if (mysql_num_rows($result_eventsOfMonth)>0) {
            //print month($i);
            while ($row = mysql_fetch_assoc($result_eventsOfMonth)) {
                //print event
            }
        }
    }

beware it'is not tested.

Hi Manish.
You want to display only months that have events but you're not checking if the current month has events or not.

You could do something very simple, that'll just try another month if that one doesn't have an event, like this:

    $numEvents = 0;

    while( $numEvents == 0 ) {
       // generate an array with all dates with events
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE `event_date` LIKE '".$cYear."-".$cMonth."-%'";
        $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);

        $numEvents = mysql_num_rows($sql_result);

        if ( $numEvents == 0 ) {
            // Logic to search for the next month, like $cMonth = $cMonth + 1;
        }
    }

But the best solution would be to query the months before searching. I mean, find the next month in wich there's a event(without trying them all). Something like:

 "SELECT DISTINCT YEAR(`event_date`), MONTH(`event_date`) FROM ".$SETTINGS["data_table"]." WHERE YEAR(`event_date`) >= ".$cYear." AND MONTH(`event_date`) >= ".$cMonth." ";

I tried as per above suggestions and found solution as follows :
Than you For Suggestions @AleMonteiro and @TexWiller...

code is :

<?
date_default_timezone_set('Asia/kolkata');

error_reporting(0);
include("config.php");
$ceYear=date("Y");
$ceMonth=date("m");
$sqlevent = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE YEAR(`event_date`) >= ".$ceYear." AND MONTH(`event_date`) >= ".$ceMonth."  ";
$sql_resultevent = mysql_query ($sqlevent, $connection ) or die ('request "Could not execute SQL query" '.$sqlevent);
$rowevent = mysql_fetch_array($sql_resultevent);
/// get current month and year and store them in $cMonth and $cYear variables
if($_REQUEST["month"]>0){
    $cMonth = "0".intval($_REQUEST["month"]);
    if ($_REQUEST["month"]>9) {
        $cMonth = intval($_REQUEST["month"]);
    }
    $cYear = intval($_REQUEST["year"]);
}else
{
    $cMonth = date("m", strtotime($rowevent["event_date"]));
    $cYear = date("Y", strtotime($rowevent["event_date"]));
}

// generate an array with all dates with events
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE YEAR(`event_date`) >= ".$cYear." AND MONTH(`event_date`) >= ".$cMonth."  ";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
    $events[$row["event_date"]]["title"] = $row["title"];
    $events[$row["event_date"]]["description"] = $row["description"];
}

// calculate next and prev month and year used for next / prev month navigation links and store them in respective variables
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = intval($cMonth)-1;
$next_month = intval($cMonth)+1;

// if current month is December or January month navigation links have to be updated to point to next / prev years
if ($cMonth == 12 ) {
    $next_month = 1;
    $next_year = $cYear + 1;
} elseif ($cMonth == 1 ) {
    $prev_month = 12;
    $prev_year = $cYear - 1;
}

if ($prev_month<10) $prev_month = '0'.$prev_month;
if ($next_month<10) $next_month = '0'.$next_month;
?>
  <table width="100%">
  <tr>
      <td class="mNav"><a href="javascript:LoadMonth('<?php echo $prev_month; ?>', '<?php echo $prev_year; ?>')">&lt;&lt;</a></td>
      <td colspan="5" class="cMonth"><?php echo date("F, Y",strtotime($cYear."-".$cMonth."-01")); ?></td>
      <td class="mNav"><a href="javascript:LoadMonth('<?php echo $next_month; ?>', '<?php echo $next_year; ?>')">&gt;&gt;</a></td>
  </tr>
  <tr>
      <td class="wDays" style="background-color:#c90404; color:#FFFFFF;">Sun</td>
 <td class="wDays">Mon</td>
 <td class="wDays">Tue</td>
 <td class="wDays">Wed</td>
 <td class="wDays">Thu</td>
 <td class="wDays">Fri</td>
 <td class="wDays">Sat</td>
  </tr>
<?php 
$first_day_timestamp = mktime(0,0,0,$cMonth,1,$cYear); // time stamp for first day of the month used to calculate 
$maxday = date("t",$first_day_timestamp); // number of days in current month
$thismonth = getdate($first_day_timestamp); // find out which day of the week the first date of the month is
$startday = $thismonth['wday'] ; // 0 is for Sunday and as we want week to start on Mon we subtract 1

for ($i=0; $i<($maxday+$startday); $i++) {

    if (($i % 7) == 0 ) echo "<tr>";

    if ($i < $startday) { echo "<td>&nbsp;</td>"; continue; };

    $current_day = $i - $startday + 1;
    if ($current_day<10) $current_day = '0'.$current_day;

// set css class name based on number of events for that day
    if ($events[$cYear."-".$cMonth."-".$current_day]<>'') {
        $css='withevent';
        $click = "onclick=\"LoadEvents('".$cYear."-".$cMonth."-".$current_day."')\"";
    } else {
        $css='noevent';         
        $click = '';
    }

    echo "<td class='".$css."'".$click.">". $current_day . "</td>";

    if (($i % 7) == 6 ) echo "</tr>";
}
?> 
</table>

You're welcome Manish, and hope that'll enjoy the forum.
If you don't have any more questions regarding this issue, you can mark as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.