从codeigniter

时间:2015-05-18 10:46:43

标签: mysql sql codeigniter model html-table

我有2张桌子: 活动和注册。

事件字段:EventID和EventName

注册字段:MemberID和名称

我想拉出EventName和该人的姓名。如何使用模型执行此操作? 我的代码:
模型

function getAll()
{
    $query = $this->db->get('tblRegistration');
    if( $query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
    }
    $query->free_result();
    return $data;
}

控制器

public function getallevents() {
    $this->load->model('events_model');

    $data['records'] = $this->events_model->getAll();

    $this->load->view('view-events', $data);
}

查看

              foreach ($records as $row):
                        echo "<tr>";
                        echo "<td> $row->Name</td>";
                        // echo "<td> $row->intLocationID</td>";
                        echo "</tr>";
                    endforeach;

修改

好的,我已经从表中添加了我想要的数据。

模型

   function getAll()
{
    // get all the records from the schools table

    $this->db->select('*');
    $this->db->from('tblRegistration as reg');
    $this->db->join('tblMeetRace as met', 'reg.intMeetRaceID = met.intEventID');
    $query = $this->db->get();

    // if the number of rows returned is more than 0
    if( $query->num_rows() > 0) {
        // loop through each record (row) and place that
        // record into an array called $data
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
    }
    $query->free_result();
    // return the array to the controller
    return $data;
}

查看

     foreach ($records as $row):
                        echo "<tr>";
                        echo "<td> $row->intMeetRaceID/td>";
                        echo "<td> $row->intEventID</td>";
                        echo "</tr>";
                    endforeach;

但我收到错误:

  

遇到PHP错误   严重性:警告
  消息:为foreach()提供的参数无效   文件名:views / view-events.php
  行号:89

1 个答案:

答案 0 :(得分:0)

首先,您需要在事件表中添加另一列以存储MemberId,例如memberid。 完成后,当成员注册某个事件时,您可以将他的ID存储在事件表中并将其关联起来。

然后您可以使用Joins来获取数据

$this->db->select('*');
$this->db->from('registration as reg');
$this->db->join('events as evt', 'reg.id = evt.memberid');

$query = $this->db->get();