Jump to content

Help troble shooting my code::


bobby317

Recommended Posts

Ok I am working on an event calendar and am getting close I think.  Right now I am having trouble creating a statement to evaluate the date being created by my script with the dates stored in my database.

 

This is my thinking I beilive it should be an if else statement If date being created = date stored in database created the date on the calander with the event name else just create the date.

 

The statement I have now dose not seem to be working it skips the if part of the statement and go's to the else:

 

Not been able to get much response on this subject donno If I am not explaining my self well or what.

 

Also I would like to know how to print my results of my query so I can make sure that is right I assum it is running it  becuase I use and if else statement to run it and it dose not come back saying could not run.

 

The calendar is created just no events are added.

 

Here is my current code

 

<?php

//CSS style
print '<link href="eventMain.css" rel="stylesheet" type="text/css" />';

//This gets today's date  
$date = time ();  

//This puts the day, month, and year in seperate variables  
$day = date('d', $date);  
$month = date('m', $date);  
$year = date('Y', $date);  

//Here we generate the first day of the month  
$first_day = mktime(0,0,0,$month, 1, $year);   

//This gets us the month name  
$title = date('F', $first_day);

//Here we find out what day of the week the first day of the month falls on 
$day_of_week = date('D', $first_day);   

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero 
switch($day_of_week){  
case "Sun": $blank = 0; break;  
case "Mon": $blank = 1; break;  
case "Tue": $blank = 2; break;  
case "Wed": $blank = 3; break;  
case "Thu": $blank = 4; break;  
case "Fri": $blank = 5; break;  
case "Sat": $blank = 6; break;  
}  

//We then determine how many days are in the current month 
$days_in_month = cal_days_in_month(0, $month, $year);

//Database Connection
$dbc = mysql_connect('rwddesign.com:****', 'rwddesi1_*****', '******');
   mysql_select_db('rwddesi1_****');
   
//Query to pull all events from event table:
$query = "SELECT * FROM events WHERE date='$year-$month-$day_num'";

//Here we start building the table heads  
echo "<table>"; 
echo "<tr><th colspan=\"7\" class=\"title\"> $title $year </th></tr>"; 
echo '<tr><td class="dayOfWeek">Sunday</td><td class="dayOfWeek">Monday</td><td class="dayOfWeek">Tuesday</td><td class="dayOfWeek">Wendsday</td><td class="dayOfWeek">Thursday</td><td class="dayOfWeek">Friday</td><td class="dayOfWeek">Saturday</td></tr>';  

//This counts the days in the week, up to 7 
$day_count = 1;  echo "<tr>";

//first we take care of those blank days 
while ( $blank > 0 )  {  
echo "<td> </td>";  
$blank = $blank-1;  
$day_count++; 
}

//sets the first day of the month to 1  
$day_num = 1;  

while ( $day_num <= $days_in_month) {

if ($r = mysql_query($query)) {     //run the query.

		//Retrieve every record:
		while ($row = mysql_fetch_array($r)) {
			$new_date = "$year-$month-$day_num";	
			$fetch_date = "$row=['date']";
		}

} else {
	print "can not run query";	
}

// Match calander dates with database dates
if ($new_date == $fetch_date) {
	print "<td>$day_num<br /> {$row['eventName']} </td>";
	$day_num++;  
	$day_count++;		
} else {
	print "<td>$day_num</td>";
	$day_num++;
	$day_count++;
}

//Make sure we start a new row every week 
if ($day_count > 7) { 
	echo "</tr><tr>"; 
	$day_count = 1; 
} 

}

//Finaly we finish out the table with some blank details if needed 
while ( $day_count >1 && $day_count <=7 )  {  
echo "<td> </td>";  
$day_count++;  
}  

//Close column and table
echo "</tr></table>";

?>

Link to comment
Share on other sites

The link $fetch_date is inherently wrong:

