doutuo6048 2017-08-02 06:23
浏览 36
已采纳

如何按ID显示? 笨

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.

  • 写回答

2条回答 默认 最新

  • dsf8897 2017-08-02 07:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统