0
votes

someone from freelancer created this web based software for us. By mistake i deleted all the medical examination (i'm a doctor assistant) from the control panel. The software now gives me this error:

Message: Undefined variable: data

Filename: models/gestione_model.php

Line Number: 127

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: models/gestione_model.php

Line Number: 127

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: models/gestione_model.php

Line Number: 127

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: models/gestione_model.php

Line Number: 127

I find the line 27 here:

public function conta_visite($s)
{
    $query = $this->db->get('oggetti');
    if ($query->num_rows() > 0) {
        $data = $query->result_array();
    }
    $aperte = 0;
    $chiuse = 0;
    foreach ($data as $d) {

        if (new DateTime() > new DateTime($d['DataVisita'])) $chiuse++;
        else $aperte++;
    }

    if($s == 1) return $aperte;
    else return $chiuse;
}

Can someone help me please? Thanks

2

2 Answers

0
votes

you are closing IF before foreach!

    public function conta_visite($s) { 
$query = $this->db->get('oggetti'); 
if ($query->num_rows() > 0) { 
 $data = $query->result_array(); 
} // HERE if result is ==0 then $data does not exist

$aperte = 0; $chiuse = 0; foreach ($data as $d) {

        if (new DateTime() > new DateTime($d['DataVisita'])) $chiuse++;
        else $aperte++;
    }

    if($s == 1) return $aperte;
    else return $chiuse;
0
votes

You need to make sure that $data is set before using it. If no rows are returned then $data is not set and you get errors when trying to process it.

if ($query->num_rows() > 0) {
    $data = $query->result_array();
} else {
   //do what needs to be done if the database does not return any rows
    $data = array(); //empty array maybe?
}