开发者

how is convert and fix error in this code to CodeIgniter?

开发者 https://www.devze.com 2023-03-20 03:32 出处:网络
how is convert this code to CodeIgniter code: search_hotel: -> this is CI_Model return mysql_query(\"select * from hotel_submits where name LIKE \'\".$searchterm.\"\'\")

how is convert this code to CodeIgniter code:

search_hotel: -> this is CI_Model

return mysql_query("select * from hotel_submits where name LIKE '".$searchterm."'")

i try but have error:

$query = $this->db->order_by("id", "desc")->like('name', '$searchterm')->get('hotel_submits');
        return $query->row();

error:

A PHP Error was encountered
Severity: Warning
Message: mysql_fetch_assoc() expects parameter 1 to be resource, array given
Filename: admin/tour.php
Line Number: 15


A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: admin/tour.php
Line Number: 21

code:-> this is CI开发者_运维技巧_Controller

$searchterm = $this->input->post('search_hotel');
$result = $this->model_tour->search_hotel($searchterm);
while ($row = mysql_fetch_assoc($result)) { //this is line 15
//giving names to the fields
$data = array (
   'name' => $row->name,            
);
}
echo json_encode($data);  //this is line 21


Think there's various things here. In your model where you return $query->row() you are only going to return one recordset row. You should try $query->result() instead. I would also advise moving the code you have in your controller into the model as well. So you model would begin to look something like:

function search_hotel($searchterm)
{

$query = $this->db->order_by("id", "desc")->like('name', $searchterm)->get('hotel_submits');

$data = array();

foreach ($query->result() as $row)
{
   data[] = $row->name
}

return $data
}

So your controller simple becomes something like:

function your_controller() {
//Set your $search_term somehow

    echo json_encode($this->model('your_model_name')->search_hotel($search_term);
}

Hopefully that should start to point you on the right track. Full information on creating and using recordsets can be found within the CI docs - http://codeigniter.com/user_guide/database/index.html

0

精彩评论

暂无评论...
验证码 换一张
取 消