Jump to content

JavaScript causing infinite loop?


hellonoko

Recommended Posts

My below todo list code displays all the todo items and children from a DB.

 

I have just added in a live javascript countdown so you can set a date for a todo item and have a live countdown of how long you have left to complete the todo item.

 

The countdown script that pulls the date from the DB and creates the countdown <div> seems to work fine. But it throws the php into a infinite loop that starts echoing out line after line of table rows.

 

Display Code: All the extra table rows are echoed out right after the

include "menu.php"

wich is just a button to add a new todo item.

The include for the timer is farther down at line 131.

 

<link rel="shortcut icon" href="images/favicon.ico" >

<?php


error_reporting(E_All);

session_start();

session_register('sort');

if ($_session['sort'] == NULL)
{
	$_session['sort'] = $HTTP_GET_VARS[sort];
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Idea Box</title>

<style type="text/css">
<!--
td,th {
font-size: 10px;
font-family: Arial, Helvetica, sans-serif;
color: #FFFFFF;
}
body {
font-size: 10px;
font-family: Arial, Helvetica, sans-serif;
}

a:link {
color: #FFFFFF;
}
a:visited {
color: #FFFFFF;
}
a:hover {
color: #666666;
}
a:active {
color: #FFFFFF;
}

.menu_text {color: #000000}
-->
</style>

</head>

<?php

include 'dbconnect.php';
include 'menu.php';

if ( $_session['sort'] == NULL)
{
	$_session['sort'] = "title_ascending";
}

if ( $_session['sort'] == "title_ascending" )
{
	$query = "SELECT * FROM ideas ORDER BY rank ASC";
}
else if ( $_session['sort'] = "title_descending")
{
	$query = "SELECT * FROM ideas ORDER BY rank DESC";
}

//echo $_session['sort'];

$result = mysql_query($query);
$rows = mysql_num_rows($result);

echo	"<table width='100%' border='0' cellspacing='1'>";

for ($i=0; $i <$rows; $i++)
{
	$row = mysql_fetch_array($result);

	$num_rows = mysql_num_rows($result);

	echo	"<tr>";

	/// changes the state of viewing childen IF the idea has children

	if ( $row[haschildren] == TRUE)
	{
		if ( $row[state] == FALSE)
		{
			echo		"<form action='state.php' method='get'>";
			echo			"<input name='id' type='hidden' value=".$row[id]." />";
			echo			"<input name='state' type='hidden' value='open' />";
			echo			"<td width='16' align='center' valign='middle' bgcolor='#ffffff'><input type='image' src='images/right.png' alt='Open'></td>";
			echo		"</form";
		}
		if ( $row[state] == TRUE)
		{
			echo		"<form action='state.php' method='get'>";
			echo			"<input name='id' type='hidden' value=".$row[id]." />";
			echo			"<input name='state' type='hidden' value='close' />";
			echo			"<td width='16' align='middle' valign='middle' bgcolor='#ffffff'><input type='image' src='images/down.png' alt='Close'></td>";
			echo		"</form";
		}

	}

	if ( $row[haschildren] == FALSE)
	{
		echo		"<td width='16' valign='top' bgcolor='#ffffff'></td>";
	}

	// add sub idea button

	echo		"<form action='add_child.php' method='get'>";
	echo			"<input name='id' type='hidden' value=".$row[id]." />";
	echo			"<td width='16' align='center' valign='middle' bgcolor='#ffffff'><input type='image' src='images/add.png' alt='Add Child'></td>";
	echo		"</form";

	//echos the title and content of the idea

	echo		"<td width='180' valign='top' bgcolor='#666666'>".$row['title']."</td>";
    	echo		"<td valign='top' bgcolor='#666666'>";
	echo		$row['content'];
	if	( $row[hasduedate] == "true")
	{
		include		"timer/timer.php";
		echo		"</td>";
	}
	else
	{
		echo		"</td>";
	}

	//echo "</td>";

	// echos the UP and DOWN rank buttons and form
	if ( $row[rank] > 1)
	{
		echo		"<form action='idea_rank.php' method='get'>";
		echo			"<input name='id' type='hidden' value=".$row[id]." />";
		echo			"<input name='direction' type='hidden' value='up' />";
		echo			"<td width='16' valign='middle' bgcolor='#666666'><input type='image' src='images/up.png' alt='Up'></td>";
		echo		"</form";

	}

	if ( $row[rank] < $num_rows)
	{
		echo		"<form action='idea_rank.php' method='get'>";
		echo			"<input name='id' type='hidden' value=".$row[id]." />";				
		echo			"<input name='direction' type='hidden' value='down' />";
		echo			"<td width='16' valign='middle' bgcolor='#666666'><input type='image' src='images/down.png' alt='Down'></td>";
		echo		"</form";

	}

	echo		"<form action='edit_idea.php' method='get'>";
	echo			"<input name='id' type='hidden' value=".$row[id]." />";
	echo			"<td width='16' valign='middle' bgcolor='#ffffff'><input type='image' src='images/edit.png' alt='Edit'></td>";
	echo		"</form";

  		echo		"<form action='delete_idea.php' method='get'>";
	echo			"<input name='delete_idea' type='hidden' value=".$row[id]." />";
	echo			"<td width='16' valign='middle' bgcolor='#ffffff'><input type='image' src='images/delete.png' alt='Delete'></td>";
	echo		"</form";


	echo	"</tr>";
	// child query

	$query_children = "SELECT * FROM children";

	$children_result = mysql_query($query_children);
	$children_rows = mysql_num_rows($children_result);

	//this part displays the children of the item IF it is set to OPEN and IF it has any children.

	if ( $row[state] == TRUE )
	{
		for ($c=0; $c < $children_rows; $c++)
			{
				$children_row = mysql_fetch_array($children_result);

				if ( $children_row['childof'] ==  $row['id'] )
				{
					echo "<tr>";
					echo	"<td width='16' valign='top' bgcolor='#ffffff'></td>";
					echo	"<td width='16' valign='top' bgcolor='#ffffff'></td>";
					echo 	"<td width='150' valign='top' bgcolor='#999999'>";
					echo 		$children_row['title'];
					echo 	"</td>";
					echo	"<td bgcolor='#999999'>".$children_row['content']."</td>";

					// links for editing and deleting each child.

					echo		"<form action='edit_child.php' method='get'>";
					echo			"<input name='id' type='hidden' value=".$children_row[id]." />";
					echo			"<td width='16' valign='middle' bgcolor='#ffffff'><input type='image' src='images/edit.png' alt='Edit Child'></td>";
					echo		"</form";

  						echo		"<form action='delete_child.php' method='get'>";
					echo			"<input name='parent_id' type='hidden' value=".$row[id]." />";
					echo			"<input name='delete_child' type='hidden' value=".$children_row[id]." />";
					echo			"<td width='16' valign='middle' bgcolor='#ffffff'><input type='image' src='images/delete.png' alt='Delete Child'</td>";
					echo		"</form";
					echo "</tr>";
				}

			}
	}
//

}

echo	"</table>";
?>

 

JavaScript timer:

<?php

//$date = $HTTP_GET_VARS[date];

//$hours = $HTTP_GET_VARS[hours];
//$minutes = $HTTP_GET_VARS[minutes];

//get date from DB
include "../dbconnect.php";

//SELECT DATE_FORMAT(myDateField, '%d-%m-%Y') FROM myTable
//gets the idea background color from DB
//$query = "SELECT DATE_FORMAT(duedate, '%d-%m-%Y') FROM ideas WHERE id='18'";
$query = "SELECT duedate FROM ideas WHERE id = '18'"; 
//$row['id']";
$result = mysql_query($query);
$rows = mysql_fetch_array($result);

$duedate = $rows[0];
//echo $duedate;
//echo "<br>";
$due_date = date('F j Y H:i:s',strtotime($duedate));
//echo "<br>";
//echo $due_date;


//2007-12-28 14:59:59
//$due_date = $date." ".$hours.":".$minutes.":00";   

//fake $due_date for testing
$due_date = "January 1 2008 00:00:00";

$bg_color = "#FF0000";
?>

<!--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>-->

<style style="text/css">

.custom
{
color:#FFFFFF;
font: bold 20px Arial, Helvetica, sans-serif;
background-color:<?php echo $bg_color;?>;
}

.lcdstyle{ /*Example CSS to create LCD countdown look*/
background-color:black;
color:yellow;
font: bold 18px MS Sans Serif;
padding: 3px;
}

.lcdstyle sup{ /*Example CSS to create LCD countdown look*/
font-size: 80%
}

</style>

<script type="text/javascript">

/***********************************************
* Dynamic Countdown script- © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

function cdtime(container, targetdate){
if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
this.currentTime=new Date()
this.targetdate=new Date(targetdate)
this.timesup=false
this.updateTime()
}

cdtime.prototype.updateTime=function(){
var thisobj=this
this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}

cdtime.prototype.displaycountdown=function(baseunit, functionref){
this.baseunit=baseunit
this.formatresults=functionref
this.showresults()
}

cdtime.prototype.showresults=function(){
var thisobj=this


var timediff=(this.targetdate-this.currentTime)/1000 //difference btw target date and current date, in seconds
if (timediff<0){ //if time is up
this.timesup=true
this.container.innerHTML=this.formatresults()
return
}
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(timediff/oneDay)
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
hourfield=dayfield*24+hourfield
dayfield="n/a"
}
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
minutefield=dayfield*24*60+hourfield*60+minutefield
dayfield=hourfield="n/a"
}
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
var secondfield=timediff
dayfield=hourfield=minutefield="n/a"
}
this.container.innerHTML=this.formatresults(dayfield, hourfield, minutefield, secondfield)
setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}

/////CUSTOM FORMAT OUTPUT FUNCTIONS BELOW//////////////////////////////

//Create your own custom format function to pass into cdtime.displaycountdown()
//Use arguments[0] to access "Days" left
//Use arguments[1] to access "Hours" left
//Use arguments[2] to access "Minutes" left
//Use arguments[3] to access "Seconds" left

//The values of these arguments may change depending on the "baseunit" parameter of cdtime.displaycountdown()
//For example, if "baseunit" is set to "hours", arguments[0] becomes meaningless and contains "n/a"
//For example, if "baseunit" is set to "minutes", arguments[0] and arguments[1] become meaningless etc


function formatresults(){
if (this.timesup==false){//if target date/time not yet met
var displaystring="<span class='custom'>"+arguments[0]+" days "+arguments[1]+" hours "+arguments[2]+" minutes "+arguments[3]+" seconds</span>"
}
else{ //else if target date/time met
var displaystring="Future date is here!"
}
return displaystring
}



</script>

<!--<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Count Down!</title>
</head>

<body>-->

<div id="countdowncontainer"></div>
<!-- <div id="countdowncontainer2"></div> -->


<script type="text/javascript">

var futuredate=new cdtime("countdowncontainer", "<?php echo $due_date;?>")
futuredate.displaycountdown("days", formatresults)

<!--

-->

</script>

<!--</body>
</html>-->

 

The only thing I can think of is that the JavaScript is breaking the loop and causing it to go on and on and on?

But if this is so I do not know why or how to fix it.

 

Any idea?

 

Thanks for your time,

ian

Link to comment
Share on other sites

If it works by bypassing the javascript stuff, then you need to post this to the javascript board for their opinion...

 

There should be a way to prove this code works or doesn't without the javascript stuff, so do that. If the code isn't working correctly without it, then we need to fix that.

 

PhREEEk

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.