Php 删除函数总是选择相同的ID-blog-Codeigniter
所以,我一直试图让这个删除函数现在工作一段时间 在foreach的底部有一个delete函数。函数本身也可以工作,但是它总是选择post id为1 看法 模型 编辑: 在模型中获取_帖子Php 删除函数总是选择相同的ID-blog-Codeigniter,php,codeigniter,Php,Codeigniter,所以,我一直试图让这个删除函数现在工作一段时间 在foreach的底部有一个delete函数。函数本身也可以工作,但是它总是选择post id为1 看法 模型 编辑: 在模型中获取_帖子 public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){ if($limit){ $this->db->limit($limit, $offset); } if($slug === FAL
public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->order_by('posts.id', 'DESC');
$this->db->join('categories', 'categories.id = posts.category_id');
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->row_array();
}
函数确实运行了,我收到了一条确认消息,所以我唯一真正感到困惑的是Id。
整个过程都是使用CodeIgniter框架编写的。使用row\u array()函数只返回1行,因此应该替换为result\u array():
感谢大家,你们的回答对我帮助很大,我也学到了一些关于PHP的新东西。我试着实现你的想法,他们解决了我所问的问题。最后,我决定使用一个简单的mysqli查询从数据库中获取所需的数据<代码>mysqli_查询($con,“选择id、类别id、用户id、标题、正文,根据id描述从帖子中创建”)代码>正确检查您的get\u post型号。从您所拥有的信息来看,您的查询将从category表中获取其id 试试这个
public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->select('posts.id AS id, posts.slug, posts.body, posts.created_at');
$this->db->from('posts, categories');
$this->db->where('categories.id = posts.category_id');
$query = $this->db->get();
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->result_array();
}
将join更新为LEFT join或simple use WHERE可能我错了,但我认为这是foreach语法问题, 因为如果你在所有地方都有相同的身份证 排, 你需要像这样写
<?php foreach ($posts as $post) : ?>
instead of,
<?php foreach ($posts as $post) { ?>
//your code goes here
<?php } ?>
而不是
//你的密码在这里
在您的视图中回显$post['id']时,请确保每个帖子都有不同的值PHPmyAdmin表中的id是自动递增的,因此每个帖子都有不同的值。是的,我理解这一点,但我不知道如何使用函数get_posts获取数据,因此我想问您是否看到$post['id']的不同值在这一行中,当您按F12时,print\r($data)的结果是什么调用VIEW之前,控制器中的code>始终显示值1@RildoGomez@frankjamesleo我认为这就是您试图更新代码的原因,使用WHERE并在查询中指定select posts.id,而不是选择category.id的模糊语句,因为idIt实际上并不总是相同的post id,它被设置为职位类别的ID。我确实使用了,效果很好。谢谢你的回答。。。我真的不知道这个方法,所以这就是为什么我要花牙套,谢谢,
public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->order_by('posts.id', 'DESC');
$this->db->join('categories', 'categories.id = posts.category_id');
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->row_array();
}
public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->order_by('posts.id', 'DESC');
$this->db->join('categories', 'categories.id = posts.category_id');
$query = $this->db->get('posts');
return $query->result_array();
}else{
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->result_array();
}
}
public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->select('posts.id AS id, posts.slug, posts.body, posts.created_at');
$this->db->from('posts, categories');
$this->db->where('categories.id = posts.category_id');
$query = $this->db->get();
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->result_array();
}
<?php foreach ($posts as $post) : ?>
instead of,
<?php foreach ($posts as $post) { ?>
//your code goes here
<?php } ?>