Page 1 of 1

Please check the logic of my while loop

Posted: Thu Apr 26, 2007 4:37 pm
by Jhorra
I'm working on a scheduleing app, and when someone submits an item that conflicts with another entry, they have the option of bumping out the other entries. In order to do this properly, I'm updating the conflicting event, but I have to loop through every update, and if that now conflicts, update what it conflicted with. It seems like it's going into an endless loop right now.

Code: Select all

//We split what we are passed into an ID and a time difference
$array = explode(',', $array);
$id = $array[0];
$time_difference = $array[1];
$conflict = true;
//We will loop through and continually update until we are out of conflicts
while($conflict = true)
{
	//Take our ID and update it with the id an time difference we have
	$this->db->sql = "UPDATE schedule SET start_time = start_time + ".$time_difference.", end_time = end_time + ".$time_difference." WHERE id = ".$id;
	$check = $this->db->update_record();

	//Now get the schedule entry info after it's been updated
	$this->get_info($id);
	$end_time = $this->end_time;

	//Now take this info and check for an additional conflict
	$additional = $this->additional_conflicts($end_time);
	//If we get a 0 back there is no other conflict
	if($additional == 0)
		$conflict = false;
	else 
	{
		//Else we explode our array to get an id and time difference and continue the loop 
		$array = explode(',', $additional);
		$id = $array[0];
		$time_difference = $array[1];
	}
}
Here's what the additional conflicts function looks like

Code: Select all

$this->db->sql = "SELECT 
			start_time 
		FROM 
			tv_schedule 
		WHERE 
			channel_id = '".$this->channel_id."' AND 
			start_time < ".$end_time." AND 
			end_time > ".$end_time;
$count = $this->db->count_records();

if($count == 0)
	return 0;
else 
{
	$row = $this->db->select_record();
	$time_difference = $end_time = $row['start_time'];
	return $row['id'].",".$time_difference;
}
If I had to guess what was wrong, I would think that my while loop isn't reading the updated variables I'm setting at the end of it, and continuing to read the ones set at the top. It's only test data right now, so it should only be updating 1 or 2 entries.

Posted: Thu Apr 26, 2007 4:45 pm
by Burrito
try echoing out $additional and see what it is over the course of the loop.

Posted: Thu Apr 26, 2007 4:53 pm
by Jhorra
I don't know why I didn't think of that, but that was it. There was an error in my SQL.