Jump to content

Calendar Class...need help with mysql part


bschultz

Recommended Posts

I found a calendar class online (http://davidwalsh.name) but I need some help adding in the database call.

 

The writer left a comment stating where the db call should go...

 

<?php
/* draws a calendar */
function draw_calendar($month,$year){

/* draw table */
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

/* table headings */
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();

/* row for week one */
$calendar.= '<tr class="calendar-row">';

/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
	$calendar.= '<td class="calendar-day-np"> </td>';
	$days_in_this_week++;
endfor;

/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
	$calendar.= '<td class="calendar-day">';
		/* add in the day number */
		$calendar.= '<div class="day-number">'.$list_day.'</div>';




		/** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/





		$calendar.= str_repeat('<p> </p>',2);

	$calendar.= '</td>';
	if($running_day == 6):
		$calendar.= '</tr>';
		if(($day_counter+1) != $days_in_month):
			$calendar.= '<tr class="calendar-row">';
		endif;
		$running_day = -1;
		$days_in_this_week = 0;
	endif;
	$days_in_this_week++; $running_day++; $day_counter++;
endfor;

/* finish the rest of the days in the week */
if($days_in_this_week < :
	for($x = 1; $x <= (8 - $days_in_this_week); $x++):
		$calendar.= '<td class="calendar-day-np"> </td>';
	endfor;
endif;

/* final row */
$calendar.= '</tr>';

/* end the table */
$calendar.= '</table>';

/* all done, return result */
return $calendar;
}


/* sample usages */
echo '<h2>May 2010</h2>';
echo draw_calendar(5,2010);

?>

 

I tried using the code I was using on my old calendar:

 

<?php
$sql = "SELECT * FROM calendar WHERE date='$year-$month-$list_day'";           
$rs = mysql_query($sql,$dbc);  
$matches = 0; 
while ($row = mysql_fetch_assoc($rs))  {
$matches++; 
echo "<img src='/images/line.gif' alt='line' /><br />";
echo "<span class='style4'>$row[time]"."  <br /><strong>$row[sport]"." $row[visitor]"." at "."$row[home]</strong><br />";
echo "$row[ump1]  "."$row[ump2]  "."$row[ump3]  "."$row[ump4]  "."$row[ump5]</span><br />";
echo "<span class='field'>$row[field]</span><br>"."<span class='notes'>$row[notes]</span><br>";
}  
if (! $matches) { 
echo "<img src='/images/line.gif' alt='line' /><br />"; } echo ""; 

$sql = "SELECT * FROM vacation WHERE date='$year-$month-$list_day'";          
$rs = mysql_query($sql,$dbc);  
$matches = 0; 
while ($row = mysql_fetch_assoc($rs))  {
$matches++; 
echo "<br /><span class='style5'>$row[umpire] Off</span>";
}  
if (! $matches) { 
echo "<span class='style5'> </span>"; } echo "<br />"; 
?>

 

but it just printed the results at the top of the page...and not in each individual day's cell in the table.  I'm sure it's because I'm "echo"...ing results in the middle of a function...but I don't know how else to do it.

 

Can someone please point me in the right direction?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/200505-calendar-classneed-help-with-mysql-part/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.