选择语句返回一个人而不是几个人的结果

时间:2013-01-05 22:03:40

标签: php codeigniter activerecord

当有多个具有相同姓氏的名称时,搜索名称会返回一个结果。它可能是foreach循环,但我认为它是activerecord声明。

模型

public function finduser($name) {

    if ($name == null || $name == '') {
            return false;
    }

    $this->db->select('e.emp_no, e.first_name, e.last_name, t.title, d.dept_no');
    $this->db->from('employees AS e, titles AS t, dept_emp AS d');
    $this->db->where('e.last_name', $name);
    $this->db->where('e.emp_no = t.emp_no');
    $this->db->where('e.emp_no = d.emp_no');

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

    $data = array();

    foreach ($result->result() as $row )
    {
    $entry = array();
    $entry['emp_no'] = $row->emp_no;        
    $entry['first_name'] = $row->first_name;
    $entry['last_name'] = $row->last_name;      
    $entry['title'] = $row->title;
    $entry['dept_no'] = $row->dept_no;

    $data[] = $entry;

    return $data;

}
}

视图

if (isset($user)) {
foreach ($user as $res) {

echo "employee id =" .$res['emp_no']; ?> <br> <?php
echo "first name = " .$res['first_name']; ?> <br> <?php
echo "last name = " .$res['last_name']; ?> <br> <?php
echo "title = " .$res['title']; ?> <br> <?php
echo "department no = " .$res['dept_no']; ?> <br><br> <?php  

2 个答案:

答案 0 :(得分:2)

您的return位于for循环内。

答案 1 :(得分:0)

将return语句放在foreach循环中

foreach ($result->result() as $row )
    {
    $entry = array();
    $entry['emp_no'] = $row->emp_no;        
    $entry['first_name'] = $row->first_name;
    $entry['last_name'] = $row->last_name;      
    $entry['title'] = $row->title;
    $entry['dept_no'] = $row->dept_no;

    $data[] = $entry;



}
 return $data;