Codeigniter DATABASE麻烦

时间:2016-06-12 12:49:03

标签: php mysql database codeigniter

事情就是当我使用这段代码时

在我的模型中

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;
}                    

在我的控制器

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');
        }
    }        

在我的 php页面

<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>

我获得了数据库中的所有任务,没有任何主题过滤器。

所以我的问题是 如何使页面根据任务>来回显任务?

这也是我如何设置数据库结构

  1. subject_id
  2. TASK_ID
  3. TASK_NAME
  4. task_desc
  5. USER_ID
  6. 其中subject_id是插入任务的主题的ID。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

这不起作用:

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

来自Active Record documentations:多次调用where将导致AND

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