Now With Web 2.0!

Archive for February 2005

Feb
27
2005
I was recently faced with the daunting task of creating a calendar using PHP and honestly I had no clue where to start. I looked online for various tutorials and walkthroughs but found none suitable to my needs so I did what any self respecting Developer does in this position. I freaked out. After a good freak out session I pulled myself together and just took bits and pieces of code that I knew I needed and started from there. The following is a “for Dummies” explanation of how I got a calendar working and some options available to you for expansion and modification. I’ve included links to the PHP reference manual from functions that may not be so common. I built a separate function for my calendar because I was sure I was going to need it in more than one place, I named it buildcalendar and put it into an include file as includes/calendar.inc.php. In order to show the current month, the function needs todays day, month and year so it ended up reading like this… function buildcalendar($today,$month,$year)
{

}
So before I call this function I need to set the day, month and year to $today,$month and $year respectively. This can easily be acheived using the getdate() function as shown below in my main file calendar.php $date = getdate();
$today = $date[’mday’];
$month = $date[’mon’];
$year = $date[’year’];
The mday, mon and year pieces are calling to the array that getdate() sets. Ok, so now we have our function and the variables it needs to work but what goes into the function? I will first provide the code then explain it… function buildcalendar($today,$month,$year) {
     $daysOfWeek = array(’Su’,'Mo’,'Tu’,'We’,'Th’,'Fr’,'Sa’);
     $firstDayOfMonth = mktime(0,0,0,$month,1,$year);
     $numberDays = date(’t',$firstDayOfMonth);
     $dateComponents = getdate($firstDayOfMonth);
     $monthName = $dateComponents[’month’];
     $dayOfWeek = $dateComponents[’wday’];
     
     $calendar = "<H6>$monthName, $year </H6>";
     $calendar .= "<table class=’calendar’>";
     $calendar .= "<tr>";
     
     foreach($daysOfWeek as $day) {
          $calendar .= "<th class=’header’>$day</th>";
     }
     $currentDay = 1;
     
     $calendar .= "</tr><tr>";
     
     if ($dayOfWeek > 0) {
          $calendar .= "<td colspan=’$dayOfWeek’>&nbsp;</td>";
     }
     
     while ($currentDay <= $numberDays) {
          if ($dayOfWeek == 7) {
               $dayOfWeek = 0;
               $calendar .= "</tr><tr>";
          }
          $calendar .= "<td class=’day’>$currentDay</td>n";
          $currentDay++;
          $dayOfWeek++;
     }
     if ($dayOfWeek != 7) {
          $remainingDays = 7 - $dayOfWeek;
          $calendar .= "<td colspan=’$remainingDays’>&nbsp;</td>";
     }
     $calendar .= "</table>";
     return $calendar;
}
On the first line I created an array with the day abbreviations, next I wanted to find the first day of the current month using the mktime function using the values I set within calendar.php. From there I needed to get the total number of days in the current month, for this I used the date function. In order to find out more about the first day of the month, I called the getdate function again as I did before. In the next 2 lines I set the month name and the day of the week that this first day fell on. This first $calendar sets the header text which is the month and year. The next two set up the table that the calendar will be displayed in. I then used a foreach function to run through the weekday names I set before and put them in the first row of cells. Here’s where the real fun begins! I started a counter $currentDay and set it to 1, then I started a new table row in $calendar and set a colspan in the first <td> tag set to $dayOfWeek so that the calendar displays the days evenly. Next is a fairly large while loop that first finds if the day is the last (if ($dayOfWeek == 7)) and if it is it will start a new row. If not I set another day in $calendar. Finally, I incremented the $dayOfWeek and $currentDay for the next go around. When all the days in the month are set, I then check to see if the last day is still in the middle of the week(if ($dayOfWeek != 7)) and if so made a final cell that spanned the remaining days ($remainingDays = 7 - $dayOfWeek;). To finish it off I closed the table and set the return $calendar; to return the calendar table. Finally, in calendar.php after the code to find the current day put in your include to the function file and a call to the function itself. Your final file shoul look like this… <html>
     <head>
          <link rel=”stylesheet” type=”text/css” href=”includes/calendar.css” />
     </head>
     <body>
          <?php
               $date = getdate();<br />
               $today = $date[’mday’];<br />
               $month = $date[’mon’];<br />
               $year = $date[’year’];<br /><br />
               include “includes/calendar.inc.php”;<br />
               echo "<div align=’center’>". buildcalendar($today,$month,$year,$dateArray)." </div>";<br />
          ?>
     </body>
</html>
Following is the CSS I made to correspond to the calendar, I called it includes/calendar.css h6 {
     font-family:arial,helvetica;
     font-size:11px;
     color: black;
     font-weight: bold;
     text-align:center;
}
th {
     background-color:#ccc;
}

.calendar {
     font-family:arial,helvetica;
     font-size:11px;
     color: white;

     background-color: #dedede;
     border-color: #000000;
     border-style: solid;
     border-width: 1px;
}
.day {
     background-color: #808080;
     border-color: #000000;
     border-style: solid;
     border-width: 1px;
     text-align: center;
}
.header {
     background-color: #ccc;
     border-color: #000000;
     border-style: solid;
     border-width: 1px;
     color:#000000;
}

Feb
21
2005
Here I am again, blabbering senselessly about typography. Sorry can’t help it, I spent my entire undergrad thesis on typrography and its effectiveness in the mass media. You must be thinking I’m obsessed by it, unfortunately I am. Last time I mentioned I would get into using typography with cutting edge photography. I will get right to it and keep it short. If you are walking down a busy street, driving on the highway, or riding public transportation, you must have come across numerous amount of ads using mainly photography and the right amount of content to send a message across. Let’s face it, all of us, or most of us have a short attention span, including myself. I can get sidetracked in exactly 16 seconds, yes, I timed myself. Advertisers are faced with the daunting task of publishing ads that will have an immediate effect. Therefore, they use cutting edge photography with carefully picked type sets for content. Pick up a magazine and check out ads for Absolut Vodka, Nike, Adidas, Got Milk, and many others. In the future, be aware of your surroundings and try to pick up what I just talked about. I’ll sign off for now, and return with a new topic regarding typography in the near future.
Feb
14
2005
In my browsing I found this interesting article on how one person works through his projects. I think it’s pretty neat if for nothing else than to compare notes. One Way to Design
Feb
09
2005
The PHP Security Consortium (PHPSC) is an international group of PHP experts dedicated to promoting secure programming practices within the PHP community. http://phpsec.org/
Feb
05
2005
Any Search Engine Optimizer worth a dime should know everything in this article already but I decided to post this anyway for those of you unfamiliar with the ways of the search engine world. Very good read, sums it up pretty well… Basics Of Search Engine Optimisation
Feb
04
2005
This is an experimental alpha feature that is still under development on the new MSN Search Beta. Here are some step-by-step instructions:
  • run your web or news search on the MSN Search Beta
  • add the text ‘&format=rss’ to the end of the url
  • copy the full url into your RSS Reader.
Here is a sample web search url: http://beta.search.msn.com/results.aspx?q=tsunami+relief&format=rss Here is a sample news search url: http://beta.search.msn.com/news/results.aspx?q=tsunami+relief&format=rss Obviously this new functionality holds some amazing potential, especially for SEO. Imagine having dynamic seaerch results on your website, the possibilities are endless! Please note, however, that this is still in it’s experimental stage so things will no doubt change. I also read that if it’s abused in any way it will be pulled so be careful what you do.