i am trying to render values from mysql query and insert them into a multidimensional array. I am able to create the array and insert all the values from the query into the array.
the problem occurs when a value does not exist in the database and i try to echo out the array setting. I get the followig error:
Undefined offset: 2
might be easier if i show you want i mean
my class contains lots of functions that echo dynamically set values i.e
public $answer=array();
public function questionOne()
{
return $this->answers[$row['period_id']]['question_one'];
}
the above values are set from the dynmic rendering of values from the query below:
while ($row = mysqli_fetch_array ($results, MYSQLI_ASSOC))
{
$this->answers[$row['period_id']][] = $row['question_one'];
$this->answers[$row['period_id']][] = $row['question_two'];
}
This fuction works as long as the values exist in the database. So, i need a way to initlize the array.
i tried doing this ;
$this->answers = $this->theResponseArray();
public function theResponseArray()
{
return $array = array (
1 => array (
0 => null,
1 => null,
),
2 => array (
0 => null,
1 => null,
),
3 => array (
0 => "",
1 => "",
),
3 => array (
0 => "",
1 => "",
)
);
}
but all that happened is that it rendered the entire array as null and prevented from placing new values into the array.
UPDATE
i have placed the entire code on fiddle: