Cat*_*Can 6 php mysql codeigniter
我正在使用Academy LMS源代码在Github 中, 它是一个学习管理系统,旨在为每个课程设置一个类别
我想为一门课程添加多个类别
// 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);
}
Run Code Online (Sandbox Code Playgroud)
然后在/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;
}
Run Code Online (Sandbox Code Playgroud)
然后在/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');
}
Run Code Online (Sandbox Code Playgroud)
在 SQL 课程表中有一个名为 category_id int(11) 的列,它为每个课程存储一个类别我已将其更改为 TEXT 格式并放置逗号分隔的值,如 1,2,3 并使用类似的方式
$this->db->where_in('category_id',$category_id)
Run Code Online (Sandbox Code Playgroud)
和
$this->db->like('category_id',$category_id)
Run Code Online (Sandbox Code Playgroud)
和
$this->db->where("FIND_IN_SET(".$category_id.",category_id) >", 0);
Run Code Online (Sandbox Code Playgroud)
并没有得到任何结果我只需要课程在 category_id 列中有逗号分隔的值
简单地说,我希望课程有多个类别
数据库表 https://pasteboard.co/JNSxO2kz.png
课程表 https://pasteboard.co/JNSy7g0.png
类别表 https://pasteboard.co/JNSyhlk.png
category_id 表(@micheal-la-ferla 建议的映射表) https://pasteboard.co/JNSyGGn.png
有人可以帮忙吗?
Academy-LMS 的设计方式使您只能为每门课程添加 1 个类别。它限制了用户并且可能令人沮丧。
一种可行的解决方法是创建一个包含 3 列的新映射表,如下所示:
Field Name | Data Type
------------+------------------
ID | int(11)
Course ID | int(11)
Category ID | int(11)
Run Code Online (Sandbox Code Playgroud)
显然,您需要拥有创建新表的权限才能使用此解决方法。不确定这是否适用于您实施 Academy-LMS。
上述步骤本质上会在课程和类别之间创建一对多关系,以便一门课程可以成为多个类别的一部分。(实际上,这将是多对多关系,因为我假设一个类别显然可能属于多个课程)。
因此,数据库设计将与我在这里创建的数据库设计类似。
如果您实施此更改,则需要对您的代码进行以下更改:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
本质上,我在这里所做的是将parent类别中的替换为category_id和。不确定这些是否正确。从数据库中读取数据时,您需要查看这些内容,因为我从未使用过 CodeIgniter。categorycourse_id
| 归档时间: |
|
| 查看次数: |
229 次 |
| 最近记录: |