Juus site developer Juus site developer - 5 months ago 33
MySQL Question

Codeigniter DATABASE Trouble

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.

Answer
<?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>
Comments