-1

The thing is when i use this code

in my model :

public function get_all_subject_tasks(){
$this->db->select('*');
$this->db->from('task'); 
$this->db->where("user_id",$this->session->userdata('user_id'));  
$this->db->order_by("task_id", "desc");    
$query_result=$this->db->get();
$result=$query_result->result();
return $result;
}                    

in my controller:

public function subjects($t,$action=""){
        $data=array();
        $data['subject_tasks']=$this->Schoolmodel->get_all_subject_tasks($t);
        if($action=='asyn'){
          $this->load->view('theme/task',$data);
        }else{    
          $this->load->view('theme/include/header');
        $this->load->view('theme/include/school_sidebar');
          $this->load->view('theme/task',$data);
          $this->load->view('theme/include/footer');
        }
    }        

in my php page:

<div class="panel-body">
       <table id="teacher_table" class="table table-striped table-bordered table-condensed">
        <th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
       <?php foreach($subject_tasks as $list){ ?>
        <tr>
        <td class="task_name"><?php echo $list->task_name ?></td>
        <td class="task_desc"><?php echo $list->task_desc ?></td>
        </tr>
       <?php } ?>
        </table>
    </div>

I get all of the tasks that are in the database, without any subject filter.

So my question is How do i make the page echo the tasks based on which subject they are in?


Also here is how i have setup my database structure

  1. subject_id
  2. task_id
  3. task_name
  4. task_desc
  5. user_id

Where subject_id is the id of the subject where the task is inserted in.

2 Answers2

0
<?php 

// your model

public function getTask($subject_id)
{
    $result = $this->db
        ->where("subject_id", $subject_id) // !!!
        ->where("user_id", $this->session->userdata('user_id')) // !!!
        ->order_by("task_id", "desc")
        ->get('tasks');

    return $result->num_rows() > 0 ?
        $result->result() : false;
}

// your class

class School extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if($this->session->userdata('logged_in')==FALSE){
            redirect('User');
        }
        $this->load->database();
        $this->load->model('Schoolmodel');
        $this->load->library('form_validation');
    }

    public function subjects($subject_id,$action=""){
        $data=array();
        $data['subject_tasks'] = $this->Schoolmodel->getTask($subject_id);
        if($action=='asyn'){
            $this->load->view('theme/task',$data);
        }else{
            $this->load->view('theme/include/header');
            $this->load->view('theme/include/school_sidebar');
            $this->load->view('theme/task',$data);
            $this->load->view('theme/include/footer');
        }
    }

}

?>

// your view

    <div class="panel-body">
           <table id="teacher_table" class="table table-striped table-bordered table-condensed">
            <th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
    <?php if(false !== $subject_tasks) { foreach($subject_tasks as $list){ ?>
        <tr>
            <td class="task_name"><?php echo $list->task_name ?></td>
            <td class="task_desc"><?php echo $list->task_desc ?></td>
        </tr>
    <?php } } ?>
    </table>
    </div>
PawelN
  • 339
  • 2
  • 9
  • You don't need to use ->select(*) if you'd like to select all fields. – PawelN Jun 12 '16 at 13:29
  • You can use ->get('table') instead ->from('table') and ->get(); , its better to use method chaining :) ->met1()->met2()->... – PawelN Jun 12 '16 at 13:31
0

This is not going to work:

$this->db->where("subject_id",$subject_id AND "user_id",$this->session->userdata('user_id'));  

From Active Record documentations: Multiple calls to where will result in AND:

$this->db->where("subject_id", $subject_id);
$this->db->where("user_id", $this->session->userdata('user_id'));  
Amir Rahimi Farahani
  • 1,580
  • 1
  • 12
  • 14