Jump to content

[SOLVED] Looping Display problem


Drewser33

Recommended Posts

I used some code I found on the internet to make a calendar, which the calendar part is working but now I want to insert information into the cells/days that are created by the calendar.  Below is the code I am using now but it is only displaying the last database rows information.  Is it possible to loop inside the loop? And then only display it once?  Because if I change my code to have the while include the 'td' I get two displays of everyday.  THANKS!!

 

<?php

if(isset($_GET['prm']))
{
$prm = $_GET['prm'];
$chm = $_GET['chm'];
$m=$prm+$chm;
}
else
{
$m= date("m");
}
$month = date('F');
$d= date("d");     // Finds today's date
$y= date("Y");     // Finds today's year
$no_of_days = date('t',mktime(0,0,0,$m,1,$y));	// This is to calculate number of days in a month
$mn=date('M',mktime(0,0,0,$m,1,$y)); // Month is calculated to display at the top of the calendar
$yn=date('Y',mktime(0,0,0,$m,1,$y)); // Year is calculated to display at the top of the calendar
$j= date('w',mktime(0,0,0,$m,1,$y)); // This will calculate the week day of the first day of the month
for($k=1; $k<=$j; $k++)
{ 
$adj .="<td>&nbsp</td>";
}
echo $string;
/// Starting of top line showing name of the days of the week

echo " <table border='1' bordercolor='#FFFF00' cellspacing='0' cellpadding='0' align=center>

<tr><td>";

echo "<table cellspacing='0' cellpadding='0' align=center width='750' border='1'><td align=center bgcolor='#ffff00'><font size='3' face='Tahoma'> <a href='mycalendar.php?prm=$m&chm=-1'><</a> </td><td colspan=5 align=center bgcolor='#ffff00'><font size='3' face='Tahoma'>$mn $yn </td><td align=center bgcolor='#ffff00'><font size='3' face='Tahoma'> <a href='mycalendar.php?prm=$m&chm=1'>></a> </td></tr><tr>";

echo "<td width=14.2%><font size='3' face='Tahoma'><b>Sun</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Mon</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Tue</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Wed</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Thu</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Fri</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Sat</b></font></td></tr><tr>";

////// End of the top line showing name of the days of the week//////////

//////// Starting of the days//////////


for($i=1;$i<=$no_of_days;$i++)
{
$daytotime = ($i.' '.$mn.' '.$yn);
$string = strtotime($daytotime);
$query = "SELECT * FROM calendar";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
	$t = $row['startDate'];
	$u = $row['endDate'];
	if($string >= $t && $string <= $u)
	{
		$infonext = $row['title'];
	}else
	{
		$infonext = '';
	}
}
echo $adj."<td valign=top height=100><font size='2' face='Tahoma'>$i<br></font><font size='1' color='#ff0000'>$infonext"; // This will display the date inside the calendar cell;
echo " </font></td>";
$adj='';
$j ++;
if($j==7)
{
	echo "</tr><tr>";
	$j=0;
}	
}
//echo $infonext;
?>

Link to comment
https://forums.phpfreaks.com/topic/121875-solved-looping-display-problem/
Share on other sites

side note...save yourself time and use Google Calendar. In there, create a calendar, then go to the settings for it. There is a spot where it gives you code to 'embed it in a website'. Then just use Google Calendar to keep the stuff up to date.

its because you are echoing outside your collection of the query results loop so those vars you set are going to be equal to the last value when echoed.

 

 

store those values in an array in the while($row) part and then use that array to produce in the fields.

 

 

At rhodesa: google is good but a lot of times you don't get the flexibility you want.

 

 

<?php
while($row = mysql_fetch_assoc($r)){
$events[$row['eventid']]['Date'] = $row['Date'];

$events[$row['eventid']]['Location'] = $row['Location'];

$events[$row['eventid']]['Time'] = $row['Time'];
}

while(ECHO CALENDAR){
#find matches in array $events for the current date stamp and if match echo it

Thanks for the help, but I couldn't get it working with the arrays, but this is how I got it without using arrays ->

 

<?php
include 'includes/config.php';
include 'includes/opendb.php';

if(isset($_GET['prm']))
{
$prm = $_GET['prm'];
$chm = $_GET['chm'];
$m=$prm+$chm;
}
else
{
$m= date("m");
}
$month = date('F');
$d= date("d");     // Finds today's date
$y= date("Y");     // Finds today's year
$no_of_days = date('t',mktime(0,0,0,$m,1,$y));	// This is to calculate number of days in a month
$mn=date('M',mktime(0,0,0,$m,1,$y)); // Month is calculated to display at the top of the calendar
$yn=date('Y',mktime(0,0,0,$m,1,$y)); // Year is calculated to display at the top of the calendar
$j= date('w',mktime(0,0,0,$m,1,$y)); // This will calculate the week day of the first day of the month
for($k=1; $k<=$j; $k++)
{ 
$adj .="<td>&nbsp</td>";
}
echo $string;
/// Starting of top line showing name of the days of the week

echo " <table border='1' bordercolor='#FFFF00' cellspacing='0' cellpadding='0' align=center>

<tr><td>";

echo "<table cellspacing='0' cellpadding='0' align=center width='750' border='1'><td align=center bgcolor='#ffff00'><font size='3' face='Tahoma'> <a href='mycalendar.php?prm=$m&chm=-1'><</a> </td><td colspan=5 align=center bgcolor='#ffff00'><font size='3' face='Tahoma'>$mn $yn </td><td align=center bgcolor='#ffff00'><font size='3' face='Tahoma'> <a href='mycalendar.php?prm=$m&chm=1'>></a> </td></tr><tr>";

echo "<td width=14.2%><font size='3' face='Tahoma'><b>Sun</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Mon</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Tue</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Wed</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Thu</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Fri</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Sat</b></font></td></tr><tr>";

////// End of the top line showing name of the days of the week//////////

//////// Starting of the days//////////

for($i=1;$i<=$no_of_days;$i++)
{
$daytotime = ($i.' '.$mn.' '.$yn);	
$string = strtotime($daytotime);
echo $adj."<td valign=top height=100><font size='2' face='Tahoma'>$i<br></font><font size='1' color='#ff0000'>";
$query = "SELECT * FROM calendar";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
	$startDate = $row['startDate'];
	$endDate = $row['endDate'];
	$title = $row['title'];
	if($string >= $startDate && $string <= $endDate) {
		echo $title."<br />"; // This will display the date inside the calendar cell;
	}
}
echo " </font></td>";
$adj='';
$j ++;
if($j==7)
{
	echo "</tr><tr>";
	$j=0;
}	
}
?>

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.