CodeIgniter Forums
how is convert this code to CodeIgniter code? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: how is convert this code to CodeIgniter code? (/showthread.php?tid=43520)



how is convert this code to CodeIgniter code? - El Forum - 07-14-2011

[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



how is convert this code to CodeIgniter code? - El Forum - 07-14-2011

[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.


how is convert this code to CodeIgniter code? - El Forum - 07-14-2011

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


how is convert this code to CodeIgniter code? - El Forum - 07-14-2011

[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');



how is convert this code to CodeIgniter code? - El Forum - 07-14-2011

[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;
        }



how is convert this code to CodeIgniter code? - El Forum - 07-14-2011

[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.


how is convert this code to CodeIgniter code? - El Forum - 07-14-2011

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


how is convert this code to CodeIgniter code? - El Forum - 07-15-2011

[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();



how is convert this code to CodeIgniter code? - El Forum - 07-15-2011

[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-guide/database/active_record.html
First section on the get() has a better example