<?php
		//Retrieve every record:
		while ($row = mysql_fetch_array($r)) {
			$new_date = "$year-$month-$day_num";	
			$fetch_date = "$row=['date']";
		}
?>

 

Should be:

<?php
		//Retrieve every record:
		while ($row = mysql_fetch_array($r)) {
			$new_date = "$year-$month-$day_num";	
			$fetch_date = $row['date'];
		}
?>

 

To print the results of each row just use print_r();

<?php
		//Retrieve every record:
		while ($row = mysql_fetch_array($r)) {
			print_r($row);
			$new_date = "$year-$month-$day_num";	
			$fetch_date = $row['date'];
		}
?>

 

Hope this helps,

-cb-

Link to comment
Share on other sites

Ok I did what you said and that loads the page for a long time and gives me an error. I refreshed so I lost error if it comes up again will repost:

 

I have been trouble shooting this on my own and have come up with a few things.

 

This code here will build my calendar just how I want it to:

 

<?php

//CSS style
print '<link href="eventMain.css" rel="stylesheet" type="text/css" />';

//This gets today's date  
$date = time ();  

//This puts the day, month, and year in seperate variables  
$day = date('d', $date);  
$month = date('m', $date);  
$year = date('Y', $date);  

//Here we generate the first day of the month  
$first_day = mktime(0,0,0,$month, 1, $year);   

//This gets us the month name  
$title = date('F', $first_day);

//Here we find out what day of the week the first day of the month falls on 
$day_of_week = date('D', $first_day);   

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero 
switch($day_of_week){  
case "Sun": $blank = 0; break;  
case "Mon": $blank = 1; break;  
case "Tue": $blank = 2; break;  
case "Wed": $blank = 3; break;  
case "Thu": $blank = 4; break;  
case "Fri": $blank = 5; break;  
case "Sat": $blank = 6; break;  
}  

//We then determine how many days are in the current month 
$days_in_month = cal_days_in_month(0, $month, $year);

//Here we start building the table heads  
echo "<table>"; 
echo "<tr><th colspan=\"7\" class=\"title\"> $title $year </th></tr>"; 
echo '<tr><td class="dayOfWeek">Sunday</td><td class="dayOfWeek">Monday</td><td class="dayOfWeek">Tuesday</td><td class="dayOfWeek">Wendsday</td><td class="dayOfWeek">Thursday</td><td class="dayOfWeek">Friday</td><td class="dayOfWeek">Saturday</td></tr>';  

//This counts the days in the week, up to 7 
$day_count = 1;  echo "<tr>";

//first we take care of those blank days 
while ( $blank > 0 )  {  
echo "<td> </td>";  
$blank = $blank-1;  
$day_count++; 
}

//sets the first day of the month to 1  
$day_num = 1;  

//count up the days, untill we've done all of them in the month 
while ( $day_num <= $days_in_month )  { 

if (date('d') != $day_num) {
	echo "<td> $day_num </td>";  
	$day_num++;  
	$day_count++;
} else {
	echo "<td class=\"today\"> $day_num </td>";  
	$day_num++;  
	$day_count++;	
}
  

//Make sure we start a new row every week 
if ($day_count > 7) { 
	echo "</tr><tr>"; 
	$day_count = 1; 
} 
}

//Finaly we finish out the table with some blank details if needed 
while ( $day_count >1 && $day_count <=7 )  {  
echo "<td> </td>";  
$day_count++;  
}  

//Close column and table
echo "</tr></table>";

 

and This code will get the result I want from the database.

 

<?php 

$dbc = mysql_connect('rwddesign.com:3306', 'rwddesi1_********', '*****');
   mysql_select_db('rwddesi1_test');
   
$year = "2010";
$month = "6";
$day_num = "20";

$query = "SELECT * FROM events WHERE date='$year-$month-$day_num'";

if ($r = mysql_query($query)) {

while ($row = mysql_fetch_array($r)) {
	print "{$row['eventName']}";

}
} else {
print 'error' . mysql_error() . 'from' . $query . '';
}

