如何使用codeigniter从foreach获取特定的id

时间:2017-02-05 18:36:59

标签: javascript php ajax codeigniter bootstrap-modal

只想询问如何在特定的foreach中获取ID

我的控制器

public function validate_subtopic(){

    $data = array('success' => false, 'messages' => array());
    $this->form_validation->set_rules("subtopicname", "SubTopicName", "trim|required");
    $this->form_validation->set_rules("subtopicdescription", "SubTopicDescription", "trim|required");
    $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
    if($this->form_validation->run()){
        $data['success'] = true;

        $subtopic_data = array(
            'subtopicname'          => $this->input->post('subtopicname'),
            'subtopicdescript'  => $this->input->post('subtopicdescription'),
            'subjectID'         => $this->input->post('subjectID'),
            'topicID'           => $this->input->post('topicID'),
            );

        $this->addtopic_model->insert_subtopic($subtopic_data);
    }
    else{
        foreach ($_POST as $key => $value) {
            $data['messages'][$key] = form_error($key);
        }
    }

    echo json_encode($data);
}

我的模特

public function insert_subtopic($subtopic_data){
    $this->db->insert('subtopics', $subtopic_data);
}

我从bootstrap模态的视图

<div class="modal inmodal fade" id="addSubTopic" tabindex="-1" role="dialog"  aria-hidden="true">

    <?php 
    $att = array(
        'method' => 'POST',
        'id' => "form-user_sub");
    echo form_open("topicAdd_Controller/validate_subtopic", $att);
    ?>
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h5 class="modal-title">Please Input SubTopic</h5>
            </div>
            <div id="the-message"></div>
            <div class="modal-body">

                <form role="form">
                    <div class="form-group">
                        <label>Sub Topic Name</label>
                        <input class="form-control" name="subtopicname" id="subtopicname" type="text">
                    </div>
                    <div class="form-group">
                        <label>Sub Topic Description</label>
                        <textarea name="subtopicdescription" id="subtopicdescription" class="form-control">
                        </textarea>
                    </div>
                    <?php foreach($sample as $row){
                    ?>
                        <?php $index = current($sample); ?>
                        <input type="text" value="<?php echo $row['topicID']; ?> name="topicID">
                    <?php } ?>

                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
    <?php form_close();?>
</div>

这是$ sample的输出:
This is the output of $sample
问题是我无法从我选择的特定foreach loop获取ID。我该怎么解决这个问题? 请帮忙

1 个答案:

答案 0 :(得分:0)

您可以使用while(){}方法,因此您的脚本将是这样的

$i = 0;
$res = array();
while($data = $_POST){
    $res[$i] = $data;
    $i++;
}
echo json_encode($res);