1

Good day! I would like to ask for help.

I'm currently making an Evaluation System for my school project. In my code, I can generate ALL the questions in my dbase together with 5 checkboxes.

e.g. If I add a new question to a particular criteria, it will be show to a table together with 5 auto-generated checkboxes.

how can I auto increment the name and value of my checkbox so if I save them to my dbase the value will be incremented every time there's a new question added?

I currently have 5 criterias, each have 5 questions and 5 sets of checkbox each. consisting of 25checkbox per criteria, but they have the same value and name when auto-generating.

<?php

        $sql = "SELECT * FROM question_pool_registrar WHERE criteria_id = 1";
        $result = $connect->query($sql);

        if($result->num_rows > 0){
            $nr = 0;
            while($row = $result->fetch_assoc()){
            $question = $row['question'];

                echo "<tr>
                <td>$question</td>
                <td><input type='checkbox' value=\"1c5".$nr."\" name=\"answer1".$nr."\" class='radio'></td>
                <td><input type='checkbox' value=\"1c4".$nr."\" name=\"answer1".$nr."\" class='radio'></td>
                <td><input type='checkbox' value=\"1c3".$nr."\" name=\"answer1".$nr."\" class='radio'></td>
                <td><input type='checkbox' value=\"1c2".$nr."\" name=\"answer1".$nr."\" class='radio'></td>
                <td><input type='checkbox' value=\"1c1".$nr."\" name=\"answer1".$nr."\" class='radio'></td>
                      </tr>";
                $nr++;
            }
        }

        ?>
  • 1
    Possible duplicate of [Get $\_POST from multiple checkboxes](https://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes) – miken32 May 22 '19 at 02:29
  • You can increment `$nr` on each line inside your loop like this:``. It will increase the value before insertion on the first instance, then that value will be available on the second. You should move `$nr = 0;` outside of the loop if you don't want the values repeated each time the loop executes. – tshimkus May 22 '19 at 05:05
  • thank u for your answer sir! my code is now working. now my problem is how can i save all the value of my checkbox to all the columns on my database :D – aldrian diaz May 22 '19 at 05:34

0 Answers0