如何在视图中的 JavaScript 函数中调用模型数据库查询?
在我的用户消息系统的用户收件箱中,我正在构建一个功能,用户可以将电子邮件消息设为最喜欢的消息。
我在消息模型中创建了 2 个方法。将“messages”表中的“favourite”列中的 0 更新为 1,这意味着用户希望该消息成为收藏夹。此外,用户单击以收藏该消息的图像也会从灰色变为彩色。
然后,如果用户想让该消息不被收藏,我会再次执行相同的过程。 1 更新为 0,图像再次变灰。
下面是我的 jquery/javascript,如果选中该框(图像为彩色),它基本上会执行某些操作;如果未选中该框(图像呈灰色),则它会执行其他操作。
// favourite check box
$('input.favourite:checkbox').simpleImageCheck({
image: '<?php echo base_url()?>images/messages/check.png',
imageChecked: '<?php echo base_url()?>images/messages/unchecked.png',
afterCheck: function(isChecked) {
if (isChecked) {
//query to db from php to update favourite number to 1
}
else (!isChecked)
{
//query to db from php to update favourite number to 0
}
}
});
我想要找出的是在模型中调用 2 db 查询的最佳方法:
public function favourite_checked($message_id)
{
$username = $this->session->userdata('username');
return $this->db->query("UPDATE messages SET favourite = 1 WHERE id = $message_id AND to_user = '$username'");
}
public function favourite_unchecked($message_id)
{
$username = $this->session->userdata('username');
return $this->db->query("UPDATE messages SET favourite = 0 WHERE id = $message_id AND to_user = '$username'");
}
执行此操作的最佳方法是什么?直接在视图中调用模型是不好的做法,不是吗?
那么我怎样才能实现我想要实现的目标呢?
控制者可以充当中间人吗?
如果是这样,我该如何在 javascript 函数中调用控制器呢?
我一直很感激这里给出的建议。提前致谢
On my user inbox from my user messaging system I am building a feature where a user can make an email message a favourite message.
I have created 2 methods in my messages model. One updates the 0 in the 'messages' table, 'favourite' column to 1 which would mean the user wants the message to be a favourite. Also the image the user clicks on to make the message a favourite turns from grey to colour.
Then I do this same process again if a user wants to make the message not a favourite. The 1 is updated to a 0 and the image turns greyed out again.
Below is my jquery/javascript that basically does something if the box is checked (image is in colour) and something else if the box is not unchecked (image greyed out).
// favourite check box
$('input.favourite:checkbox').simpleImageCheck({
image: '<?php echo base_url()?>images/messages/check.png',
imageChecked: '<?php echo base_url()?>images/messages/unchecked.png',
afterCheck: function(isChecked) {
if (isChecked) {
//query to db from php to update favourite number to 1
}
else (!isChecked)
{
//query to db from php to update favourite number to 0
}
}
});
What I am trying to figure out is the best way to called my 2 db queries in my model:
public function favourite_checked($message_id)
{
$username = $this->session->userdata('username');
return $this->db->query("UPDATE messages SET favourite = 1 WHERE id = $message_id AND to_user = '$username'");
}
public function favourite_unchecked($message_id)
{
$username = $this->session->userdata('username');
return $this->db->query("UPDATE messages SET favourite = 0 WHERE id = $message_id AND to_user = '$username'");
}
What would be the best way to do this? Calling a model directly in a view is bad practice isn't it?
So how could I achieve what I would like to achieve?
Could a controller act as a middle man?
If so how would I even call a controller in a javascript function?
I always appreciate the advice given on here. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 jQuery 中,您可以对 CodeIgniter 控制器使用 Ajax get 请求。假设您的控制器称为 users,您的函数称为 update_favorites。 jQuery 调用可能如下所示:
然后,您的控制器将找到 user_id 和 message_id 的 URL 参数,并使用这些参数加载您的模型。该模型将对数据库进行更改。
祝你好运!
In the jQuery, you would use an Ajax get request to a CodeIgniter controller. Say your controller is called users, and your function is called update_favorites. The jQuery call might look like this:
Your controller would then find the URL arguments for user_id and message_id, and load your model with those arguments. The model would make changes to the database.
Good luck!
除了 Summer 的 $.get 方法之外,您还可以以大致相同的方式使用 jQuery 的 $.post() 方法。我喜欢用
post 更好,因为 Codeigniter 的编写是为了支持 post 请求而不是 get 请求。
in addition to Summer's $.get method, you can use jQuery's $.post() method in much the same way. I like using
post better because Codeigniter was written to favor post requests over get requests.