我在查詢結果中創建表時遇到問題。 我有這樣的代碼:需要幫助從查詢結果創建表
// Model
<?php
class Search_Employee extends CI_Model {
function getAll() {
$searchterm = $this->name = $_POST['empname'];
$this->db->like('first_name', $searchterm);
$this->db->or_like('last_name', $searchterm);
$q = $this->db->query("SELECT first_name, last_name, deparment.name FROM employee
INNER JOIN deparment ON employee.department_id = department.id
WHERE first_name LIKE '%" . $searchterm . "%' OR last_name LIKE '%" . $searchterm . "%'");
if ($q = $this->db->get('employee'))
{
if ($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
} else {
echo "No results found.";
return array();
}
} else {
echo sprintf("DB Query Failed: %s", $q);
return false;
}
}
}
// Controller
class Employees extends CI_Controller {
function search_employee()
{
$this->load->library('table');
$this->load->model('search_employee');
$data['main_content'] = 'display_employee';
$data['rows'] = $this->search_employee->getAll();
$this->load->view('includes/template', $data);
}
}
// View
<h1>Results</h1>
<?php
$this->table->set_heading('Name', 'Last Name', 'Department');
$tmp = array ('table_open' => '<table border="0" cellpadding="2" cellspacing="1">');
$this->table->set_template($tmp);
foreach ($rows as $r) :
$this->table->add_row($r); ?>
endforeach;
?>
我不斷收到一個
Fatal error: Call to a member function result() on a non-object in C:\xampp\htdocs\employees\application\views\display_employee.php on line 15
Line 15
是foreach()
循環。
非常感謝您的幫助。
是查詢返回結果?可能是發生錯誤,並且getAll函數返回false。 – Zoidberg 2011-04-10 16:10:11
我也想補充一點,你的查詢讓你打開SQL注入攻擊。 – Zoidberg 2011-04-10 16:10:46
是的,查詢返回結果。 – ebecker 2011-04-10 16:28:11