簡體   English   中英

如何將輸入值從視圖傳遞到控制器和模型以正確獲取數據? 代碼點火器

[英]How can I pass input value from view to controller and model to fetch data correctly? Codeigniter

我正在嘗試獲取與我的輸入隱藏值具有相同 ID 的數據。

我無法獲取數據的 id 或輸入隱藏值

首先,這是我所做的,以使事情更清晰,以便我調試我制作了一個硬編碼表單和一個將我帶到頁面的按鈕

在視圖中 -組/index.php

如您所見,我創建了posts/index/1這是一個硬編碼值,但我可以稍后輕松更改它,這不是我的問題

<?php echo form_open('posts/index/1') ?>

    <input type="hidden" name="txtGroup" value="1" />
    <button type="submit" class="btn btn-info"> Submit</button>

</form>

所以在我制作表格后,我將在控制器中創建一個函數來獲取posts/index

在控制器中 - Posts.php

public function index(){
    $this->load->view('templates/header');
    $this->load->view('teachers/posts/index');
    $this->load->view('templates/footer');   
}

所以我獲取了頁面。 此時,我現在可以通過/posts/index/1並查看我的頁面

在我的頁面posts/index.php我有一個數據,它是通過 Ajax 獲取的,它是

所以我已經在posts/showPosts獲取了這些數據

showAllQuestions();

//Show all data
function showAllQuestions(){
    $.ajax({
        type: 'ajax',
        url: '<?php echo base_url() ?>posts/showPosts',
        async: false,
        dataType: 'json',
        success: function(data){
            var html = '';
            var i;
            var n=1;

            for(i=0; i<data.length; i++){
                html +='<div class="card">'+
                    '<div class="card-header" style="color:white; background-color:black">'+
                        '<h4><span class="iconify" data-icon="ant-design:info-circle-outlined" data-inline="false"></span> Question No. '+ n++ +'</h4>'+
                    '</div>'+
                    '<div class="card-body">'+
                        '<form>'+
                            '<div class="form-group">'+
                                '<label for="exampleFormControlTextarea1"><h5> <span class="iconify" data-icon="emojione:check-mark-button" data-inline="false"></span> </h5></label>'+
                                '<input type="text" value="'+data[i].question+'" class="form-control" disabled />'+
                            '</div>'+
                            '<hr>'+
                            '<a href="<?php echo base_url() ?>posts/edit/'+data[i].id+'" class="btn btn-info"><span class="iconify" data-icon="el:edit" data-inline="false"></span> </a>&nbsp;'+
                            '<a href="<?php echo base_url()?>posts/delete/'+data[i].id+'" class="btn btn-primary"><span class="iconify" data-icon="fa-solid:trash-alt" data-inline="false"></span></a>'+
                            //  '<a href="javascript:;" class="btn btn-info item-edit" data-id="'+data[i].id+'">Edit </a>&nbsp;'+
                        '</form>'+
                    '</div>'+
                '</div><br>';
            }

            $('#showdata').html(html);
        },
        error: function(){
            alert('Could not get Data from Database');
        }
});

}

posts/showPosts - Posts.php,這是控制器

public function showPosts(){
    $result = $this->post_model->showPosts();
    echo json_encode($result);
}

最后是根據我提交的數據 ID 確定我是否獲取正確 ID 的模型

問題是 $id 為空,我沒有線索開始,因為我在視圖頁面上聲明了一個隱藏的輸入值。

public function showPosts(){
    // Show questions and answers
    $id = $this->input->post('txtGroup');
    $this->db->select('*');
    $this->db->from('questions');
    $this->db->where('group_id', $id);
    $query = $this->db->get();
    return $result = $query->result_array();

}

我將嘗試比您之前的問題更好地理解這個問題(現在可以安全刪除,因為這是對您的問題的更好描述)。

在您的groups/index.php視圖中,您希望允許用戶導航posts/index頁面並傳遞一個整數作為單個參數(由您硬編碼,而不是由用戶輸入)--我將其稱為$txtGroup作為上下文。 因為您沒有執行INSERT | UPDATE | DELETE操作,更好的做法是將數據作為$_GET而不是$_POST 因為 Codeigniter 允許將$_GET參數作為 url 的斜杠分隔擴展名傳遞,所以我認為設置<form>沒有任何好處。

使用您喜歡的類將您的新超鏈接設置為按鈕。

<a href="<?php echo base_url("posts/index/{$integer}"); ?>">Link to <?php echo $integer; ?></a>

這會將您的數據發送到posts/index.php控制器。 這是您應該從 url 訪問/提取$txtGroup值的地方。

這就是我開始對你需要什么感到模糊的地方。 如果要將值傳遞給teachers/posts/index視圖,請在加載視圖時傳遞關聯數組作為第二個參數。

public function index($txtGroup) {
    $this->load->view('templates/header');
    // if you want to pass the value to THIS 
    $this->load->view('teachers/posts/index', ['txtGroup' => $txtGroup]);
    $this->load->view('templates/footer');   
}

如果您對將$txtGroup傳遞給視圖不感興趣,那么將值從groups/index.php傳遞給posts/index的唯一其他原因是修改查詢,然后將動態數據傳遞給teachers/posts/index視圖(無論如何在加載視圖時都需要提供)。

public function index($txtGroup) {
    $data['posts'] = $this->post_model->showPosts($txtGroup);
    $this->load->view('templates/header');
    $this->load->view('teachers/posts/index', $data);
    $this->load->view('templates/footer');   
}

在您的模型中,使用傳遞的參數。

public function showPosts($groupId) {
    return $this->db->get_where('questions', ['group_id' => $groupId])->result_array();
}

這樣,您就不需要使用 ajax 調用(看起來您無論如何都無條件地在您的視圖中觸發)。 現在 Codeigniter 將發揮它的魔力將$data到您的視圖,您可以通過它訪問多維結果集$posts (該變量由您分配的第一級鍵命名)。 使用foreach()循環迭代結果行並生成您的 html 內容——所有這些都使用服務器端語言。

<?php foreach ($posts as $index => $post) { ?> 
    <div class="card">
    // ... the rest of your content ...
<?php } ?>

當您使用上述循環設計新的動態內容時:

  • 使用$index作為您的計數器,這樣您就不必手動遞增。
  • 不要INSERT | UPDATE | $_GET事件觸發的DELETE操作,這不是最佳實踐。 這些類型的操作只能由$_POST提交啟動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM