php - 无法在 codeigniter 中按类别过滤数据
问题描述
使用 URL 过滤 php 数据
我正在开发一个在线商店,我想在其中显示类别明智的数据。但我不能!我的代码或 URL 有什么问题吗?
我通过如下 URL
http://localhost/e-bookshop/users/all_books?ctg=1
我的模型:
public function get_books()
{
$this->db->select('*');
$this->db->from('category');
$this->db->join('books', 'books.categoryId = category.id');
if(isset($_GET['ctg']))
{
$a = $_GET['ctg'];
$query = $this->db->where('category.id', '$a');
$query = $this->db->get();
return $query->result();
}
$this->db->order_by('books.id', 'DESC');
$query = $this->db->get();
return $query->result();
}
这没有显示任何错误,它只是显示空白或什么都没有。我该如何解决这个问题?提前谢谢。
解决方案
你非常接近你想要的。我觉得你的网址没问题。
问题就在这里。你不能像这样传递一个数组。您以字符串格式传递数组。对于 apostope coma,URL 无法读取您的数组。你必须改变这条线。
$query = $this->db->where('category.id', '$a');
而不是这一行,写成下面的行
$query = $this->db->where('category.id', $a);
我认为以上这行可以解决您的问题。只需复制此行并享受。
推荐阅读
- r - 如何遍历单个数据框中的两个单独的列列表以在 R 中创建多个表?
- scala - 如何编写一个泛型函数,该函数采用一个泛型,该泛型实现一个采用其他泛型的类
- actions-on-google - 请求同步始终返回 404:“错误:未找到请求的实体。”
- rust - 改变选项生命周期的模式匹配是什么
没有模式匹配如何实现? - java - 如何在 Jooq 生成之前应用 Flyway Java 迁移?
- r - R:两组的平均(和标准差)出现次数
- azure - New-AzWebAppSSLBinding:操作返回了无效状态代码“NotFound”
- solr - SOLR 7.7 - 连接给出未知字段错误消息
- postgresql - Ubuntu 18(作为虚拟机)上 Docker 内的 Spring Boot 应用程序未连接到主机 PostgreSQL。PSQLException:连接被拒绝
- python - 如何创建一个在 tf.estimator.train_and_evaluate 评估步骤中持续存在的变量?