Welcome Guest, Not a member yet? Register   Sign In
Strange Error and Double Error Messages
#1

I am surprised and confused.

I have a fully working php web application that is throwing an Undefined offset warning notice and I cannot see why.

I use Codeigniter (obviously) and have this call to a model:

$data = $this->clubinfo_model->get_data($club);

An array is returned and print_r confirms that the array is as I expected with the '0' array containing a number of key-value pairs. Here is the print_r output:

Array ( [0] => Array ( [clubID] => 18 [shortname] => Woodmere [fullname] => Woodmere Bridge Club [game1day] => x [DOW] => Thursday [game1time] => [game1location] => [director] => [readdirectory] => [offset] => [urlpath] => [webpage] => ) )

I then make this assignment:

$DOW = $data['0']['DOW'];
$DOW get the day of week from the array.

Works fine.

But, php reports an error on the assignment line and says:

ERROR - 2015-01-05 05:50:00 --> Severity: Notice --> Undefined offset: 0 /home3/billhogs/public_html/ccbridgeclub/application/controllers/scores.php 71

Taking out the '0' index gets the expected results--the program does not work.

So, what is going on?

I assume this is unrelated, but the Codeignighter error log reports the Undefined offset warning notice twice. I have checked and the function with the strange error is only called once anywhere in the project. Actually, it is only called from an address fed to the default controller.

Thanks,

Bill
Reply
#2

(01-05-2015, 11:10 AM)bhogsett Wrote: ...
Here is the print_r output:

Array ( [0] => Array ( [clubID] => 18 [shortname] => Woodmere [fullname] => Woodmere Bridge Club [game1day] => x [DOW] => Thursday [game1time] => [game1location] => [director] => [readdirectory] => [offset] => [urlpath] => [webpage] => ) )

I then make this assignment:

$DOW = $data['0']['DOW'];
...

ERROR - 2015-01-05 05:50:00 --> Severity: Notice --> Undefined offset: 0 /home3/billhogs/public_html/ccbridgeclub/application/controllers/scores.php 71

Taking out the '0' index gets the expected results--the program does not work.

I'm guessing you need to use $data[0]['DOW'] instead of $data['0']['DOW']. Ideally, you would track down wherever the $data array is getting padded with an additional array and eliminate the need for the 0 in the first place, but I think the short term problem will be fixed by using a numeric index instead of a string.
Reply
#3

(01-05-2015, 01:49 PM)mwhitney Wrote: I'm guessing you need to use $data[0]['DOW'] instead of $data['0']['DOW']. Ideally, you would track down wherever the $data array is getting padded with an additional array and eliminate the need for the 0 in the first place, but I think the short term problem will be fixed by using a numeric index instead of a string.

No, changing ['0'] to [0] has no effect. I had tried the change before posting. I also posted on Stack Overflow and the same suggestion was made there and someone confirmed that the quoting would make no difference.

The code works, it just throws a warning that does not make sense.

Any idea why Codeigniter reports the warning twice?

Bill
Reply
#4

Hi Bill,

Could you please provide us the code of the model's function get_data ?

I'm wondering too if you'll ALWAYS have one row as answer ? If the answer is yes, you should return the results by this way :

PHP Code:
return $this->db->row() 

This way you only return first query row and no need anymore to use the [0] offset...
Reply
#5

(01-06-2015, 01:59 AM)$this Wrote: Hi Bill,

Could you please provide us the code of the model's function get_data ?

</quote>

Thanks, here is the get_Data function:


Code:
public function get_data($club, $time, $db = "scores2014", $request = "") {

       $this->dbTable = $db;
       $data = $this->dataMaker($club, $time)
       $where = "";
       $callers = debug_backtrace();

       if ($callers[1]['function'] === 'winners' || $request === "winners") {
           $winner = true;
           $arrayName = "winners";
           $data['arrayName'] = "winners";
           $data['sectionTitle'] = "Ten Most Recent " . $data['winnersTitle'];
           $data['title'] = $data['winnersTitle'];
           $this->db->select("DATE_FORMAT(date,'%W, %M %D') as `formattedDate`, `date`, FORMAT(`percent`,2) as `formattedPercent`, `masterpoints`,`pair`, `club`,`pair`,`place`, `url`, `NS`, `movement`, `id`", FALSE);
           $this->db->from($this->dbTable);
           $where = $this->get_WhereTime($time);
           $this->db->order_by("date", "DESC");
           $this->db->order_by("percent", "DESC");
           $where .= " AND `place` = 1";
           if ($club) {
               $where .= " AND `club` = \"" . $data['clubFullName'] . "\"";
           }
EXCLUDING SOME IF ELSE CLAUSES

       $query = $this->db->get();
       $data['quRsAr'] = $query->result_array();
       return $data;

}
Quote:I'm wondering too if you'll ALWAYS have one row as answer ? If the answer is yes, you should return the results by this way :

PHP Code:
return $this->db->row() 

This way you only return first query row and no need anymore to use the [0] offset...

No, multiple rows are returned by get_Data() so I cannot use $this->db->row()

PLEASE keep in mind that the program works and the assignment is made correctly, but php/Codeigniter issues a warning message--actually two identical warning message for the same line.

Bill
Reply
#6

(01-06-2015, 03:30 PM)bhogsett Wrote:
(01-06-2015, 01:59 AM)$this Wrote: Hi Bill,

Could you please provide us the code of the model's function get_data ?

</quote>

Thanks, here is the get_Data function:


Code:
public function get_data($club, $time, $db = "scores2014", $request = "") {

       $this->dbTable = $db;
       $data = $this->dataMaker($club, $time)
       $where = "";
       $callers = debug_backtrace();

       if ($callers[1]['function'] === 'winners' || $request === "winners") {
           $winner = true;
           $arrayName = "winners";
           $data['arrayName'] = "winners";
           $data['sectionTitle'] = "Ten Most Recent " . $data['winnersTitle'];
           $data['title'] = $data['winnersTitle'];
           $this->db->select("DATE_FORMAT(date,'%W, %M %D') as `formattedDate`, `date`, FORMAT(`percent`,2) as `formattedPercent`, `masterpoints`,`pair`, `club`,`pair`,`place`, `url`, `NS`, `movement`, `id`", FALSE);
           $this->db->from($this->dbTable);
           $where = $this->get_WhereTime($time);
           $this->db->order_by("date", "DESC");
           $this->db->order_by("percent", "DESC");
           $where .= " AND `place` = 1";
           if ($club) {
               $where .= " AND `club` = \"" . $data['clubFullName'] . "\"";
           }
EXCLUDING SOME IF ELSE CLAUSES

       $query = $this->db->get();
       $data['quRsAr'] = $query->result_array();
       return $data;

}
Quote:I'm wondering too if you'll ALWAYS have one row as answer ? If the answer is yes, you should return the results by this way :

PHP Code:
return $this->db->row() 

This way you only return first query row and no need anymore to use the [0] offset...

No, multiple rows are returned by get_Data() so I cannot use $this->db->row()

PLEASE keep in mind that the program works and the assignment is made correctly, but php/Codeigniter issues a warning message--actually two identical warning message for the same line.

Bill

I think you have that line of code in a loop since it generates the same error twice and it gets the expected result as well. Try wrapping the statement with an if statement to check if $data[0] exists. "if (isset($data[0]) { $DOW=.... }". Error message should disapair.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB