Welcome Guest, Not a member yet? Register   Sign In
Arrays
#1

[eluser]Todlerone[/eluser]
Hello everyone and TY in advance. I'm getting an Message: Invalid argument supplied for foreach() with this method. I'm trying to compare the form submitted data with an array with database info. Is my approace to the array wrong...TY

Code:
function add(){
        $data['user'] = $this->ion_auth->get_user_array();
        if (!$this->ion_auth->logged_in()){
                redirect('home/login', 'refresh');
            }else{
                $ptID = $this->session->userdata('demo_num_id');

            $this->load->helper(array('form', 'array'));
            $this->load->library('form_validation');
            $this->load->model('Patients_model');
            $this->load->model('Plans_cols');
            $this->config->load('patients');

            $data['status'] = $this->config->item('status');
            $data['tracking'] = $this->config->item('tracking_method');
            $data['method'] = $this->config->item('plan_method');
            $data['algorithm'] = $this->config->item('algorithm');
            $data['collimator'] = $this->config->item('collimator');
            $data['delivery'] = $this->config->item('delivery');

            $data['pt_status'] = $this->Patients_model->get_patient_status($ptID);
            $data['patient'] = $this->Patients_model->get_patient_where($ptID);
            $data['treatID'] = $this->uri->segment(4);
            $data['treatNUM'] = $this->uri->segment(5);
            $data['clinic_visit'] = $this->uri->segment(6);
            $plan_status = $this->Patients_model->get_status_where($ptID, $this->uri->segment(6));
            
            $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
            if ($this->form_validation->run('plan_add') == FALSE){
                $this->title="Add a plan";    
                $this->thisPage="Patients";
                $this->load->view('header',$data);
                $this->load->view('patients/plan_add_form', $data);
                $this->load->view('sidebars/sidebar_patient_selected');
                $this->load->view('footer');
            }else{
                foreach ($plan_status as $ps):
                    if ($ps['plans_plan_name'] == FALSE && $ps['plan_status_current'] == 'ip'){
                        $this->Patients_model->update_plans_plan_name($ptID, $data['user']);
                        $this->Plans_cols->add_plans($ptID, $data['user']);
                        $this->Plans_cols->add_collimators($ptID, $data['user']);
                    }
                    if ($ps['plans_plan_name'] != $this->input->post('plans_plan_name')){
                        $this->Patients_model->add_new_cv_plan($ptID, $data['user']);
                        $this->Plans_cols->add_plans($ptID, $data['user']);
                        $this->Plans_cols->add_collimators($ptID, $data['user']);
                    }
                endforeach;
                //$this->Plans_cols->add_plans($ptID, $data['user']);
                //$this->Plans_cols->add_collimators($ptID, $data['user']);
                //redirect('patients/patient/patient_selected/', 'refresh');
            }// end form_validation
        }//end logged_in
    }// end plan_add
#2

[eluser]CroNiX[/eluser]
Hard to say without seeing your model where you are getting you data here:
$plan_status = $this->Patients_model->get_status_where($ptID, $this->uri->segment(6));

My guess is that in get_status_where(), you are returning a database object instead of result_array().

If you need it returned as an object, you should change your comparisons in your foreach to use objects instead of arrays like:
Code:
if ($ps->plans_plan_name == FALSE && $ps->plan_status_current == 'ip'){
...
#3

[eluser]Todlerone[/eluser]
I tried this and I'm still getting the same error. TY for your response. Here is my model call.
Code:
function get_status_where($ptID, $cv){
        $this->db->where('demo_num_id', $ptID);
        $this->db->where('clinic_visit_id', $cv);
        $query = $this->db->get('plan_status');
        if($query->num_rows()>0){
            return $query->result_array();
        }
    }

TY again
#4

[eluser]CroNiX[/eluser]
dump your array and check what it looks like:
Code:
$plan_status = $this->Patients_model->get_status_where($ptID, $this->uri->segment(6));
echo '<pre>'; print_r($plan_status); echo '</pre>';  //add this
Maybe the format is different.

Edit: also you are not returning anything in your model unless you have a result from your query. You might get rid of that and just return the result, which would be an empty array if there are no results. As it is now, if there are no results you will get errors when trying to iterate over an array that doesn't exist.
#5

[eluser]Todlerone[/eluser]
TY here is what I get.
Array
(
[0] => Array
(
[plan_status_id] => 79
[demo_num_id] => 183
[clinic_visit_id] => 110
[plans_plan_name] =>
[plan_status_current] => ip
[plan_status_plan_overall] =>
[plan_status_started_plan_date] =>
[plan_status_finished_plan_date] =>
[plan_status_comments] =>
[plan_status_created_by] => asmith
[plan_status_created_on] => 2011-04-08 10:30:18
[plan_status_edited_by] => tgauld
[plan_status_edited_on] => 2011-04-11 04:02:29
)

)

It doesn't seem to like my foreach loop. My two if loops simply check to see if the original plan name is NULL so that I can place a name (from the returning form) in it. The second one is to check that the returning form plan name isn't the same as the one from the database table, if it isn't create a new row ....

TY again

PS: love the <pre> formatting of print_r().....awesome...TY
#6

[eluser]toopay[/eluser]
@Todlerone

You should modify your model! Your model will return NULL if there no match result, while in your controller, your foreach expecting array!

Code:
function get_status_where($ptID, $cv)
{
        $this->db->where('demo_num_id', $ptID);
        $this->db->where('clinic_visit_id', $cv);
        $query = $this->db->get('plan_status');
        return $query->result_array();
}

Now it should give you an array, but you might need to do some inspection in your foreach function.
#7

[eluser]CroNiX[/eluser]
if ($ps->plans_plan_name == FALSE && $ps->plan_status_current == 'ip'){

Maybe try:
if ( empty($ps->plans_plan_name) && $ps->plan_status_current == 'ip'){
#8

[eluser]Todlerone[/eluser]
TY for your response. My table will always have an entry at this point. The particular field of interest will be by default NULL. It is at this point that I want to change the value. My first IF loop is to determine the NULL value of the field. I could change the model as there is never a time for num=0. But will that help me still? TY again.
#9

[eluser]CroNiX[/eluser]
[quote author="Todlerone" date="1302654506"]

PS: love the <pre> formatting of print_r().....awesome...TY[/quote]
Yeah, I have a helper I use for this to make it easy...printr() instead of print_r() for simple debugging.
Code:
//title is optional
function printr($arr, $title = '')
{
    $output = '';
    if(!empty($title)) $output .= '<h4 style="color:blue; margin:0">' . $title . '</h4>';
    $output .= '<pre style="margin-top:0">';
    $output .= htmlspecialchars(print_r($arr, true));
    $output .= '</pre>';
    echo $output;
}
#10

[eluser]toopay[/eluser]
@Todlerone

As php told you, the error is on foreach statement, not somewhere else. Foreach statement expected an array, if you give another type, php will told you that error.




Theme © iAndrew 2016 - Forum software by © MyBB