?>

 

Any clue how you would combine the two to get my $row['eventName'] to print within the right date if it exhists?

Link to comment
Share on other sites

Ok I tried combining the two codes and now I am getting this error:

 

Fatal error: Maximum execution time of 30 seconds exceeded in /home/rwddesi1/public_html/1stCalendar.php on line 63

 

it seems to start working untill the first day of the month is to be printed.

 

any ideas?

 

<?php

//CSS style
print '<link href="eventMain.css" rel="stylesheet" type="text/css" />';

//This gets today's date  
$date = time ();  

//This puts the day, month, and year in seperate variables  
$day = date('d', $date);  
$month = date('m', $date);  
$year = date('Y', $date);  

//Here we generate the first day of the month  
$first_day = mktime(0,0,0,$month, 1, $year);   

//This gets us the month name  
$title = date('F', $first_day);

//Here we find out what day of the week the first day of the month falls on 
$day_of_week = date('D', $first_day);   

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero 
switch($day_of_week){  
case "Sun": $blank = 0; break;  
case "Mon": $blank = 1; break;  
case "Tue": $blank = 2; break;  
case "Wed": $blank = 3; break;  
case "Thu": $blank = 4; break;  
case "Fri": $blank = 5; break;  
case "Sat": $blank = 6; break;  
}  

//We then determine how many days are in the current month 
$days_in_month = cal_days_in_month(0, $month, $year);

//Here we start building the table heads  
echo "<table>"; 
echo "<tr><th colspan=\"7\" class=\"title\"> $title $year </th></tr>"; 
echo '<tr><td class="dayOfWeek">Sunday</td><td class="dayOfWeek">Monday</td><td class="dayOfWeek">Tuesday</td><td class="dayOfWeek">Wendsday</td><td class="dayOfWeek">Thursday</td><td class="dayOfWeek">Friday</td><td class="dayOfWeek">Saturday</td></tr>';  

//This counts the days in the week, up to 7 
$day_count = 1;  echo "<tr>";

//first we take care of those blank days 
while ( $blank > 0 )  {  
echo "<td> </td>";  
$blank = $blank-1;  
$day_count++; 
}

//sets the first day of the month to 1  
$day_num = 1;  

//count up the days, untill we've done all of them in the month 
while ( $day_num <= $days_in_month )  { 

$dbc = mysql_connect('rwddesign.com:3306', 'rwddesi1_*******', '******');
   mysql_select_db('rwddesi1_test');
   
$query = "SELECT * FROM events WHERE date='$year-$month-$day_num'";

if ($r = mysql_query($query)) {

while ($row = mysql_fetch_array($r)) {
	//print "{$row['eventName']}";
	echo "<td> $day_num {$row['eventName']} </td>";  
	$day_num++;  
	$day_count++;

}
} else {
print 'error' . mysql_error() . 'from' . $query . '';
}

mysql_close();

//Make sure we start a new row every week 
if ($day_count > 7) { 
	echo "</tr><tr>"; 
	$day_count = 1; 
} 
}

//Finaly we finish out the table with some blank details if needed 
while ( $day_count >1 && $day_count <=7 )  {  
echo "<td> </td>";  
$day_count++;  
}  

//Close column and table
echo "</tr></table>";

Link to comment
Share on other sites

You have an infinite loop:

//Finaly we finish out the table with some blank details if needed 
while ( $day_count >1 && $day_count <=7 )  {  
echo "<td> </td>";  
$day_count++;  
}

 

$day_count will never be less than 1 if it was already greater than 1 since your never subtracting anything from it, and $day_count is never changed.

 

Hopefully that will get you closer to an answer,

-cb-

Link to comment
Share on other sites

