Hi there,
I am making recipe type website. My problem is, i could like to calendar option to this site. I came out with something, but it disyplays entire year.
Is there any possibilty that i could i have only one mont, than move withing monts with nex and prev text links.
I am linking to dabase table called recipes, ( id and date )
my code is :
<?php
$db_host = "localhost";
$db_name = "**";
$db_username = "***";
$db_passwd = "***";
$db_table_name = "***";
$db_column_name = "date";
$db_type = 0; // 0=simple 1=complex
//
// How many months across to display?
//
$num_columns_static = 3;
//
// Formatting variables -- change to your heart's content!
//
$month_table_header = "\n<table border=0 cellspacing=5>";
$month_row_header = "\n<tr>";
$month_td_tag = "<td valign=top>";
$cal_table_tag = "\n<table width=140 border=0 bgcolor=#DDDDDD>";
$cal_mon_tr_tag = "\n<tr bgcolor=#D3DCE3>";
$cal_dayname_tr_tag = "\n<tr bgcolor=#CCCCCC>";
$cal_day_tr_tag = "\n<tr>";
$cal_day_opentd = "<td align=center>";
$cal_day_closetd = "</td>";
$cal_event_opentd = "<td align=center bgcolor=#c0c0ff><b>";
$cal_event_closetd = "</b></td>";
$event_table_opentag = "<table cellspacing=0 cellpadding=4 border=0>\n";
$event_table_closetag = "</table>\n";
$event_tr_opentag = "\t<tr>\n";
$event_tr_closetag = "\t</tr>\n";
$event_th_opentag = "\t\t<th bgcolor=#D3DCE3>\n";
$event_th_closetag = "\t\t</th>\n";
$event_td_opentag = "\t\t<td bgcolor=#CCCCCC>\n";
$event_td_closetag = "\t\t</td>\n";
//
// Connection to the database and selection of the correct db.
//
$db_link = mysql_connect($db_host, $db_username, $db_passwd);
$db = mysql_select_db($db_name, $db_link);
//
// Function fixlen($string) adds a zero to the front of the string if the
// string length does not equal 2
//
function fixlen($fixit) {
if (strlen($fixit)<2) {
return "0$fixit";
} else {
return $fixit;
}
}
//
// Function getcal($year) will generate a calendar for the given year.
//
function getcal($year) {
global $num_columns_static,$month_table_header,$month_row_header,$month_td_tag;
$num_of_months = 12;
$count_up = 1;
echo $month_table_header;
while ($count_up<=$num_of_months) {
echo $month_row_header;
$num_columns = $num_columns_static;
while (($num_columns>0) && ($count_up<=$num_of_months)) {
echo $month_td_tag;
echo gen_month($year,$count_up);
echo "</td>";
$count_up++;
$num_columns--;
}
echo "</tr>";
}
echo "\n</table>";
}
//
// Function gen_month($year,$month) will generate a month for
// the year and month given.
//
function gen_month($year,$month) {
global $cal_table_tag,$cal_mon_tr_tag,$cal_dayname_tr_tag,$cal_day_tr_tag;
global $cal_event_opentd, $cal_event_closetd;
global $cal_day_opentd, $cal_day_closetd;
$mon_date = getdate(mktime(0,0,0,$month,1,$year));
$mon_name = $mon_date[month];
$mon_total_days = strftime( "%d",mktime(0,0,0,$month+1,0,$year));
echo $cal_table_tag;
echo $cal_mon_tr_tag . "<td colspan=7 align=center><b>$mon_name $year</b></td></tr>";
echo $cal_dayname_tr_tag . "<td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td></tr>";
$day_count = 1;
while ($day_count<=($mon_total_days-7)) {
$fweek = getdate(mktime(0,0,0,$month,$day_count,$year));
if ($day_count<=7) {
$sb_cnt_td = $fweek[wday];
$sb_cnt = $fweek[wday];
$sb_max = 7;
$p_line = $cal_day_tr_tag;
while ($sb_cnt_td>=1) {
$p_line .= "\n<td> </td>";
$sb_cnt_td--;
}
while ($sb_cnt<$sb_max) {
//
// The $urldisp is what shows the number of the
// day. the gen_day_url() function makes the
// number linkable if there is data in a
// certain table for that day.
//
//
// $find_data is used in the SQL query in gen_day_url()
// If there is any occurrence of $find_data in
// $db_column_name, then a link is generated.
//
$find_data = $fweek[year] ."-". fixlen($fweek[mon]) ."-". fixlen($day_count);
$urldisp = gen_day_url($find_data,$day_count);
if (strlen($urldisp) > 2)
$p_line .= $cal_event_opentd . $urldisp . $cal_event_closetd;
else
$p_line .= $cal_day_opentd . $urldisp . $cal_day_closetd;
$sb_cnt++;
$day_count++;
}
echo "$p_line</tr>";
} else {
$weekday_count = 1;
$p_line = $cal_day_tr_tag;
while ($weekday_count<=7) {
//
// $find_data is used in the SQL query in gen_day_url()
// If there is any occurrence of $find_data in
// $db_column_name, then a link is generated.
//
$find_data = $fweek[year] ."-". fixlen($fweek[mon]) ."-". fixlen($day_count);
$urldisp = gen_day_url($find_data,$day_count);
if (strlen($urldisp) > 2)
$p_line .= $cal_event_opentd . $urldisp . $cal_event_closetd;
else
$p_line .= $cal_day_opentd . $urldisp . $cal_day_closetd;
$weekday_count++;
$day_count++;
}
$p_line .= "</tr>";
echo $p_line;
}
}
$sb_cnt_td = 7-($mon_total_days - $day_count);
$p_line = $cal_day_tr_tag;
while ($day_count<=$mon_total_days) {
//
// $find_data is used in the SQL query in gen_day_url()
// If there is any occurrence of $find_data in
// $db_column_name, then a link is generated.
//
$find_data = $fweek[year] ."-". fixlen($fweek[mon]) ."-". fixlen($day_count);
$urldisp = gen_day_url($find_data,$day_count);
if (strlen($urldisp) > 2)
$p_line .= $cal_event_opentd . $urldisp . $cal_event_closetd;
else
$p_line .= $cal_day_opentd . $urldisp . $cal_day_closetd;
$day_count++;
}
while ($sb_cnt_td>1) {
$p_line .= "\n<td> </td>";
$sb_cnt_td--;
}
echo "$p_line</tr>";
echo "</table>";
}
//
// Function gen_day_url($day,$month,$year) makes either a
// a plain text day number, or a linked number, depending on if a record
// exists for that day.
//
function gen_day_url($data,$day) {
global $db_table_name,$db_column_name,$db_host, $db_link;
if (strlen($db_host)>0) {
$qrsql = "SELECT * FROM $db_table_name WHERE ($db_column_name='$data')";
$qr_ret = mysql_query($qrsql, $db_link);
$qr_num=mysql_num_rows($qr_ret);
if ($qr_num>0) {
//
// Here is where you put the link to the action
// you want to perform if there is data in the
// table.
//
$row=mysql_fetch_array($qr_ret);
return "<a href=\"listd.php?date=$data\" alt=\"$qr_num Recipes\" title=\"$qr_num Recipes\">$day</a>";
} else {
//
// Returns just the number of the day if there
// is no data in the table for that date
//
return $day;
}
} else {
//
// This returns just the number of the day, because
// the $db_host variable is empty (the user does not
// want to use the db functions of the snippet.
//
return $day;
}
}
?>
<?
// Here is the call!!!
$year = 2005;
getcal($year);
?>
So when user clikck on the date, he will see total amount of recipes for that day, and possibility to clikc on that day, will take him to news page called listd.php ( "listd.php?date=2003-05-07"). All recipes for this date.
Is this possible ? If anyone of you have better solution i am open for suggestions.
Thank you all for your help,