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条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看