HomeMore Examples
This example demonstrates how phpLens can be used to display date information using templates. In this
example, we display the diary entries retrieved by the SQL statement
SELECT cal_time,cal_name,cal_create_by,cal_date FROM webcal_entry in a calendar.
Because everything is written using components, code reuse and productivity is maximised.
/**
* (c) 2000, 2001 Remistech Sdn Bhd. All Rights Reserved.
* Developed by John Lim
* Licensed to Natsoft (M) Sdn Bhd to distribute and support. Refer to LICENSE document.
*/
define(PHPLENS_DIR,'.');
include_once(PHPLENS_DIR.'/phplens.inc.php');
include(PHPLENS_DIR.'/phplens-cal.inc.php');
session_start();
/*
Purpose: Return a phpLens object configured as a calendar.
$id = the unique id to identify this phplens object
$db = database connection
$sql = sql statement without the where clause
$datecolumn = column which contains the dates you want to match
$sortorder = sort order columns
*/
function lenscalendar($id,$db,$sql,$datecolumn,$sortorder)
{
$renderer = new PHPLensRenderCal($datecolumn);
$date_str1 = date('Ymd',mktime(0,0,0,$renderer->mon,1,$renderer->year));
$date_str2 = date('Ymd',mktime(0,0,0,$renderer->mon,GetMonthDays($renderer->year,$renderer->mon),$renderer->year));
$sql .= " where $datecolumn >= '$date_str1' and $datecolumn <= '$date_str2' order by $sortorder ";
$lens = new PHPLens($id,$db,$sql);
$lens->showGrid = false;
$lens->renderer = &$renderer;
$lens->pageSize = 999999;
return $lens;
}
/*-------------------------------------------------------------------------------------------------*\
Actual code begins
\*-------------------------------------------------------------------------------------------------*/
# connect to database
$db = &ADONewConnection('mysql');
if (!$db->PConnect('server','user','pwd','db')) print 'Error:'.$db->ErrorMsg();
# create phplens object for calendar
$lens = lenscalendar('mysqlcal2',$db,
"select cal_time,cal_name,cal_create_by,cal_date from webcal_entry",
'cal_date', # date column
'cal_date,cal_time' # sortorder
);
# the time in webcal is in binary decimal format, so we change it to HH:MM format.
$lens->powerLens=
'cal_time^=substr(str_pad({cal_time},6,"0",STR_PAD_LEFT),0,2).
":".substr(str_pad({cal_time},6,"0",STR_PAD_LEFT),2,2)';
$lens->Render();
$lens->Close();
/*-------------------------------------------------------------------------------------------------*\
Actual code ends
\*-------------------------------------------------------------------------------------------------*/
How To Display an SQL Query in a Month Calendar
Purpose: You want to display some data based on a date column
(in the example, the cal_date field).
1. Create files with the following structure:
./ex950.php # the file that contains the phplens code
./templates/calentry.tpl # template for 1 calendar entry (eg. 1 record)
./templates/calmonth.tpl # template for the monthly calender
./templates_c/
./config/
Note that the templates_c and config directories must be created.
2. You must define the following global variables in your source.
GLOBAL $lens_year,$lens_month;
These define the month and year to show. If these are undefined, then the
current year and month are used.
3. Then modify your sql statement to suit your database table structure.
4. Next modify calentry.tpl so that the database field names match a single
calendar entry.
5. Lastly configure the parameters passed to lenscalendar (see above source code comments):
$lens = lenscalendar($id, $db,$sql,$datecolumn,$sortorder);
The PHPLensRenderCal class
==========================
The following PHPLensRenderCal class is created used to render (generate)
the html for the calendar. Create a new instance of this class, with the
name of the SQL date column as the parameter to the constructor.
$renderer = new PHPLensRenderCal('cal_date');
Then create the phplens object, and pass the renderer variable to phplens.
$lens->renderer = $renderer;
The renderer can be controlled by setting the following class variables
defined below:
class PHPLensRenderCal {
var $dateCol; // Date Column to order by in SQL statement.
var $view = 'M'; // 'W' = weekly (untested), 'M' = monthly;
var $templateLayout = 'calmonth.tpl';
var $templateDetail ='calentry.tpl';
// the following values are set using $lens_year and $lens_month
var $mday; // day of the month to show (1-31)
var $wday; // day of the week to show (numeric)
var $mon; // month to show (1-12)
var $year; // year to show (4 digits)
var $month; // text name of month to show
:
:
}
VARIABLES IN SMARTY TEMPLATE
============================
{$MONTH_BEFORE} the previous month (1-12)
{$YEAR_BEFORE} the year of the previous month
{$MONTH_NEXT} the next month (1-12)
{$YEAR_NEXT} the year of the next month
{$MON} month to show (1-12)
{$MONTH} text name of month to show
{$YEAR} year to show (4 digits)
{$LENSDAY1} text of the first day of the week (eg. Sunday).
: Taken from phplens-lang.*.inc.php, $txtWeekDays.
:
{$LENSDAY7} text of last day of the week (eg. Saturday)
{$LENSDATENUM} holds the days of the month (eg. 1,2,3,4,5,6...31) formatted as html in an array
{$LENSDATETEXT} holds the messages in a 2 dimensional array. 1st dimension is the week (the row),
the 2nd is the day of the week (the col).
USEFUL FUNCTIONS
================
function GetMonthDays($year,$month)
Returns number of days given a year (4 digits) and a month (1-12)
Database Structure
Sample MySQL webcal_entry table creation script:
DROP TABLE IF EXISTS webcal_entry;
CREATE TABLE webcal_entry (
cal_id int(11) NOT NULL default '0',
cal_group_id int(11) default NULL,
cal_create_by varchar(25) NOT NULL default '',
cal_date int(11) NOT NULL default '0',
cal_time int(11) default NULL,
cal_mod_date int(11) default NULL,
cal_mod_time int(11) default NULL,
cal_duration int(11) NOT NULL default '0',
cal_priority int(11) default '2',
cal_type char(1) default 'E',
cal_access char(1) default 'P',
cal_name varchar(80) NOT NULL default '',
cal_description text,
PRIMARY KEY (cal_id),
KEY cal_name(cal_name),
UNIQUE KEY cal_id(cal_id),
KEY cal_date(cal_date),
KEY cal_create_by(cal_create_by)
) TYPE=ISAM PACK_KEYS=1;
# Data for September 2000
INSERT INTO webcal_entry VALUES (1,NULL,'Jack',20000919,143000,20000919,142942,30,2,'E','P','Meet Alvin','from Digi'),
(2,NULL,'Jack',20000923,193000,20000919,143051,0,2,'E','P','Hooi Tyng\'s wedding','Equatorial Melaka'),
(3,NULL,'Mary',20000920,110000,20000919,143832,60,2,'E','P','EA FORMS for YEE,GOH','Print EA FORMS for YFS and GSH'),
(4,NULL,'Gerard',20000922,193000,20000919,151126,0,2,'E','R','Cherlyn meeting','Commonwealth Club'),
(5,NULL,'Mary',20000924,190000,20000919,151748,0,2,'E','P','Andrew MG','at house');