Page 1 of 1

iteration problem

Posted: Wed Mar 02, 2011 1:19 am
by tayys

Code: Select all

<!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>Haigou-Tokou Calculation</title>
</head>
<body>
<h1>Haigou-Tokou Calculation</h1>
<?php 
$con=mysql_connect("localhost", "admin", "123123") or die("cannot connect"); 

if (!$con)
	{
		die ('Could not connect: ' . mysql_error());
	}	
else 
mysql_select_db("haigodb", $con);

$sql="SELECT * FROM tb_workingplan order by no";
$result=mysql_query($sql);
$numRow = mysql_num_rows($result);

echo '<p>Number of items found: '.$numRow.'</p>';

for($i=0; $i<$numRow; $i++)
{
	while($row = mysql_fetch_assoc($result)) 
	{	
			$haigouProcess=46800;	//13hrs
			$tokouProcess=36000;	//10hrs
			$id="{$row['no']}";
			$tokouStringTime= strtotime("{$row['tokouStartTime']}");
			$tokouStartTime=date("H:i:s",$tokouStringTime);
			$tokouDate=strtotime("{$row['tokouDate']}");			
			//$tokouDate=date("H:i:s", $tokouStringDate);
			
			$haigouStringTime=$tokouStringTime-$haigouProcess;
			$haigouStartTime=date("H:i:s", $haigouStringTime);
			
			if($tokouStartTime<$haigouStartTime)
			{
				$haigouDate=date("Y-m-d",$tokouDate-86400);
			}
			else
			{
				$haigouDate=date("Y-m-d",$tokouDate);
			}			
					
			$nextLotTokouStringTime=$tokouStringTime+$tokouProcess;
			$nextLotHaigouStringTime=$nextLotTokouStringTime-$haigouProcess;
			$nextLotTokouStartTime=date("H:i:s", $nextLotTokouStringTime);
			$nextLotHaigouStartTime=date("H:i:s", $nextLotHaigouStringTime);
			
			if($nextLotTokouStartTime<$tokouStartTime)
			{
				$nextLotTokouDate=date("Y-m-d",$tokouDate+86400);
			}
			else
			{
				$nextLotTokouDate=date("Y-m-d",$tokouDate);
			}
			
			mysql_query("UPDATE tb_workingplan SET haigouStartTime='$haigouStartTime', 
						haigouDate='$haigouDate'	
						WHERE no='$id'");			
		
			
			$id++;		
			mysql_query("UPDATE tb_workingplan SET haigouStartTime ='$nextLotHaigouStartTime',
						tokouStartTime='$nextLotTokouStartTime', tokouDate='$nextLotTokouDate'
						WHERE no='$id+1'");	

				echo '<p><strong>'.($i+1).'. Tokou machine no.: ';
	echo htmlspecialchars(stripcslashes($row['tokouMachineNo']));
	echo '</strong><br/>Tokou start time: ';
	echo stripcslashes($row['tokouStartTime']);
	echo '<br/>Tokou date: ';
	echo stripcslashes($row['tokouDate']);
	echo '<br/>Haigou start time: ';
	echo stripcslashes($row['haigouStartTime']);
	echo '<br/>Haigou date: ';
	echo stripcslashes($row['haigouDate']);
	echo '</p>';
	}
}
?>
</body>
</html>
First, there are 2 process. I need to do a backward calculation in order to get the starting time for the first item and so on.
The problem here is it seems like doesn't iterate. I need to refresh it every time to retrieve the values for the next iteration.
Thank you in advance.

Re: iteration problem

Posted: Wed Mar 02, 2011 1:29 pm
by danwguy
try getting rid of this...

Code: Select all

for($i=0; $i<$numRow; $i++)
{
You don't need it, you are already doing a while statement so there is no need to have that $i variable for statement. Don't forget to remove the extra } at the end of the code when you remove the for statement

Re: iteration problem

Posted: Thu Mar 03, 2011 11:31 pm
by tayys

Code: Select all

<!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>Haigou-Tokou Calculation</title>
</head>
<body>
<h1>Haigou-Tokou Calculation</h1>
<?php 
$con=mysql_connect("localhost", "admin", "123123") or die("cannot connect"); 

if (!$con)
	{
		die ('Could not connect: ' . mysql_error());
	}	
else 
mysql_select_db("haigodb", $con);

$sql="SELECT * FROM tb_workingplan order by no";
$result=mysql_query($sql);
$numRow = mysql_num_rows($result);

echo '<p>Number of items found: '.$numRow.'</p>';

while($row = mysql_fetch_assoc($result)) 
{	
	$haigouProcess=46800;	//13hrs
	$tokouProcess=36000;	//10hrs
	$id="{$row['no']}";
	if($id==1)
	{
		$tokouStringTime= strtotime("{$row['tokouStartTime']}");
		$tokouDate=strtotime("{$row['tokouDate']}");
		$haigouStringTime=$tokouStringTime-$haigouProcess;
		$tokouStartTime=date("H:i:s",$tokouStringTime);
		$haigouStartTime=date("H:i:s", $haigouStringTime);
		$tD=date("Y-m-d", $tokouDate);
		if($tokouStartTime<$haigouStartTime)
		{
			$haigouDate=date("Y-m-d",$tokouDate-86400);
		}
		else
		{
			$haigouDate=date("Y-m-d",$tokouDate);
		}	
		$nextLotTokouStringTime=$tokouStringTime+$tokouProcess;
		$nextLotHaigouStringTime=$nextLotTokouStringTime-$haigouProcess;
		$nextLotTokouStartTime=date("H:i:s", $nextLotTokouStringTime);
		$nextLotHaigouStartTime=date("H:i:s", $nextLotHaigouStringTime);
		if($nextLotTokouStartTime<$tokouStartTime)
		{
			$nextLotTokouStringDate=$tokouDate+86400;
			$nextLotTokouDate=date("Y-m-d",$nextLotTokouStringDate);	
		}
		else
		{
			$nextLotTokouStringDate=$tokouDate;
			$nextLotTokouDate=date("Y-m-d",$nextLotTokouStringDate);			
		}	
		mysql_query("UPDATE tb_workingplan SET haigouStartTime='$haigouStartTime', 
				haigouDate='$haigouDate'	
				WHERE no='$id'");
	}
	else
	{
		$tokouStringTime= $_SESSION['nextTokouStringTime'];
		$tokouDate=$_SESSION['nextTokouStringDate'];
		$haigouStringTime=$tokouStringTime-$haigouProcess;
		$tokouStartTime=date("H:i:s",$tokouStringTime);
		$haigouStartTime=date("H:i:s", $haigouStringTime);
		$tD=date("Y-m-d", $tokouDate);
		if($tokouStartTime<$haigouStartTime)
		{
			$haigouDate=date("Y-m-d",$tokouDate-86400);
		}
		else
		{
			$haigouDate=date("Y-m-d",$tokouDate);
		}	
			
		$nextLotTokouStringTime=$tokouStringTime+$tokouProcess;
		$nextLotHaigouStringTime=$nextLotTokouStringTime-$haigouProcess;
		$nextLotTokouStartTime=date("H:i:s", $nextLotTokouStringTime);
		$nextLotHaigouStartTime=date("H:i:s", $nextLotHaigouStringTime);
			
		if($nextLotTokouStartTime<$tokouStartTime)
		{
			$nextLotTokouStringDate=$tokouDate+86400;
			$nextLotTokouDate=date("Y-m-d",$nextLotTokouStringDate);	
		}
		else
		{
			$nextLotTokouStringDate=$tokouDate;
			$nextLotTokouDate=date("Y-m-d",$nextLotTokouStringDate);			
		}	
		
		mysql_query("UPDATE tb_workingplan SET tokouStartTime='$tokouStartTime',
					tokouDate='$tD',	
					haigouStartTime='$haigouStartTime', 
					haigouDate='$haigouDate'	
					WHERE no='$id'");
	}			
		
	$_SESSION['nextTokouStringTime']=$nextLotTokouStringTime;
	$_SESSION['nextTokouStringDate']=$nextLotTokouStringDate;
		
	//Display
	echo '<p><strong>'.($i+1).'. Tokou machine no.: ';
	echo htmlspecialchars(stripcslashes("{$row['tokouMachineNo']}"));
	echo '</strong><br/>Tokou start time: ';
	echo stripcslashes($tokouStartTime);
	echo '<br/>Tokou date: ';
	echo stripcslashes($tD);
	echo '<br/>Haigou start time: ';
	echo stripcslashes($haigouStartTime);
	echo '<br/>Haigou date: ';
	echo stripcslashes($haigouDate);
	echo '</p>';			
		
	$i++;
}
?>
</body>
</html>
Thank you very much for your quick reply..
i eliminated already the for loop as u mention.
But, i would like to improve it by transforming the time value into graph..where x-axis (date and time) ; y-axis (machine no).
hope you can help to look into this issue for me.
Thank you...