0

当有多个姓氏相同时,搜索一个名字会返回一个结果。它可能是 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  
4

2 回答 2

2

returnfor循环中。

于 2013-01-05T22:04:50.600 回答
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;
于 2013-01-05T22:09:06.487 回答