I don't belive that is it. My code is not reaching that point in the script and what that line is saying is that if $day_count is greater than one or equal to or less than 7 it will add a cell to the table and increse $day_count by one so once $day_count got to 8 it would quit running. I think it has to do with my loop that is running the query but still working on it. Anything else you can think of? Thanks

Link to comment
Share on other sites

Ok I am getting very very close  :D  to having this let me do some explaining.

 

It has something to do with my $day_num variable. Which is the varable that is printed to display the number of the date on the calendar I am also trying to use it to retive my info from the database like so:

 

$query = "SELECT * FROM events WHERE date='$year-$month-$day_num'";

 

which the above code dose nothing.

Now I have an event on the 20 of this mounth that is just a test. if I replace $day_num with 20 in my query it works and desplayes that on every date.

 

Now the $day_num variable prints the dates to the screen in the calendar and then increasing and keeps moving so it should change my $day_num variable to the write number but dose not seem to be working that way.

 

My question is why when I put the actualy number 20 in the query it workes but a variable set to 20 will not?

 

Also here is my code as it currently stands may have changed some things as I have been working through this.

 

<?php

//CSS style
print '<link href="eventMain.css" rel="stylesheet" type="text/css" />';

//This gets today's date  
$date = time ();  

//This puts the day, month, and year in seperate variables  
$day = date('d', $date);  
$month = date('m', $date);  
$year = date('Y', $date);  

//Here we generate the first day of the month  
$first_day = mktime(0,0,0,$month, 1, $year);   

//This gets us the month name  
$title = date('F', $first_day);

//Here we find out what day of the week the first day of the month falls on 
$day_of_week = date('D', $first_day);   

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero 
switch($day_of_week){  
case "Sun": $blank = 0; break;  
case "Mon": $blank = 1; break;  
case "Tue": $blank = 2; break;  
case "Wed": $blank = 3; break;  
case "Thu": $blank = 4; break;  
case "Fri": $blank = 5; break;  
case "Sat": $blank = 6; break;  
}  

//We then determine how many days are in the current month 
$days_in_month = cal_days_in_month(0, $month, $year);

//Here we start building the table heads  
echo "<table>"; 
echo "<tr><th colspan=\"7\" class=\"title\"> $title $year </th></tr>"; 
echo '<tr><td class="dayOfWeek">Sunday</td><td class="dayOfWeek">Monday</td><td class="dayOfWeek">Tuesday</td><td class="dayOfWeek">Wendsday</td><td class="dayOfWeek">Thursday</td><td class="dayOfWeek">Friday</td><td class="dayOfWeek">Saturday</td></tr>';  

//This counts the days in the week, up to 7 
$day_count = 1;  echo "<tr>";

//first we take care of those blank days 
while ( $blank > 0 )  {  
echo "<td> </td>";  
$blank = $blank-1;  
$day_count++; 
}

//sets the first day of the month to 1  
$day_num = 1;  

$dbc = mysql_connect('rwddesign.com:3306', 'rwddesi1_****', '******');
   mysql_select_db('rwddesi1_****');

$query = "SELECT * FROM events WHERE date='$year-$month-$day_num'";

//count up the days, untill we've done all of them in the month 
while ( $day_num <= $days_in_month )  { 

if ($r = mysql_query($query, $dbc)) {

while ($row = mysql_fetch_array($r, $dbc)) {
	$event_name = "{$row['eventName']}";

}
} else {
print 'error' . mysql_error() . 'from' . $query . '';
}


	echo "<td> $day_num $event_name </td>";  
	$day_num++;  
	$day_count++;

//Make sure we start a new row every week 
if ($day_count > 7) { 
	echo "</tr><tr>"; 
	$day_count = 1; 
} 
}

//Finaly we finish out the table with some blank details if needed 
while ( $day_count >1 && $day_count <=7 )  {  
echo "<td> </td>";  
$day_count++;  
}  

//Close column and table
echo "</tr></table>";

print "$event_name";

?>

 

Any advice? And plesae explain so I may learn. Thanks.

