首页 > 解决方案 > 无法在 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();
    }

这没有显示任何错误,它只是显示空白或什么都没有。我该如何解决这个问题?提前谢谢。

标签: phpcodeigniterfiltering

解决方案


你非常接近你想要的。我觉得你的网址没问题。

问题就在这里。你不能像这样传递一个数组。您以字符串格式传递数组。对于 apostope coma,URL 无法读取您的数组。你必须改变这条线。

 $query = $this->db->where('category.id', '$a');

而不是这一行,写成下面的行

$query = $this->db->where('category.id', $a);

我认为以上这行可以解决您的问题。只需复制此行并享受。


推荐阅读