如何将输入值从视图传递到控制器和模型以正确获取数据?点火枪 [英] How can I pass input value from view to controller and model to fetch data correctly? Codeigniter
问题描述
我正在尝试获取具有与输入隐藏值相同ID的数据。
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
首先,这是我做的工作,目的是使我更清楚地调试我制作的硬编码表单和一个带我进入页面的按钮
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
视图中- groups / index.php
如您所见,我制作了
posts / index / 1
这是一个硬编码的值,但以后可以轻松更改,这不是我的问题
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
<?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.php
public function index(){
$this->load->view('templates/header');
$this->load->view('teachers/posts/index');
$this->load->view('templates/footer');
}
于是我拿到了页面。此时,我现在可以浏览 / posts / index / 1
并查看我的页面
And so I fetched the page. At this point, i can now go through /posts/index/1
and see my page
在我的页面中 posts / index.php 我有一个数据,它是通过Ajax提取的
In my page posts/index.php I have a data and it is fetched through Ajax here it is
在
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> '+
'<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> '+
'</form>'+
'</div>'+
'</div><br>';
}
$('#showdata').html(html);
},
error: function(){
alert('Could not get Data from Database');
}
});
}
在 posts / showPosts
-Posts.php,这是控制器
In posts/showPosts
- Posts.php, this is the controller
public function showPosts(){
$result = $this->post_model->showPosts();
echo json_encode($result);
}
最后,该模型根据数据ID确定是否获取了正确的ID我提交了
Finally the Model to Determine if I fetched the correct ID depending on the data id I submit
问题是$ id为null并且我没有头绪,因为我声明了一个隐藏的输入值
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.
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).
在您的<$ c在$ c> groups / index.php 视图中,您要允许用户导航到帖子/索引
页面并传递一个整数作为单个参数(由您硬编码,而不由用户输入)-在上下文中,我将其称为 $ txtGroup
。因为您没有执行 INSERT
| UPDATE
| Delete
操作上,更好的做法是将数据作为 $ _ GET
而不是 $ _ POST
发送。因为Codeigniter允许将 $ _ GET
参数作为URL的斜杠分隔扩展名传递,所以在设置<时看不到任何好处; form>
。
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. Because you are not performing a INSERT
|UPDATE
|DELETE
operation, better practice is to send the data as a $_GET
instead of $_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>
.
使用您喜欢的类将新的超链接设置为按钮样式。
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>
这会将您的数据发送到 posts / index.php
控制器。这是您应该从URL访问/提取 $ txtGroup
值的地方。
This will send your data to the posts/index.php
controller. This is where you should be accessing/extracting the $txtGroup
value from the url.
在这里,我开始弄不清楚您需要什么。如果要将值传递给 teachers / posts / index
视图,请在加载视图时通过将关联数组作为第二个参数传递。
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.
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
视图(
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).
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();
}
这样,您不需要使用ajax调用(看起来就像您无条件触发视图一样)。现在,Codeigniter将发挥魔力将 $ data
传输到您的视图中,从而您可以将多维结果集作为 $ posts
进行访问(变量由您为其分配的第一级键命名)。使用 foreach()
循环遍历结果行并生成html内容-全部使用服务器端语言。
This way, you don't need to use an ajax call (which looks like you are unconditionally triggering in your view anyhow). 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). Use a foreach()
loop to iterate the rows of results and generate your html content -- all using server-side language.
<?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:
- 使用
$ index
作为计数器,这样就不必手动递增。 - 请勿不允许
插入
|更新
|由
操作不是最佳实践。这些类型的操作只能由$ _ GET
事件触发的DELETE$ _ POST
提交来发起。
- Use
$index
as your counter so that you don't have to manually increment. - DO NOT allow
INSERT
|UPDATE
|DELETE
operations to be triggered by$_GET
events, this is not best practice. Those types of actions should only be initiated by$_POST
submissions.
这篇关于如何将输入值从视图传递到控制器和模型以正确获取数据?点火枪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!