Page 1 of 1

Looping Problem

Posted: Sat Nov 04, 2006 2:45 am
by ibanez270dx
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey guys,
 I don't normally need to loop stuff,  but I have a very repetitive task I want to take care of. Pretty much, I want to select date information from my database ($dwntime_year, $dwntime_month, $dwntime_day, $dwntime_year2, $dwntime_month2, $dwntime_day2) and convert them to timestamps ($start_ts, $end_ts) and put them back into the database. Dwntime_group='TRUE' for all rows. Here is my code:

Code: Select all

<?php
 include("connect.php");
 $sql = "SELECT * FROM downtime WHERE dwntime_group='TRUE'";
 $result = @mysql_query($sql,$connection) or die(mysql_error());
 $num = mysql_num_rows($result);
 if($num >= 1)
	{
	 while ($row = mysql_fetch_array($result)) 
		{
		 $row_id = $row['row_id'];
		 $dwntime_day = $row['dwntime_day'];
		 $dwntime_month = $row['dwntime_month'];
	 	 $dwntime_year = $row['dwntime_year'];
		 $dwntime_day2 = $row['dwntime_day2'];
		 $dwntime_month2 = $row['dwntime_month2'];
	 	 $dwntime_year2 = $row['dwntime_year2'];
		 
$start_ts = mktime(0, 0, 0, $dwntime_month, $dwntime_day, $dwntime_year);
		 $end_ts = mktime(0, 0, 0, $dwntime_month2, $dwntime_day2, $dwntime_year2);
		
 		 include("connect.php");
 		 $sql = "UPDATE downtime SET dwntime_ts1='$start_ts', dwntime_ts2='$end_ts' WHERE row_id='$row_id'";
 		 $result = @mysql_query($sql,$connection) or die(mysql_error());
		}
	} 
?>
The problem is that it only does the first row in the table and doesn't loop for the rest of them. I tried messing around with the .= thing, but no luck.

Any help is appreciated!!

Thanks,
- Jeff


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Nov 04, 2006 3:05 am
by volka
You do not need a loop here either ;)

Code: Select all

UPDATE
	`dwntime_group`
SET 
	dwntime_ts1=UNIX_TIMESTAMP(CONCAT_WS('-', dwntime_year, dwntime_month, dwntime_day)),
	dwntime_ts2=UNIX_TIMESTAMP(CONCAT_WS('-', dwntime_year2, dwntime_month2, dwntime_day2))