views:

505

answers:

3
+1  Q: 

PHP Parse Error

I am trying to add an echo statement after the line:

echo '<td class="today" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';

It is just a simple echo ''; (Really I am trying to break the line up to not include the in the same echo statement, so I can insert some more code later. I however am getting a Parse error after adding echo ''; and I cannot figure out why. Any advice will be appreciated.

<?PHP

require 'includes/user_db_connect.php';
include 'includes/functions.php';

function isAjax() {
 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
     $_SERVER ['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest';
}

if(isAjax() && isset($_POST['month']))
{
    $month = $_POST['month'];
    $year = !isset($_POST['year']) ? date('Y', $current_time) : $_POST['year'];
    die(getCalendar($month,$year));
}

$month = date('m', time());
$year = date('Y', time());
$calendar = getCalendar($month,$year);

function getCalendar($month,$year)
{
    $current_time = time();

    $month_start = mktime(0,0,0,$month, 1, $year); 

    $month_name = date('F', $month_start); 

    $first_day = date('D', $month_start);

    switch($first_day)
    {
    case "Sun":
     $offset = 0;
     break;
    case "Mon":
     $offset = 1;
     break;
    case "Tue":
     $offset = 2;
     break;
    case "Wed":
     $offset = 3;
     break;
    case "Thu":
     $offset = 4;
     break;
    case "Fri":
     $offset = 5;
     break;
    case "Sat":
     $offset = 6;
     break;
    } 

    if($month == 1)
     $num_days_last = cal_days_in_month(CAL_GREGORIAN, 12, ($year -1));
    else
     $num_days_last = cal_days_in_month(CAL_GREGORIAN, ($month - 1), $year);

    $num_days_current = cal_days_in_month(CAL_GREGORIAN, $month, $year); 

    for($i = 0; $i < $num_days_current; $i++)
    {
     $num_days_array[] = $i+1;
    } 

    for($i = 0; $i < $num_days_last; $i++)
    {
     $num_days_last_array[] = '';
    }

    if($offset > 0){ 
     $offset_correction = array_slice($num_days_last_array, -$offset, $offset);
     $new_count = array_merge($offset_correction, $num_days_array);
     $offset_count = count($offset_correction);
    }
    else
    { 
     $new_count = $num_days_array;
    }

    $current_num = count($new_count); 

    if($current_num > 35)
    {
     $num_weeks = 6;
     $outset = (42 - $current_num);
    }
    else if($current_num < 35)
    {
     $num_weeks = 5;
     $outset = (35 - $current_num);
    }
    if($current_num == 35)
    {
     $num_weeks = 5;
     $outset = 0;
    }

    for($i = 1; $i <= $outset; $i++)
    {
     $new_count[] = '';
    }

    $weeks = array_chunk($new_count, 7);

    ob_start();

    $last_month = $month == 1 ? 12 : $month - 1;
    $next_month = $month == 12 ? 1 : $month + 1;

    echo <<<HEAD
    <table id="calendar">
    <tr>
     <td class="month">
     <p><a href="#" class="monthnav" onclick="getPrevMonth();return false;">&laquo; Prev</a>
     $month_name $year</b>
     <a href="#" class="monthnav" onclick="getNextMonth();return false;">Next &raquo;</a></p></td>
    </tr>
    <tr class="daynames"> 
     <td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>
    </tr>
HEAD;

    foreach($weeks AS $week){
     echo '<tr class="week">'; 
     foreach($week as $day)
     {
      if($day == date('d', $current_time) && $month == date('m', $current_time) && $year == date('Y', $current_time))
       echo '<td class="today" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';

      else
       echo '<td class="days" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';
     }
     echo '</tr>';
    }

    echo '</table>';

    return ob_get_clean();
}
?>

<html>
<head>
<link href="./calendar.css" rel="stylesheet" type="text/css">
<script src="../js/prototype.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
    var current_month = <?PHP echo @$month ?>;
    var current_year = <?PHP echo @$year ?>;

    function getPrevMonth()
    {
     if(current_month == 1)
     {
      current_month = 12;
      current_year = current_year - 1;
     }
     else
     {
      current_month = current_month - 1;
     }
     params = 'month='+current_month+'&year='+current_year;
     new Ajax.Updater('calendar_wrapper',window.location.pathname,{method:'post',parameters: params});
    }
     function getNextMonth()
     {
      if(current_month == 12)
      {
       current_month = 1;
       current_year = current_year + 1;
      }
      else
      {
       current_month = current_month + 1;
      }
      params = 'month='+current_month+'&year='+current_year;
      new Ajax.Updater('calendar_wrapper',window.location.pathname,{method:'post',parameters: params});
     }
</script>
</head>

<body>
<div id="calendar_wrapper"><?PHP echo @$calendar ?></div>
</body>
</html>
+2  A: 

If you leave out the braces ({}) in control structures like if, for, while, foreach etc. only a single statement is allowed as body part. For multiple statements you need the braces to mark the start and end of the block.

So try this:

if ($day == date('d', $current_time) && $month == date('m', $current_time) && $year == date('Y', $current_time)) {
    echo '<td class="today" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';
    // your echo
} else
    echo '<td class="days" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';

The else part also could be wrapped into braces. But that is optional in this case.

Gumbo
+1  A: 

you need to add curly brackets { }

     if($day == date('d', $current_time) && $month == date('m', $current_time) && $year == date('Y', $current_time))
      echo '<td class="today" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';

     else
      echo '<td class="days" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';

should become:

     if($day == date('d', $current_time) && $month == date('m', $current_time) && $year == date('Y', $current_time))
        {
      echo '<td class="today" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';
            // add what you want
        }
     else
      echo '<td class="days" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';
Aziz
Thanks, I knew it was something I overlooked.
jason_m
A: 

It's because you're in the middle of an if-else there. You'd need to do:

if($day == date('d', $current_time) && $month == date('m', $current_time) && $year == date('Y', $current_time)) {
    echo '<td class="today" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';
    echo '';
} else {
    echo '<td class="days" valign="top"><a class="monthnav" href="dayPlanner.php?d='.$day.'">'.$day.'</a></td>';
}
chaos