jump to navigation

Building a Monthly Calendar with Pear’s HTML_Table modul May 6, 2011

Posted by Tournas Dimitrios in PHP.
trackback

PEAR (PHP Extension and Application Repository ) has a big collection of high-quality , open-source code packages , that you can freely download and use in your own applications . PEAR  provides over 600 packages  covering everything from database access through to authentication , file handling , date formatting , networking and email .The repository is updated regularly , so visit the official web page for a full list of available packages .   To be more precisely , some packages have some license restrictions , so check first the licence before using the code on your applications .

I presumes that you are familiar with the PEAR package installer and how to install a package from the repository .

This article will demonstrate the HTML_Table package , it lets you generate html tables programmatically , rather than directly outputting HTML markup yourself . Changes that need to be done , at any time , is easilly  made through modifying attributes of the Html_Table modul , so you don’t have to fiddle with html code .

First install the package with : pear  install  Html_table  , and start coding .

The API for this module can be read here >>>

A practical example :

< ? p h p
require_once "HTML/Table.php";
//Select your Time zone: http://www.php.net/manual/en/timezones.php
date_default_timezone_set('Europe/Athens');
$tcaption = "</pre>
<h2>The days of this month are :</h2>
<pre>" ;
$weekDays = array('Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' ,
					'Thursday' , 'Friday' , 'Saterday');
$data = array();
$data['numDays'] = date('t');
$data['curDay'] = date('j');
$data['curMonth'] = date('F');
$data['firstDay'] = date('w', strtotime('1' . $data['curMonth']));
$tAttributes = array( 'border' => '5' , 'BORDERCOLOR' => 'red' );
$dateTable = new HTML_Table($tAttributes);
$dateTable->setAutoGrow(true);
$dateTable->setAutoFill('');
for($i=0 ; $i < count($weekDays) ; $i++) {
 $dateTable->setHeaderContents(0 , $i , $weekDays[$i]);
 }
 $row = 1 ;
 $col = $data['firstDay'];

 for ($i=1; $i<$data['numDays']; $i++) {
 $dateTable->setCellContents($row , $col , $i);
 if ($i == $data['curDay']) {
 $todayAttrs = array('bgcolor' => '#00ff00');
 $dateTable->setCellAttributes($row , $col , $todayAttrs);
 }
 $col++;
 IF ($col>6) {
  $row++;
  $col =0;
  }
  }
  $weekendAttrs = array('bgcolor' => '#cccccc');
  $dateTable->setColAttributes( 0 , $weekendAttrs);
  $dateTable->setColAttributes(6 , $weekendAttrs);
  $dateTable->setCaption($tcaption.$data['curMonth']);
  $dateTable->toHTML();

  ? >

The final result on our browser :


..

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s