Calendar

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

GKaplan
New php-forum User
New php-forum User
Posts: 6
Joined: Wed May 09, 2012 4:06 am

Calendar

Postby GKaplan » Sat Oct 26, 2013 8:51 am

Hi! I'm trying to make a calendar change the days background color when clicking on the day, but I'm having certain problems because it only changes the last day css. Heres the code to see if someone can help me!!

Code: Select all

<?php

   $date = time();
   
   $day = date('d', $date);
   $month = date('m', $date);
   $year = date('Y', $date);
   
   $first_day = mktime(0,0,0, $month, 1, $year);
   
   $title = date('F', $first_day);
   
   $day_of_week = date('D', $first_day);
   
   switch ($day_of_week) {
   
      case "Mon": $blank = 0; break;
      case "Tue": $blank = 1; break;
      case "Wed": $blank = 2; break;
      case "Thu": $blank = 3; break;
      case "Fri": $blank = 4; break;
      case "Sat": $blank = 5; break;
      case "Sun": $blank = 6; break;
   
   }
   
   $days_in_month = cal_days_in_month (0, $month, $year);
   
?>
   <style type="text/css">
      
         a {
            text-decoration: none;
            color: black;
            cursor: pointer;
         }
         
         table {
            background: #eee;
         }
         
         .change {
            background: red;
            color: white;
         }
      
   </style>
   

<?php   
   
   echo "<table border = 0 width = 253>";
   echo "<tr><th colspan = 60> $title $year </th></tr>";
   echo "<tr><td width = 42>Lu</td><td width = 42>Ma</td><td width = 42>Mi</td><td width = 42>Ju</td><td width = 42>Vi</td><td width = 42>Sa</td><td width = 42>Do</td></tr>";
   
   $day_count = 1;
   
   echo "<tr>";
   
   while ($blank > 0) {
   
      echo "<td></td>";
      $blank = $blank - 1;
      $day_count++;
   
   }
   
   $day_num = 1;
   $change = 1;
      
   while ($day_num <= $days_in_month) {
   
      ?>
      
         <script>
            var co = <?php echo json_encode($change); ?>;
            cn = co - 1;
            var ch = new Array();
            ch[cn] = co;
         </script>
         
      <?php
      
      echo "<td> <a id='$change' onclick='change()'>$day_num</a> </td>";
      $day_num++;
      $day_count++;
      $change++;
         
      if ($day_count > 7) {
      
         echo "</tr><tr>";
         $day_count = 1;
      
      }
   }
   
   while ($day_count > 1 && $day_count <= 7) {
   
      echo "<td> </td>";
      
      $day_count++;
   
   }
   
   echo "</tr></table>";
   
   
?>

<script type="text/javascript">
      
         function change () {
            
            document.getElementById(ch[cn]).style.cssText = 'background:red; color: white;';
                                    
         }
               
</script>

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Calendar

Postby johnj » Mon Oct 28, 2013 12:34 am

this is happening due to the way you have coded. Take a look at your code again and make sure all <td>s are affected.

GKaplan
New php-forum User
New php-forum User
Posts: 6
Joined: Wed May 09, 2012 4:06 am

Re: Calendar

Postby GKaplan » Mon Oct 28, 2013 2:53 am

OK, thanks it's allready solved!!


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron