douqiao8032
2015-12-10 08:53
浏览 37
已采纳

将行变量传递给Codeigniter中的函数

I'm trying create a function where in when you click on a person's name, a list of trainings he/she has attended will show up on another page. My problem right now is how to pass the user_id from each row so that I can use it for my query.

Below is my code so far

Controller:

public function traineeRater()
{
    if(!$this->session->userdata('loginuser'))
    {
        redirect(base_url());
    }
    if($this->session->userdata('level') != 'Rater')
    {
        $this->session->sess_destroy();
        redirect(base_url());
    }
    $data['trainee'] = $this->rater_model->traineeList();
    $this->load->view('rater/trainee_rater', $data);
}

public function trainingAttended()
{
    if(!$this->session->userdata('loginuser'))
    {
        redirect(base_url());
    }
    if($this->session->userdata('level') != 'Rater')
    {
        $this->session->sess_destroy();
        redirect(base_url());
    }
    $data['attended'] = $this->rater_model->getAttended(//some variable here?);

    $this->load->view('rater/attended', $data);
}

Model:

public function traineeList()
{
      $query = $this->db->query("SELECT dept_name, position, user_id,
                                 CONCAT(fname, ' ', mname, ' ', lname) AS name 
                                 FROM users, department
                                 WHERE users.dept_id = department.dept_id
                                 ");
      return $query->result();
}

public function getAttended($id)
{
    $query = $this->db->query("SELECT training_title, immediate_rater, 
                               CONCAT(fname, ' ', mname, ' ', lname) AS name
                               FROM users, training_details, coach
                               WHERE users.user_id = coach.user_id
                               AND   training_details.dept_id = coach.dept_id
                               AND users.user_id = '".$id."'");
    return $query->result();
}

View(traineeList):

<table class="table table-striped table-bordered table-hover" id="dataTables-example">
     <thead>
          <tr>
             <th>Name</th>
             <th>Department</th>
             <th>Position</th>
          </tr>
     </thead>
     <tbody>
         <?php foreach ($trainee as $row) 
          {
              ?>
              <tr class = "odd gradeX">
               <td><a href="trainingAttended" style=" text-decoration: none; color: #000000"><?=$row->name;?></a></td>
               <td><?=$row->dept_name;?></td>
               <td><?=$row->position;?></td>
             </tr>
             <?php     
          }
          ?>      
    </tbody>        
 </table>

View(trainingAttended):

<table class="table table-striped table-bordered table-hover" id="dataTables-example">
    <thead>
        <tr>
         <th>Training Title</th>
         <th>Immediate Rater</th>
         <th>Coach</th>
        </tr>
    </thead>
    <tbody>
        <?php
          foreach ($attended as $row) 
          {
            ?>
            <tr class = "odd gradeX">
                <td><?=$row->training_title;?></td>
                <td><?=$row->immediate_rater;?></td>
                <td><?=$row->name;?></td>
            </tr>
            <?php     
          }
          ?>      
      </tbody>        
   </table>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题