CodeIgniter Forums
How would I "convert" this mysql_query to CI's active record? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How would I "convert" this mysql_query to CI's active record? (/showthread.php?tid=39598)



How would I "convert" this mysql_query to CI's active record? - El Forum - 03-15-2011

[eluser]dallen33[/eluser]
I'm generating some JSON for an autocomplete form. I found this code online which works beautifully, but whenever I try to re-factor it to CodeIgniter's active record style, it never works. Any ideas how I could do this?
Code:
$return_arr = array();
        $query = mysql_query("SELECT * FROM clients");
    
    while ($row = mysql_fetch_array($query, MYSQL_ASSOC)):
        $row_array['value'] = $row['name'];
            array_push($return_arr, $row_array);
    endwhile;

For example, this doesn't work:
Code:
$query = $this->db->get('clients');



How would I "convert" this mysql_query to CI's active record? - El Forum - 03-15-2011

[eluser]Isern Palaus[/eluser]
[quote author="dallen33" date="1300234871"]I'm generating some JSON for an autocomplete form. I found this code online which works beautifully, but whenever I try to re-factor it to CodeIgniter's active record style, it never works. Any ideas how I could do this?
Code:
$return_arr = array();
        $query = mysql_query("SELECT * FROM clients");
    
    while ($row = mysql_fetch_array($query, MYSQL_ASSOC)):
        $row_array['value'] = $row['name'];
            array_push($return_arr, $row_array);
    endwhile;

For example, this doesn't work:
Code:
$query = $this->db->get('clients');
[/quote]

You've to generate the results, so first:

Code:
$query = $this->db->get("clients")->result_array();

I usually do ->result() and get and object but as you need an array, it's more apropiate. Then:

Code:
$result = array(); // I first create and array so I'll never get a not defined if there is no result.

foreach($query as $row) // I dont use {} because u only need one line
    $result[] = $row;

This should work ;-)


How would I "convert" this mysql_query to CI's active record? - El Forum - 03-15-2011

[eluser]dallen33[/eluser]
Thanks Isern. Here's what I ended up doing:
Code:
function get_client()
    {
        $result = array();
        $query = $this->db->select('name')->get('clients')->result_array();
    
        foreach($query as $row):
            array_push($result, $row['name']);
        endforeach;

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    
    echo json_encode($result);
    }

Just curious... is there a way I can grab my list of clients from the frontend using jQuery autocomplete without somehow letting people view the source and accessing the AJAX URL to view the list themselves?


How would I "convert" this mysql_query to CI's active record? - El Forum - 03-15-2011

[eluser]bubbafoley[/eluser]
this should work

Code:
function get_client()
{
    if( ! (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
    ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) )
    {
        exit('no direct access');
    }

    $result = array();
    $query = $this->db->select('name')->get('clients')->result_array();
    
    foreach($query as $row):
        array_push($result, $row['name']);
    endforeach;

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    
    echo json_encode($result);
}



How would I "convert" this mysql_query to CI's active record? - El Forum - 03-15-2011

[eluser]dallen33[/eluser]
Awesome Bubbafoley! Exactly what I needed. Thanks.


How would I "convert" this mysql_query to CI's active record? - El Forum - 03-15-2011

[eluser]Isern Palaus[/eluser]
You don't need to do this:

Code:
foreach($query as $row):
        array_push($result, $row['name']);
    endforeach;

You can do this:
Code:
foreach($query as $row)
        array_push($result, $row['name']);

And will do the same ;-)


How would I "convert" this mysql_query to CI's active record? - El Forum - 03-15-2011

[eluser]Isern Palaus[/eluser]
And taking the bubbafoley code I can do it with less code:

Code:
function get_client()
{
    if(false === $this->input->is_ajax_request())
        redirect(""); // redirect to the start of the page

    $result = array();

    $query = $this->db->select('name')->get('clients')->result_array();
    
    foreach($query as $row)
        $result[] = $row["name"];

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    
    echo json_encode($result);
}

I've changed the array push, take a look in PHP manual:
Quote:PHP manual says: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.