[英]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> '+
'<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');
}
});
} }
In posts/showPosts
- Posts.php, this is the controller在
posts/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:当您使用上述循环设计新的动态内容时:
$index
as your counter so that you don't have to manually increment.$index
作为您的计数器,这样您就不必手动递增。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.