Welcome Guest, Not a member yet? Register   Sign In
how is convert this code to CodeIgniter code?
#1

[eluser]SaSa[/eluser]
how is convert this code to CodeIgniter code:
search_hotel: -> this is CI_Model
Code:
return mysql_query("select * from hotel_submits where name LIKE '".$searchterm."'")
i try but have error:
Code:
$query = $this->db->order_by("id", "desc")->like('name', '$searchterm')->get('hotel_submits');
        return $query->row();
error:
Quote: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
Code:
$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
#2

[eluser]DirkZz[/eluser]
No offence, but you constantly ask things that can be found with basic PHP knowledge or by just reading the User Guide.

You can't learn anything if you let other people fix all of your problems.
#3

[eluser]SaSa[/eluser]
I tried and more search but I not could. I am involved it in a few days.
please help me...
#4

[eluser]Eric Barnes[/eluser]
You just need to make sure your query actually generates results before you loop it. Also:

Code:
$query = $this->db->order_by("id", "desc")->like('name', '$searchterm')->get('hotel_submits');
should be
Code:
$query = $this->db->order_by("id", "desc")->like('name', $searchterm)->get('hotel_submits');
#5

[eluser]SaSa[/eluser]
Thank you,
what is this error?

Code:
A PHP Error was encountered

Severity: Warning

Message: mysql_fetch_array() expects parameter 1 to be resource, object given

Filename: admin/model_tour.php

Line Number: 19

line 19:
Code:
while($row = mysql_fetch_array($query)) //this is line 19
        {
           $data[] = $row->name;
        }
#6

[eluser]Eric Barnes[/eluser]
Sorry just looked at your code again. You will need to read the user guide -> database section. Looks like you are trying to mix straight php sql functions with CodeIgniters.
#7

[eluser]SaSa[/eluser]
I read about it but did not. please help, I'm haste
#8

[eluser]Zaher Ghaibeh[/eluser]
can you put your full code so we can spot the error ??
since if you want to get an array out of your code you can use
Code:
$query->result_array();
not
Code:
return $query->row();
which will result only one row, and you didnt limit your sql statement with limit().
but if you want to get all the results you have to use
Code:
$query->result();
#9

[eluser]Mat-Moo[/eluser]
Your mixing "Active records" with basic sql, you need something more like
Code:
foreach ($result->result() as $row)
{
    $data[] = array('name' => $row->name)
}
Something like that anyway Smile

http://ellislab.com/codeigniter/user-gui...ecord.html
First section on the get() has a better example




Theme © iAndrew 2016 - Forum software by © MyBB