简体   繁体   中英

Why i'm getting Undefined offset: 0 in this php code

i wrote this code and i'm getting this error, i tried every this but doesn't seem to work btw i've made a comment for line 55 where i'm gettin error

function calculate_result()
    {
        $option_number = array('option_a'=>'1','option_b'=>'2','option_c'=>'3','option_d'=>'4');
        $answers = array();
        $total_questions = $this->quiz_model->return_number_of_questions($this->input->post('quiz_number'));
        if($total_questions > 0)
        {
            for($i=1; $i <= $total_questions; $i++)
            { 
        //line 55   $answers[$i] = $option_number[$this->input->post('question_'.$i)];  
            }
            print_r($answers);
        }
        else
        {
            show_404();
        }
        //print_r($answers);
    }

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 0

Filename: controllers/quiz.php

Line Number: 55

Change this...

for($i=1; $i <= $total_questions; $i++)

To this...

for($i=0; $i <= $total_questions; $i++)

On that line there are two indexes, on the left side, there is $i and it is non-zero.

So, problems has to be with right side expression, with $this->input->post('question_'.$i) which probably returns 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM