使用 Codeigniter 3 无法从 MySQL table 的下一行获取数据的原因是什么?
What causes this failure to get the data from the next row in a MySQL table, with Codeigniter 3?
我正在使用 CodeIgniter 3.1.8 和 Bootstrap 4.
在单个post视图的底部,我想添加一个link到下一个post(以及前一个post)。为此,我需要获取下一个 post(posts
table 中的行)的数据(slug、标题等)。
为此,我已将此方法添加到我的 Posts_model 模型中:
/* Next post */
public function get_next_post($slug) {
$query = $this->db->get_where('posts', array('slug' => $slug));
if ($query->num_rows() > 0) {
$data = $query->next_row();
return $data;
}
}
在我的控制器中:
public function post($slug) {
//more code
$data['post'] = $this->Posts_model->get_post($slug);
$data['next_post'] = $this->Posts_model->get_next_post($slug);
print_r($data['next_post']);
//more code
}
编辑: 在 Posts_model 中,我现在有:
/* Next post */
public function get_next_post($slug) {
$query = $this->db->get('posts');
$row_index = 6;
$data = $query->row_array($row_index);
if ($query->num_rows() > 0) {
$data = $query->next_row();
return $data;
}
}
/* Prev post */
public function get_prev_post($slug) {
$query = $this->db->get('posts');
$row_index = 6;
$data = $query->row_array($row_index);
if ($query->num_rows() > 0) {
$data = $query->previous_row();
return $data;
}
}
这意味着如果我可以通过 slug 获取当前 post 的索引,我可以替换第 7 个 post - $row_index = 6
- 的这个硬编码索引 - 问题将得到解决。
我该怎么做?
编辑:这个 post 回答了原来的问题。同时,OP 编辑使用了以下代码。
您需要return查询结果:$data = $query->row_array();
而get_where()
将记录集限制为一条记录,因此没有下一条记录。您需要 return 完整的记录集 $this->db->get('posts')
。如果您知道包含 $slug
的行的 row_number(例如:5),您可以指向它。显示的 next_row 是第 6 行。
public function get_next_post($slug) {
$query = $this->db->get('posts'); // querying the whole data-set
$data = $query->row_array(5); // the missing line
if ($query->num_rows() > 0) {
$data = $query->next_row();
return $data;
}
}
现在您应该得到下一行(如果存在),请参阅 Result Rows
// Your_model.php
...
public function getPost($slug) {
$this->db->where('slug', $slug);
return $this->db->get('posts_table')->row_array();
}
public function getPrevPost($currentPostId) {
$this->db->where('id <', $currentPostId);
$this->db->order_by('id', 'desc');
$this->db->limit(1);
return $this->db->get('posts_table')->row_array();
}
public function getNextPost($currentPostId) {
$this->db->where('id >', $currentPostId);
$this->db->limit(1);
return $this->db->get('posts_table')->row_array();
}
// Yourcontroller.php
...
public function getPost($slug) {
$post = $this->your_model->getPost($slug);
$data = [
'thePost' => $post,
...
'prevPost' => $this->your_model->getPrevPost($post['id']),
'nextPost' => $this->your_model->getNextPost($post['id']),
...
];
...
}
我正在使用 CodeIgniter 3.1.8 和 Bootstrap 4.
在单个post视图的底部,我想添加一个link到下一个post(以及前一个post)。为此,我需要获取下一个 post(posts
table 中的行)的数据(slug、标题等)。
为此,我已将此方法添加到我的 Posts_model 模型中:
/* Next post */
public function get_next_post($slug) {
$query = $this->db->get_where('posts', array('slug' => $slug));
if ($query->num_rows() > 0) {
$data = $query->next_row();
return $data;
}
}
在我的控制器中:
public function post($slug) {
//more code
$data['post'] = $this->Posts_model->get_post($slug);
$data['next_post'] = $this->Posts_model->get_next_post($slug);
print_r($data['next_post']);
//more code
}
编辑: 在 Posts_model 中,我现在有:
/* Next post */
public function get_next_post($slug) {
$query = $this->db->get('posts');
$row_index = 6;
$data = $query->row_array($row_index);
if ($query->num_rows() > 0) {
$data = $query->next_row();
return $data;
}
}
/* Prev post */
public function get_prev_post($slug) {
$query = $this->db->get('posts');
$row_index = 6;
$data = $query->row_array($row_index);
if ($query->num_rows() > 0) {
$data = $query->previous_row();
return $data;
}
}
这意味着如果我可以通过 slug 获取当前 post 的索引,我可以替换第 7 个 post - $row_index = 6
- 的这个硬编码索引 - 问题将得到解决。
我该怎么做?
编辑:这个 post 回答了原来的问题。同时,OP 编辑使用了以下代码。
您需要return查询结果:$data = $query->row_array();
而get_where()
将记录集限制为一条记录,因此没有下一条记录。您需要 return 完整的记录集 $this->db->get('posts')
。如果您知道包含 $slug
的行的 row_number(例如:5),您可以指向它。显示的 next_row 是第 6 行。
public function get_next_post($slug) {
$query = $this->db->get('posts'); // querying the whole data-set
$data = $query->row_array(5); // the missing line
if ($query->num_rows() > 0) {
$data = $query->next_row();
return $data;
}
}
现在您应该得到下一行(如果存在),请参阅 Result Rows
// Your_model.php
...
public function getPost($slug) {
$this->db->where('slug', $slug);
return $this->db->get('posts_table')->row_array();
}
public function getPrevPost($currentPostId) {
$this->db->where('id <', $currentPostId);
$this->db->order_by('id', 'desc');
$this->db->limit(1);
return $this->db->get('posts_table')->row_array();
}
public function getNextPost($currentPostId) {
$this->db->where('id >', $currentPostId);
$this->db->limit(1);
return $this->db->get('posts_table')->row_array();
}
// Yourcontroller.php
...
public function getPost($slug) {
$post = $this->your_model->getPost($slug);
$data = [
'thePost' => $post,
...
'prevPost' => $this->your_model->getPrevPost($post['id']),
'nextPost' => $this->your_model->getNextPost($post['id']),
...
];
...
}