如何按ID显示?笨

时间:2017-08-02 06:23:59

标签: php mysql codeigniter

我有三张桌子:

活动

evid - pk

EVNAME

evdate

用户

eventid - fk

userid - pk

用户名

TIME_SIGN

userid - fk

timeid - pk

timeDT

我想获取具有特定情况的用户的记录

因此,在我之前的视图中,有一个按钮可以单击以查看该特定事件的注册用户列表。

控制器文件:events.php(索引)

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

查看文件: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; ?>

现在这部分:

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

我正在尝试获取活动的ID。目前,我只有2个活动和3个用户。在Event1中有User1和User2,在Event2中只有User3。但显示屏中发生的是显示所有用户。两者都在Event1和Event2视图中。

这是我的视图文件显示记录的模型。我错过了什么或我的代码有什么问题?

模型文件: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();
}

这是列表的视图:

<?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; ?>

这是显示列表的控制器:EVENTS.php

(我在下面发表评论,因为这是我获取ID但不起作用的想法)

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);
}

谢谢。

2 个答案:

答案 0 :(得分:3)

如果我正确理解你的问题,我认为你只需要在模型中为你的SQL添加一个where子句,并从控制器脚本中将事件ID传递给它。

首先启动Events.php控制器脚本。

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);
 }

第二,model_events.php。你将返回所有内容,因为你的陈述中没有where子句。

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();
}

希望这能让你走上正轨。

答案 1 :(得分:1)

尝试此代码事件控制器

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

模型文件: 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();
    }