简体   繁体   中英

How to display by id? codeigniter

I have three tables:

EVENTS

evid - pk

evname

evdate

USERS

eventid - fk

userid - pk

username

TIME_SIGN

userid - fk

timeid - pk

timeDT


I want to get the records of the users who have the particular eventid

So in my previous view there is a button to be clicked to view the list of registered users of that particular event.

Controller File: events.php (index)

function index()
{
    $data['events'] = $this->model_events->getusersbyid();
    $this->load->view('viewEvents', $data);
}

View File: viewEvents.php

<?php foreach ($events as $event): ?>
              <div class="col-sm-6 col-md-4" style="width:200px; height:200px;">
                <div class="thumbnail">
                  <img src="<?php echo base_url(); ?>assets/images/eventi.png" alt="image-only">
                  <div class="caption">
                    <h3><?=$event->eventID;?></h3>
                    <h3><?=$event->eventName;?></h3>
                    <h4><?=$event->eventDate;?></h4>
                    <a href="<?php echo base_url()."index.php/events/showRegList/".$event->eventID; ?>" class="btn btn-primary" role="button">View Users</a>
                  </div>
                </div>
              </div>
      <?php endforeach; ?>

Now this part:

<a href="<?php echo base_url()."index.php/events/showRegList/".$event->eventID; ?>" class="btn btn-primary" role="button">View Users</a>

I'm trying to get the ID of the event. Currently, I have only 2 events and 3 users. In Event1 there is User1 and User2 and in Event2 there is only User3. But what happens in the display is it displays all the users. Both, in Event1 and Event2 views.

This is the model for my view file in displaying the records. What am I missing or what could be wrong with my code?

Model File: model_events.php (display_table)

function display_table()
{
      $this->db->select("events.eventID, users.id, users.event_id, CONCAT(lname, ', ', fname, ' ', mname) AS name, users.position,  time_sign.indexuser, time_sign.timein, time_sign.timeout");
      $this->db->from('events');
      $this->db->join('users', 'users.event_id = events.eventID', 'LEFT');
      $this->db->join('time_sign', 'time_sign.indexuser = users.id', 'LEFT');
      $query = $this->db->get();
      return $query->result();
}

This is the view of the list:

<?php foreach ($events as $event): ?>
           <tr>
               <td class="hidden"><?=$event->id;?></td>
               <td><?=$event->name;?></td>
               <td><?=$event->position;?></td>
               <td><?=$event->timein;?></td>
               <td><?=$event->timeout;?></td>
               <td width="100px">
                   <a href="<?php echo base_url()."index.php/tabledisplay/recTime/".$event->id; ?>">
                       <button type="submit" class="btn btn-success" name="time-in" style="margin:3px">Sign In</button>
                   </a>
                   <a href="<?php echo base_url()."index.php/tabledisplay/recOut/".$event->id; ?>">
                       <button type="submit" class="btn btn-danger" name="time-in" style="margin:3px">Sign Out</button>
                   </a>
               </td>
               <td>
                   <a href="<?php echo base_url()."index.php/events/show_recid/".$event->id; ?>">
                    <button type="button" class="btn btn-info" aria-label="Left Align">
                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                       </button></a>
                   <a href="<?php echo base_url()."index.php/events/delete/".$event->id; ?>">
                   <button type="button" class="btn btn-danger" aria-label="Left Align">
                    <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                       </button></a>
               </td>
           </tr>
          <?php endforeach; ?>

And this is the controller for displaying the list: EVENTS.php

(I put a comment below because that was my idea of getting the ID but doesn't work)

function showRegList()
{
    $data = array();
    $id = $this->uri->segment(3);
    //$evID = $this->model_events->getusersbyid();
    $data['events'] = $this->model_events->display_table();
    $this->load->view('table_view', $data);
}

THANK YOU.

If I'm understanding what you are asking correctly, I think you just need to add a where clause to your SQL in the model and pass the event ID to it from the controller script.

First up the Events.php controller script.

function showRegList()
 {
    $data = array();
    $id = $this->uri->segment(3);
    //$evID = $this->model_events->getusersbyid();
    $data['events'] = $this->model_events->display_table($id);
    $this->load->view('table_view', $data);
 }

Second, model_events.php. You will be returning everything because there's no where clause in your statement.

function display_table($event_id)
{
  $this->db->select("events.eventID, users.id, users.event_id, CONCAT(lname, ', ', fname, ' ', mname) AS name, users.position,  time_sign.indexuser, time_sign.timein, time_sign.timeout");
  $this->db->from('events');
  $this->db->join('users', 'users.event_id = events.eventID', 'LEFT');
  $this->db->join('time_sign', 'time_sign.indexuser = users.id', 'LEFT');
  $this->db->where('events.evid',$event_id);
  $query = $this->db->get();
  return $query->result();
}

Hopefully that should get you on the right track.

Try This code Events Controller

function showRegList()
{
    $data = array();
    $id = $this->uri->segment(3);
    $data['events'] = $this->model_events->display_table($id);
    $this->load->view('table_view', $data);
}

Model File: model_events.php (display_table)

    function display_table($id)
    {
          $this->db->select("events.eventID, users.id, users.event_id, CONCAT(lname, ', ', fname, ' ', mname) AS name, users.position,  time_sign.indexuser, time_sign.timein, time_sign.timeout");
          $this->db->from('events');
          $this->db->join('users', 'users.event_id = events.eventID', 'LEFT');
          $this->db->join('time_sign', 'time_sign.indexuser = users.id', 'LEFT');
          $condition=array("events.eventID"=>$id)
          $this->db->where($condition);
          $query = $this->db->get();
          return $query->result();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM