简体   繁体   English

如何将输入值从视图传递到控制器和模型以正确获取数据? 代码点火器

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

I am trying to fetch a data that has the same id of my input hidden value.我正在尝试获取与我的输入隐藏值具有相同 ID 的数据。

I cannot fetch the id or input hidden value of the data我无法获取数据的 id 或输入隐藏值

To start with, this is what I've done to make things clearer for me to debug things I made a hard coded form and a button to bring me to the page首先,这是我所做的,以使事情更清晰,以便我调试我制作了一个硬编码表单和一个将我带到页面的按钮

In View - groups/index.php在视图中 -组/index.php

As you can see I made the posts/index/1 which is a hard coded value but I can change that later easily that is not my problem如您所见,我创建了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>

So after I made the form I will make a function in controller to fetch the posts/index所以在我制作表格后,我将在控制器中创建一个函数来获取posts/index

In Controller - Posts.php在控制器中 - Posts.php

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

And so I fetched the page.所以我获取了页面。 At this point, i can now go through /posts/index/1 and see my page此时,我现在可以通过/posts/index/1并查看我的页面

In my page posts/index.php I have a data and it is fetched through Ajax here it is在我的页面posts/index.php我有一个数据,它是通过 Ajax 获取的,它是

So I already fetched this data in posts/showPosts所以我已经在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');
        }
});

} }

In posts/showPosts - Posts.php, this is the controllerposts/showPosts - Posts.php,这是控制器

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

Finally the Model to Determine if I fetched the correct ID depending on the data id I submit最后是根据我提交的数据 ID 确定我是否获取正确 ID 的模型

Problem is the $id is null and I don't have a clue to start with, because I declare a hidden input value on the view page.问题是 $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();

}

I am going to try to understand this question better than your previous one (which is now safe to delete since this is a better description of your issue).我将尝试比您之前的问题更好地理解这个问题(现在可以安全删除,因为这是对您的问题的更好描述)。

In your groups/index.php view, you want to allow a user to navigate to the posts/index page and pass an integer as a single parameter (which is hard-coded by you, not entered by the user) -- I'll refer to it as $txtGroup for context.在您的groups/index.php视图中,您希望允许用户导航posts/index页面并传递一个整数作为单个参数(由您硬编码,而不是由用户输入)--我将其称为$txtGroup作为上下文。 Because you are not performing a INSERT |因为您没有执行INSERT | UPDATE | UPDATE | DELETE operation, better practice is to send the data as a $_GET instead of $_POST . DELETE操作,更好的做法是将数据作为$_GET而不是$_POST Because Codeigniter enables the passing of $_GET parameter as a slash-delimited extension of the url, I don't see any benefit in setting up a <form> .因为 Codeigniter 允许将$_GET参数作为 url 的斜杠分隔扩展名传递,所以我认为设置<form>没有任何好处。

Style your new hyperlink as a button using your preferred classes.使用您喜欢的类将您的新超链接设置为按钮。

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

This will send your data to the posts/index.php controller.这会将您的数据发送到posts/index.php控制器。 This is where you should be accessing/extracting the $txtGroup value from the url.这是您应该从 url 访问/提取$txtGroup值的地方。

Here's where I start to get foggy about what you need.这就是我开始对你需要什么感到模糊的地方。 If you want to pass the value to the teachers/posts/index view, then do so by passing an associative array as the second parameter when you load the view.如果要将值传递给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');   
}

If you are not interested in passing the $txtGroup to the view, then the only other reason to pass the value from groups/index.php to posts/index would be to modify a query and then pass dynamic data to the teachers/posts/index view (which you would need to supply when loading the view anyhow).如果您对将$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');   
}

In your model, use the passed argument.在您的模型中,使用传递的参数。

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

This way, you don't need to use an ajax call (which looks like you are unconditionally triggering in your view anyhow).这样,您就不需要使用 ajax 调用(看起来您无论如何都无条件地在您的视图中触发)。 Now Codeigniter will work its magic to transfer $data to your view whereby you can access the multidimensional result set as $posts (the variable is named by the first-level key that you assign it).现在 Codeigniter 将发挥它的魔力将$data到您的视图,您可以通过它访问多维结果集$posts (该变量由您分配的第一级键命名)。 Use a foreach() loop to iterate the rows of results and generate your html content -- all using server-side language.使用foreach()循环迭代结果行并生成您的 html 内容——所有这些都使用服务器端语言。

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

As you design the new dynamic content using the above loop:当您使用上述循环设计新的动态内容时:

  • Use $index as your counter so that you don't have to manually increment.使用$index作为您的计数器,这样您就不必手动递增。
  • DO NOT allow INSERT |不要INSERT | UPDATE | UPDATE | DELETE operations to be triggered by $_GET events, this is not best practice.$_GET事件触发的DELETE操作,这不是最佳实践。 Those types of actions should only be initiated by $_POST submissions.这些类型的操作只能由$_POST提交启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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