php - Passing row variables to a function in Codeigniter

58

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 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" ><?=$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>
790

Answer

Solution:

Try like this,

<td><a href="trainingAttended/<?= $row->id ?>" class="page_speed_640716034"><?=$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);
}
64

Answer

Solution:

on your view where you click on a person's name

<a href="<?php echo base_url(): ?>traineeRater/trainingAttended/<? echo $row->id ?>">Name of person</a>

Then on you're Controller:

public function trainingAttended()
{
    if(!$this->session->userdata('loginuser'))
    {
        redirect(base_url());
    }
    if($this->session->userdata('level') != 'Rater')
    {
        $this->session->sess_destroy();
        redirect(base_url());
    }

    $id = $this->uri->segment('3');
    $data['attended'] = $this->rater_model->getAttended($id);

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

"" Hope it works.

People are also looking for solutions to the problem: php - Symfony 3.0 nested entities not saving

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.