0
votes

I am using CodeIgniter Framework, i have two li tag, i want to fill first li with all data of first table, and depending on the id if matched from the first table and second table i have another select query which gets data and show it in second li tag. Here is the working code in core php but cannot be run in CodeIgniter, but the problem is i only can get data from one table and passed it to view from controller.Please help me how i can use two tables data in view.

<?php

$sql = 'SELECT * FROM course_sections ORDER BY item_order ASC';
$query = $pdo->prepare($sql);
$query->execute();
$list = $query->fetchAll();


foreach ($list as $rs) {    ?>

<li style="sample_style">  <?=$rs['title'];?> </li>


<?php

$sql = 'SELECT * FROM course where itemid = :pid ORDER BY item_order ASC';
$query = $pdo->prepare($sql);
$query->bindParam(':pid', $rs['id'], PDO::PARAM_INT);
$query->execute();
$f_list = $query->fetchAll(); 

foreach ($f_list as $get_final_list) {   ?>

<li style="sample_style"> <?=$get_final_list['title'];?> </li>

<? } }  ?>

CodeIgniter Code:

Model function:

function get_all_topics()
{
   $this->load->database();
   $this->db->select("*");
   $this->db->from("course_sections");
   $this->db->order_by("item_order", "ASC");
   $query=$this->db->get();
   return $query->result();
}

Controller function :

function index()
{
   $this->load->model('drag_drop_model');
   $data['query']=$this->drag_drop_model->get_all_topics();
   $this->load->helper('url');
   $this->load->view('drag_drop_course_material', $data);
}
1
Why on view you have sql? Should be on a model funciton - Mr. ED
@wolfgang1983 No i don't have sql in view upper one is code written in core php which is working but i am facing above discussed issues in CodeIgniter. - user3653474
you want to fetch data for second one whenever a change is made in first one? - Niranjan N Raju

1 Answers

0
votes

try this code,

Your Model Code:-

function get_all_topics()
{
   $this->load->database();
   $this->db->select("cs.title,group_concat(c.title) as course_title");
   $this->db->from("course_sections cs");
   $this->db->join("course c","c.id=cs.itemid","left");
   $this->db->order_by("cs.item_order", "ASC");
   $this->db->group_by("c.id");
   $query=$this->db->get();
   return $query->result();
}

And In your vie:-

foreach ($list as $rs) {    ?>

<li style="sample_style">  <?=$rs['title'];?> </li>


$f_list = explode(",",$rs['course_title']);

foreach ($f_list as $get_final_list) {   ?>

<li style="sample_style"> <?=$get_final_list;?> </li>

<? } }  ?>