Reputation: 388
I want to make a simple search engine that looks for data from database and show them up, but I can't figure why the queries are performed here is my code :
class Reference_model extends CI_Model{
function search($match){
$this->db->where('sReference', $match);
$this->db->like('nReference', $match);
$this->db->or_like('sSearch', $match);
$this->db->from('treference', $match);
$query = $this->db->get();
if($query->num_rows > 0){
$output = '<ul>';
foreach ($query->result() as $rows){
$output .= '<li>'.$rows->nReference.'</li>';
}
$output .='</ul>';
}else{
return "<p>No Results!</p>"; //No Results!
}
return $output;
}
}
class References extends CI_Controller {
function index()
{
$q = $this->input->post('q');
$this->load->model('Reference_model');
$results['result'] = $this->Reference_model->search($q);
$this->load->view('constants/header');
$this->load->view('references',$results);
$this->load->view('constants/footer');
}
}
when I perform the search from the search form I get the No Results!, can some one help me out, because I've tried with many times without, and I'm not a professional webdeveloper I'm just learning, thank all the contributors
Upvotes: 1
Views: 61
Reputation: 502
Change this
if($query->num_rows > 0){
to
if($query->num_rows() > 0){
The num_rows
should be a function.
If still doesnt work.
Try to dump the query being generated and post it here.
var_dump($this->db->last_query()); die('x'); if($query->num_rows > 0){
Upvotes: 1
Reputation: 1138
Model :
class Reference_model extends CI_Model
{
function search($match)
{
$this->db->select('*'); //this line
$this->db->where('sReference', $match);
$this->db->like('nReference', $match);
$this->db->or_like('sSearch', $match);
$this->db->from('treference'); //this line
$query = $this->db->get();
if($query->num_rows > 0){
$output = '<ul>';
foreach ($query->result() as $rows){
$output .= '<li>'.$rows->nReference.'</li>';
}
$output .='</ul>';
}else{
return "<p>No Results!</p>"; //No Results!
}
return $output;
}
}
Try to debug your query with echo /print_r OR echo $this->db->last_query();
Hope this will help you.
Upvotes: 1
Reputation: 64
$this->db->like('sReference', $match);
$this->db->like('nReference', $match);
$this->db->or_like('sSearch', $match);
and this part
$this->db->from('treference', $match);
change it to
$this->db->from('treference');
try this..
hope it helps..
Upvotes: 1