Link to comment
Share on other sites

Put an echo of the query to see what it really looks like:

<?php
$query = "SELECT * FROM events WHERE date='$year-$month-$day_num'";
echo $query . "<br>\n";
?>

 

Also, you have the query set outside the while loop, so it's never changing inside your loop, i.e. you're not looping through the events in your database.

 

Ken

Link to comment
Share on other sites

well crap the query seems to be working right and $day_num is changing to the right day in the query. I wonder if it dose not find a date on the first try if it stops running? Could that be it any ideas. I am going to go back and look at my while and see if it stops running if nothing comes up in the database for the 1st day of the month.

Link to comment
Share on other sites

Ok I finally got it. What I did was set the {$row['eventName']} to a variable then called that variable in my echo. I also had to move the part were I echo outside the second where statement and place it out side it and after also had to set the $event_name varable to blank at the end of the where statement. I have got the main functions working now just need to style and add some other featers that I am pritty familure with.

 

Thank you everyone could not have done it with out you.

 

Here is the code that worked:

 

<?php

//CSS style
print '<link href="eventMain.css" rel="stylesheet" type="text/css" />';

//This gets today's date  
$date = time ();  

//This puts the day, month, and year in seperate variables  
$day = date('d', $date);  
$month = date('m', $date);  
$year = date('Y', $date);  

//Here we generate the first day of the month  
$first_day = mktime(0,0,0,$month, 1, $year);   

//This gets us the month name  
$title = date('F', $first_day);

//Here we find out what day of the week the first day of the month falls on 
$day_of_week = date('D', $first_day);   

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero 
switch($day_of_week){  
case "Sun": $blank = 0; break;  
case "Mon": $blank = 1; break;  
case "Tue": $blank = 2; break;  
case "Wed": $blank = 3; break;  
case "Thu": $blank = 4; break;  
case "Fri": $blank = 5; break;  
case "Sat": $blank = 6; break;  
}  

//We then determine how many days are in the current month 
$days_in_month = cal_days_in_month(0, $month, $year);

//Here we start building the table heads  
echo "<table>"; 
echo "<tr><th colspan=\"7\" class=\"title\"> $title $year </th></tr>"; 
echo '<tr><td class="dayOfWeek">Sunday</td><td class="dayOfWeek">Monday</td><td class="dayOfWeek">Tuesday</td><td class="dayOfWeek">Wendsday</td><td class="dayOfWeek">Thursday</td><td class="dayOfWeek">Friday</td><td class="dayOfWeek">Saturday</td></tr>';  

//This counts the days in the week, up to 7 
$day_count = 1;  echo "<tr>";

//first we take care of those blank days 
while ( $blank > 0 )  {  
echo "<td> </td>";  
$blank = $blank-1;  
$day_count++; 
}

//sets the first day of the month to 1  
$day_num = 1;  

$dbc = mysql_connect('rwddesign.com:3306', 'rwddesi1_bobby31', '******');
   mysql_select_db('rwddesi1_test');



//count up the days, untill we've done all of them in the month 
while ( $day_num <= $days_in_month )  { 

$query = "SELECT * FROM events WHERE date='$year-$month-$day_num'";

if ($r = mysql_query($query, $dbc)) {

while ($row = mysql_fetch_array($r, $dbc)) {
	$event_name = "{$row['eventName']}";

}
} else {
print 'error' . mysql_error() . 'from' . $query . '';
}


	echo "<td> $day_num $event_name</td>";  
	$day_num++;  
	$day_count++;
	$event_name = "";

//Make sure we start a new row every week 
if ($day_count > 7) { 
	echo "</tr><tr>"; 
	$day_count = 1; 
} 
}

//Finaly we finish out the table with some blank details if needed 
while ( $day_count >1 && $day_count <=7 )  {  
echo "<td> </td>";  
$day_count++;  
}  

//Close column and table
echo "</tr></table>";

//print "$query";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.