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(); 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(); For example, this doesn't work: Code: $query = $this->db->get('clients'); 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. 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() 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() 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): You can do this: Code: foreach($query as $row) 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() 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. |