SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Please check the logic on my while loop

    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.

    PHP Code:
    //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
    PHP Code:
    $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.

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe read all the entries into an array and use a sort function on them and then adjust which ever ones accordingly?

  3. #3
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I echo'd out my $additional value, and it looks like there was an error in my sql, so it just kept looping forever.

  4. #4
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    while($conflict true
    don't you mean
    PHP Code:
    while($conflict == true
    // or
    while ($conflict
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •