Search code examples
ormphp-5.3nested-loopsformshttp-post

deep nested if statement issues


Ok, So I'm writing a task organizer script and I'm having some issues with marking the subtasks as complete or not complete.. my code is as follows...

if($_POST['finished']){ // $_POST['finished'] is an html array of check boxes
            $subtask = Subtask::find_all_by_task_id($task->id);
            foreach($subtask as $stask){
                foreach($_POST['finished'] as $fin){ 
                    if($stask->id == $fin){
                        $stask->finished_at = date('Y-m-d H:i:s', time());
                        $stask->save();
                    } elseif($stask->id != $fin){
                        $stask->finished_at = "";
                        $stask->save();
                    }
                }
            }
        } else { // This works as expected when un-checking all checkboxes
            $subtask = Subtask::find_all_by_task_id($task->id);
            foreach($subtask as $stask){
                if($stask->finished_at != null){
                    $stask->finished_at = null;
                    $stask->save();
                }
            }
        }

My problem I believe is the if statement is rolling over both values(there are only two subtasks right this minute) when it gets to the last values all other $fin does not equal $stask->id anymore so those get set back to null in the DB hence making it SEEM as though they're never updated..How can I fix this whats the best route to tackle this if statement..I've tried a few variations but to no avail.


Solution

  • Ok, So a simple workaround/solution was this..

    function completetask($tasktype, $id, $return_uri = 'edittask'){
        if($tasktype == "subtask"){
            $task = Subtask::find($id);
            if($task->finished_at == null){
                $task->finished_at = date("Y-m-d H:i:s", time());
                $task->save();
                switch($return_uri){
                    case 'showtask':
                        redirect('taskdisplay/showtask/' . $task->task_id);
                        break;
                    case 'edittask':
                        redirect('admintask/edittask/' . $task->task_id);
                        break;
                    default:
                        redirect('admintask/edittask/' . $task->task_id);
                }
    
            } else {
                $task->finished_at = null;
                $task->save();
                switch($return_uri){
                    case 'showtask':
                        redirect('taskdisplay/showtask/' . $task->task_id);
                        break;
                    case 'edittask':
                        redirect('admintask/edittask/' . $task->task_id);
                        break;
                    default:
                        redirect('admintask/edittask/' . $task->task_id);
                }
            }
        } else {
            $task = Task::find($id);
            if($task->finished_date == null && $task->taskcomplete == 0){
                $task->finished_date = date("Y-m-d H:i:s", time());
                $task->taskcomplete = 1;
                $task->save();
                redirect('admintask/edittask/' . $task->id);
            } else {
                $task->finished_date = null;
                $task->taskcomplete = 0;
                $task->save();      
                redirect('admintask/edittask/' . $task->id);
            }
    
        }
    
    
    }