ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

how is convert this code to CodeIgniter code?

July 14, 2011 12:10pm

Subscribe [3]
  • #1 / Jul 14, 2011 12:10pm

    SaSa

    118 posts

    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
  • #2 / Jul 14, 2011 12:32pm

    DirkZz

    15 posts

    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 / Jul 14, 2011 12:35pm

    SaSa

    118 posts

    I tried and more search but I not could. I am involved it in a few days.
    please help me…

  • #4 / Jul 14, 2011 1:01pm

    Eric Barnes

    487 posts

    You just need to make sure your query actually generates results before you loop it. Also:

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

    should be

    $query = $this->db->order_by("id", "desc")->like('name', $searchterm)->get('hotel_submits');
  • #5 / Jul 14, 2011 1:11pm

    SaSa

    118 posts

    Thank you,
    what is this error?

    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:

    while($row = mysql_fetch_array($query)) //this is line 19
            {
               $data[] = $row->name;
            }
  • #6 / Jul 14, 2011 1:13pm

    Eric Barnes

    487 posts

    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 / Jul 14, 2011 1:23pm

    SaSa

    118 posts

    I read about it but did not. please help, I’m haste

  • #8 / Jul 15, 2011 5:26am

    Zaher Ghaibeh

    48 posts

    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

    $query->result_array();

    not

    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

    $query->result();
  • #9 / Jul 15, 2011 10:33am

    Mat-Moo

    350 posts

    Your mixing “Active records” with basic sql, you need something more like

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

    Something like that anyway 😊

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

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases