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条回答 默认 最新

  • dtny30176 2015-12-10 08:55
    关注

    Try like this,

    <td><a href="trainingAttended/<?= $row->id ?>" style=" text-decoration: none; color: #000000"><?=$row->name;?></a></td>
    

    In href make sure you add controller/method/parameters

    In controller

    public function trainingAttended($id)
                                     ^// capture id here
    {
    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($id);
                                                         ^// send here
    
    $this->load->view('rater/attended', $data);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效