如何在链接Codeigniter中传递ID值

时间:2019-06-17 02:39:45

标签: php codeigniter

我是Codeigniter的新手,请多多包涵。我试图传递id的值,并在链接中显示if的值。

示例: http://localhost/exam/index.php/admin/updateUsers/10

我尝试了类似的解决方案,但似乎没有用。任何帮助表示赞赏。

控制器:

public function addQuestion(){
        $this->load->model('queries');
        $this->queries->getQuestion();
    }
    public function addChoices(){
        $this->form_validation->set_rules('ques','Question','required');
        $this->form_validation->set_rules('ch_des1','Choices','required');
        $this->form_validation->set_rules('ch_des2','Choices','required');
        $this->form_validation->set_rules('ch_des3','Choices','required');
        $this->form_validation->set_rules('ch_des4','Choices','required');
        $this->form_validation->set_rules('ans','Answer','required');
        $this->form_validation->set_error_delimiters('<div class="text-danger">','</div>');

        if($this->form_validation->run() ){
                // $data['ques_id'] = $this->input->post($data)
                $data['ques']    = ($this->input->post('ques'));
                $data['ch_des1'] = ($this->input->post('ch_des1'));
                $data['ch_des2'] = ($this->input->post('ch_des2'));
                $data['ch_des3'] = ($this->input->post('ch_des3'));
                $data['ch_des4'] = ($this->input->post('ch_des4'));
                $data['ans']     = ($this->input->post('ans'));

                if($id=$this->queries->registerChoices($data) )
                {

                    $this->session->set_flashdata('message','Test Added Succesfully');
                    return redirect('admin/testtwo');
                }
                else {
                    $this->session->set_flashdata('message','Failed to Add Test');      
                }
                return redirect('admin/testtwo');
            }   

        else {
                $this->addQuestion();
            }   

        }

型号:

public function registerChoices($data) {
              echo $this->input->post('qtopic_id');

            $question_arr = [
                'ques' => $data['ques'],
                'qtopic_id' => $this->uri->segment(3)


            ];

            $choices_arr = [
                'ques'    => $data['ques'],
                'ch_des1' => $data['ch_des1'],
                'ch_des2' => $data['ch_des2'],
                'ch_des3' => $data['ch_des3'],
                'ch_des4' => $data['ch_des4'],
                'ans'     => $data['ans']

            ];


            $this->db->insert('tbl_choices',$choices_arr);
            $this->db->insert('tbl_question',$question_arr);
                    return $this->db->insert_id();
        }

查看:

<?php include_once('inc/header.php');?>

       <?php echo form_open('admin/addChoices/'.$post->topic_id, ['class' => 'form-horizontal']);?>
        <?php if($msg= $this->session->flashdata('message')):?>
        <div class="col-md-6">
            <div class ="alert alert-dismissble alert-success">
            <?php echo $msg;?>
            </div>
        </div>
    <?php endif;?>
    <h4> Add Question </h4>
     <br><br><hr>
    <div class="admin panel ">
        <form id="addForm" method="post">
            <div class="col-md-5">
                <table>
                        <tr>
                            <td>Question</td>
                            <td>:</td>
                            <td><input type="text" name="ques" value="" placeholder="Enter Question..." required/></td>
                        </tr>
                        <tr>
                            <td>Choice One</td>
                            <td>:</td>
                            <td><input type="text" name="ch_des1" value ="" placeholder="Enter Choice One." required/></td>
                        </tr>
                        <tr>
                            <td>Choice Two</td>
                            <td>:</td>
                            <td><input type="text" name="ch_des2" value = "" placeholder="Enter Choice Two." required/></td>
                        </tr>
                        <tr>
                            <td>Choice Three</td>
                            <td>:</td>
                            <td><input type="text" name="ch_des3" value="" placeholder="Enter Choice Three." required/></td>
                        </tr>
                        <tr>
                            <td>Choice Four</td>
                            <td>:</td>
                            <td><input type="text" name="ch_des4" value ="" placeholder="Enter Choice Four." required/></td>
                        </tr>



                        <br> <br>
                        <tr>
                        <td>Correct Answer</td>
                            <td>:</td>
                            <td><input type="text" name="ans" placeholder="Enter Correct Answer." required/></td>
                        </tr>



                        <td colspan="3" align= center> 

                                <br><br>
                                    <button type ="submit" class="btn btn-primary "> Submit </button> &nbsp
                                 <?php echo anchor('admin/update', 'Back', ['class'=>'btn btn-secondary']);?>


                            </td>
                        </tr>
                </table>
            </form>
        </div>
    </div>
</div>


  <?php echo form_close();?>
    </div>
<?php include_once('inc/footer.php');?>

1 个答案:

答案 0 :(得分:1)

这是推荐的操作方式(https://www.codeigniter.com/user_guide/general/controllers.html#passing-uri-segments-to-your-methods

URL:localhost / admin / updateUsers / 10

class Admin extends CI_Controller {

    public function updateUsers($id = null) {
        if (is_null($id)) {
            show_error('id not provided');
        }
        var_dump($id); //10
    }

}

请注意,其他参数等同于下一段

URL:localhost / admin / updateUsers / foo / bar

    public function updateUsers($var1 = null, $var2 = null) {
        var_dump($var1); //foo
        var_dump($var2); //bar
    }

建议始终在函数声明中将参数设置为null,并检查其是否为非null。