- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试在 CodeIgniter 中构建一个消息传递系统。我有如下三个表。
消息:
消息线程:
messages_participants:
我可以轻松地撰写新邮件,并且收件人可以看到已收到邮件。
当有回复时,问题就来了。回复后结果重复。它显示初始消息是从发起者和接收者发送的,并且返回 4 行而不是预期的 2 行。谁能告诉我哪里出错了?
这是我的模型:
function reply_to_thread(){
//Generate random string for the ticket number
$rand = substr(str_shuffle(MD5(microtime())), 0, 11);
//Add the random string to the hash
$messageid = $rand;
$messageinsert = array(
'message_id' => $messageid,
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message'),
'thread_id' => $this->input->post('thread_id'),
);
$participantsinsert = array(
'message_id' => $messageid,
'from_id' => $this->input->post('from_id'),
'to_id' => $this->input->post('to_id'),
'thread_id' => $this->input->post('thread_id'),
);
$this->db->insert('messages',$messageinsert);
$this->db->insert('messages_participants',$participantsinsert);
}
function view_thread($thread_id){
$this->db->where('messages.thread_id', $thread_id);
$this->db->join('messages_participants', 'messages_participants.thread_id = messages.thread_id');
$this->db->join('users', 'messages_participants.from_id = users.id');
$result = $this->db->get('messages');
var_dump($result);
return $result->result_array();
}
我的 Controller :
function check_messages(){
$id = $this->session->userdata('id');
$data['thread'] = $this->message_model->check_messages($id);
$this->load->view('messages/my_messages', $data);
}
function view_thread($thread_id){
$data['thread'] = $this->message_model->view_thread($thread_id);
$this->load->view('messages/view_thread',$data);
}
function reply(){
$this->message_model->reply_to_thread();
redirect('messages/get_messages');
}
我的 View 线程 View :
<div class="well">
<h3>View Messages</h3>
<?php foreach($thread as $threadfield):?>
<div class="message">
<img class="avatar pull-left" src="<?php echo $threadfield['avatar'];?>">
<div class="message-actions">
<button class="btn btn-success" id="reply">Reply</button>
</div>
<p>
<strong>
From: <a href="profile.html"> Users id: <?php echo $threadfield['from_id'];?></a>
</strong>
<span class="badge badge-important">Unread</span><br>
<strong>Date:</strong>
<?php echo date('M j Y g:i A', strtotime($threadfield['sent']));?>
</p>
<p><strong>Subject:</strong>
<a href = "/messages/view_thread/<?php echo $threadfield['thread_id'];?>">
<?php echo $threadfield['subject'];?>
</a>
</p>
<p><strong>Message:</strong> <?php echo $threadfield['message'];?></p>
<hr>
</div>
<?php endforeach; ?>
</div>
<div class="row-fluid" id="replyform" style="display: none">
<div class="well">
<h3>Reply to <?php echo $threadfield['username'];?>'s Message.</h3>
<?php echo form_open('messages/reply');?>
<input type="text" value="<?php echo $threadfield['thread_id'];?>" name="thread_id" id="thread_id">
<input type="text" value="<?php echo $this->session->userdata('id');?>" name="from_id" id="from_id">
<input type="text" value="<?php echo $threadfield['from_id'];?>" name="to_id" id="to_id">
<input type="text" value="RE: <?php echo $threadfield['subject'];?>" name="subject" id="subject">
<input type="text" placeholder="Message......." name="message" id="message">
<button class="btn" type="submit">Submit Reply</button>
<?php echo form_close();?>
</div>
编辑:
我设法解决了这个问题,但它又产生了一个新问题。我通过修改连接解决了这个问题,所以我们没有绑定(bind)到用户但参与者的消息,但现在,我无法显示正确的用户数据来显示初始新消息,它总是显示最后的发件人详细信息或最后收件人的详细信息。这取决于我是否将连接设置为 from_id 或 to_id。有谁知道我如何将用户表连接到最后一条消息?
最佳答案
我在您的查询中没有看到任何显示最新记录的ORDER BY。
function view_thread($thread_id){
$this->db->where('messages.thread_id', $thread_id);
$this->db->join('messages_participants', 'messages_participants.thread_id = messages.thread_id');
$this->db->join('users', 'messages_participants.from_id = users.id');
$this->db->order_by('field_name_goes_here');//put the name of field to set order by..
$result = $this->db->get('messages');
return $result->result_array();
}
关于php - Codeigniter 奇怪的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18553855/
我想扩展调用 getMessage 时返回自定义消息的异常类。 class MY_Exceptions extends CI_Exceptions{ function __construct
我已经安装了一个干净的 Apache2(加上 PHP 和 MySQL)服务器并启用了 mod_rewrite在 apache 配置中。 我添加了 .htaccess文件以从 url 中删除 index
我正在使用上传类上传图片。但是我上传的图片的存储位置是:http://www.mysite.com/uploads/ 此文件夹的绝对路径是:c:\wamp\www\mysite\uploads\ 应用
大家好 我想在codeigniter上下文中提供一些静态html页面。我不想绕过base_url中的index.php文件。 但是,当我使用对HTML文件的调用时,它不会显示404错误页面。 感谢已经
我一直想知道在模型中以 OO 风格编写代码的正确方法是什么。当然,您可以拥有一个从数据库中检索数据然后映射到模型级变量的函数。但是,当您在模型中有其他功能试图从 BD 获取其他数据时,这种方法会变得违
之前所有的 JOIN 尝试都给我留下了填充结果的 id、标题键的光盘或项目数据(可能发生了冲突)。 所以我有: item table fields: id, title disc table fiel
假设我在 Controller 中有一个名为 的方法 book($chapter,$page); 其中 $chapter 和 $page 必须是整数。要访问该方法,URI 将如下所示 book/cha
我有一个用户可以注册的页面。他在此过程中上传了个人资料照片。我想限制大小,但除了 $config['maxsize'] 之外,并没有太多强调 codeigniter 文档。我尝试了以下但我没有收到任何
我需要将 CodeIgniter 设置为真正的多语言网站。我已经搜索过,但找不到解决方案。 我已经测试了这种方法,但它不起作用。 ( http://codeigniter.com/wiki/Categ
CodeIgniter 中的常量是否可以用于整个站点中的重复文本(比如元标记和元描述)?就像是: define('METADESCRIPTION', 'This is my site'); 然后将 M
我已经在 CodeIgniter 的路由器中写了这个。 $route['companyname'] = "/profile/1"; 这工作正常,但是当我在 URL 中键入“公司名称”时,它不起作用。这
我正在开始我的第一个 CodeIgniter 项目,并希望在开始之前获得一些建议。我对 Controller 和模型的名称如何工作感到有些困惑。 如果我希望我公司页面的网址为 http://examp
可以在CodeIgniter Active Record中使用多个INSERT记录,而无需for,foreach等。 我当前的代码: foreach($tags as $tag) { $tag
SELECT * FROM certs WHERE id NOT IN (SELECT id_cer FROM revokace); 如何在 CodeIgniter 事件记录中编写上述 select
wkhtmltopdf 听起来是一个很好的解决方案...问题是 exec 上没有任何反应 shell_exec("c:\wkhtmltopdf.exe","http://www.google.com
我当前的CodeIgniter有点问题。我有一个带有“页面” Controller 的CI安装程序,该 Controller 可从/ views加载静态文件,但它最多只能包含1个子文件夹,而我正在向其
有一段时间,我一直在处理分页类中的一个问题。 问题是,除了第 1 页的链接之外,所有分页的内容都可以。 所有链接都是这样的: example.com/method/page/2 example.com
我想对请求进行一些预处理和后处理,例如处理身份验证、加载上下文数据、性能时间等等。来自 Django 的概念是 MIDDLEWARE_CLASSES这让我可以在各个阶段处理请求:https://doc
我想通过创建自己的库和配置文件在 CodeIgniter 中生成全局变量。这就是我在我的库文件中编写的,比如说 globalvars.php。我把它放在/application/libraries 中
我有以下分页样式 Previous Page
我是一名优秀的程序员,十分优秀!