php - Codeigniter 向类(class)添加多个类别(Academy-LMS)

标签 php mysql codeigniter

我正在使用 Academy LMS源代码在 Github
这是一个学习管理系统,它被设计为每门类(class)有一个类别
我想为一门类(class)添加多个类别
/controllers/API.php我有

  // Fetch all the categories
  public function categories_get($category_id = "") {
    $categories = array();
    $categories = $this->api_model->categories_get($category_id);
    $this->set_response($categories, REST_Controller::HTTP_OK);
  }

  // Fetch all the courses belong to a certain category
  public function category_wise_course_get() {
    $category_id = $_GET['category_id'];
    $courses = $this->api_model->category_wise_course_get($category_id);
    $this->set_response($courses, REST_Controller::HTTP_OK);
  }
然后在 /models/Api_model.php , 我有
// Get categories
public function categories_get($category_id)
{
    if ($category_id != "") {
        $this->db->where('id', $category_id);
    }
    $this->db->where('parent', 0);
    $categories = $this->db->get('category')->result_array();
    foreach ($categories as $key => $category) {
        $categories[$key]['thumbnail'] = $this->get_image('category_thumbnail', $category['thumbnail']);
        $categories[$key]['number_of_courses'] = $this->crud_model->get_category_wise_courses($category['id'])->num_rows();
    }
    return $categories;
}

// Get category wise courses
public function category_wise_course_get($category_id)
{
    $category_details = $this->crud_model->get_category_details_by_id($category_id)->row_array();

    if ($category_details['parent'] > 0) {
        $this->db->where('sub_category_id', $category_id);
    } else {
        $this->db->where('category_id', $category_id);
    }
    $this->db->where('status', 'active');
    $courses = $this->db->get('course')->result_array();

    // This block of codes return the required data of courses
    $result = array();
    $result = $this->course_data($courses);
    return $result;
}
然后在 /model/Crud_model.php , 我有
    public function get_category_details_by_id($id)
    {
        return $this->db->get_where('category', array('id' => $id));
    }

 function get_category_wise_courses($category_id = "")
    {
        $category_details = $this->get_category_details_by_id($category_id)->row_array();

        if ($category_details['parent'] > 0) {
            $this->db->where('sub_category_id', $category_id);
        } else {
            $this->db->where('category_id', $category_id);
        }
        $this->db->where('status', 'active');
        return $this->db->get('course');
    }
在 SQL 类(class)表中有一个名为 category_id int(11) 的列,它为每个类(class)存储一个类别我已将其更改为 TEXT 格式并放置逗号分隔的值,如 1,2,3
$this->db->where_in('category_id',$category_id)
$this->db->like('category_id',$category_id)
$this->db->where("FIND_IN_SET(".$category_id.",category_id) >", 0);
并没有得到任何结果我只需要类(class)在 category_id 列中有逗号分隔的值
简单地说,我希望类(class)有多个类别
数据库表
https://pasteboard.co/JNSxO2kz.png
类(class)表
https://pasteboard.co/JNSy7g0.png
类别表
https://pasteboard.co/JNSyhlk.png
category_id 表(@micheal-la-ferla 建议的映射表)
https://pasteboard.co/JNSyGGn.png
有人可以帮忙吗?

最佳答案

Academy-LMS 的设计方式是,每门类(class)只允许您添加 1 个类别。它限制了用户,并且可能令人沮丧。
一种可行的解决方法是创建一个包含 3 列的新映射表,如下所示:

Field Name  | Data Type
------------+------------------
ID          | int(11) 
Course ID   | int(11)
Category ID | int(11)
显然,您需要具有创建新表的权限才能使用此解决方法。不确定这是否适用于您实现 Academy-LMS。
上面的步骤基本上会在类(class)和类别之间创建一对多的关系,这样 1 门类(class)就可以成为多个类别的一部分。 (实际上,这将是多对多的关系,因为我假设一个类别显然可能属于多个类(class))。
因此数据库设计将类似于我创建的 here .
如果您实现此更改,则需要对您拥有的代码进行以下更改:
// Get categories
public function categories_get($category_id)
{
    if ($category_id != "") {
        $this->db->where('category_id', $category_id);
    }
    $this->db->where('parent', 0);
    $categories = $this->db->get('course_id')->result_array();
    foreach ($categories as $key => $category) {
        $categories[$key]['thumbnail'] = $this->get_image('category_thumbnail', $category['thumbnail']);
        $categories[$key]['number_of_courses'] = $this->crud_model->get_category_wise_courses($category['id'])->num_rows();
    }
    return $categories;
}
本质上,我在这里所做的是替换 parent在带有 category_id 的类别中和 categorycourse_id .不确定这些是否正确。从数据库读取时,您需要查看这些内容,因为我从未使用过 CodeIgniter。

关于php - Codeigniter 向类(class)添加多个类别(Academy-LMS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66079149/

相关文章:

php - 按最重复的值排序 mysql 列并在 php 中显示

php - 如何在 Wordpress 管理员中获取帖子 ID

php - 如何在codeigniter中获取具有特定id的最后一条记录

php - codeigniter 中的 union 我的 sql 错误

php - € 字符显示为 ?在 UTF8 输出中

php - 具有许多连接的 Mysql 查询需要很长时间才能加载

mysql - 如何在具有索引的 MySQL 数据库中存储 APNS/FCM 的推送设备 token ?

mysql - 在 Ja-sig CAS 中使用 MySQL 数据库进行身份验证

MySQL 计算一个值在另一列中出现的次数并存储在新列中

PHP 和 SQL WHERE(更改变量)