【发布时间】:2010-09-26 21:07:10
【问题描述】:
我正试图弄清楚如何在我的第一个 CI 应用程序(也是唯一的第二个 PHP 应用程序)中编写语句,但我被卡住了。
我有一个联结表,其中包含各自表中的 book_id 和 user_id。我想找出谁读过一本书并将其回显,但我需要格式化才有意义。这是查询:
$readQuery = $this->db->get_where('books_users', array('book_id' => $isbn));
这是我对一个人的逻辑
// Get my user info
$user = $this->ion_auth->get_user();
// Tell me who has read it.
$this->db->select('user_id');
$readQuery = $this->db->get_where('books_users', array('book_id' => $isbn));
// If only one person has read it
if ($readQuery->num_rows() == 1) {
$readResult = $readQuery->row();
// And if that person was me...
if ($readResult->user_id == $user->id) {
$message = 'You have read this.';
// If it was someone else...
} else {
$reader = $this->ion_auth->get_user($readResult->user_id);
$message = "$reader->first_name $reader->last_name has read this";
}
// If two people have read it
}
所以如果只有一个人读过它我就很好。但是当两个人读过它时,我想让它说“名字一和名字二读过这个”。如果登录的人没有阅读它。如果他们是两个人之一,它应该说“你和名字二已经读过这个”。
3-5 以此类推。如果有 3-5 个人读过,选项将是“名字一、名字二、名字三读过这个”或“你、名字二、名字三读过这个”。最多五个人
如果是 6 个或更多,它应该只说他们两个的名字,即“你,名字两个,还有 5 个其他人已经读过这个。”
我考虑为每个实例做一个条件,然后将所有用户推送到一个数组。然后我可以使用 in_array() 查看登录用户是否已阅读并尝试报告,但我只是在处理逻辑时遇到了一些麻烦。
有没有更简单的方法?
在此先感谢您, 马库斯
【问题讨论】:
标签: php mysql activerecord codeigniter