Yohanne Posted October 8, 2014 Share Posted October 8, 2014 Hi Coders, any body can convert the following code into codeigniter. if($_GET['type'] == 'country'){ $result = mysql_query("SELECT name FROM country where name LIKE '".strtoupper($_GET['name_startsWith'])."%'"); $data = array(); while ($row = mysql_fetch_array($result)) { array_push($data, $row['name']); } echo json_encode($data); } Thanks Link to comment https://forums.phpfreaks.com/topic/291499-convert-to-ci/ Share on other sites More sharing options...
Yohanne Posted October 8, 2014 Author Share Posted October 8, 2014 I try load code above with this format in ajax but not work, any body can provide any possible answer. controller/ - public_base public function ajax() { if($_GET['type'] == 'country') { $result = $this->db->query("SELECT stock_desc FROM stocks where stock_desc LIKE '".strtoupper($_GET['name_startsWith'])."%'"); $data = array(); while ($row = mysql_fetch_array($result)) { array_push($data, $row['stock_desc']); } echo json_encode($data); } } $('#country_name').autocomplete({ source: function( request, response ) { $.ajax({ url : '<?php echo base_url();?>index.php/public_base/ajax', dataType: "json", data: { name_startsWith: request.term, type: 'country' }, success: function( data ) { response( $.map( data, function( item ) { return { label: item, value: item } })); } }); }, autoFocus: true, minLength: 0 }); Link to comment https://forums.phpfreaks.com/topic/291499-convert-to-ci/#findComment-1492993 Share on other sites More sharing options...
Frank_b Posted October 8, 2014 Share Posted October 8, 2014 In Codeigniter you should run your queries in the Model and not in the Controller. More or less you should make one one Model per database table. <?php // The Model class StockModel extends CI_Model { function __construct() { // Call the Model constructor parent::__construct(); } function getAll() { $query = $this->db->get('stocks'); /* foreach ($query->result() as $row) { echo $row->stock_desc; } */ return $query->result(); } function findStockDescriptions($searchtext) { $query = $this->db->query("SELECT stock_desc FROM stocks where stock_desc LIKE '".strtoupper($searchtext)."%'"); /* foreach ($query->result() as $row) { echo $row->stock_desc; } */ return $query->result(); } // Add more query function that belongs to the stocks table } // The Controller class Stock extends CI_Controller { public function ajax($searchtext) { $this->load->model('StockModel'); $stocks = $this->StockModel->findStockDescriptions($searchtext); $data = array(); foreach($stocks as $stock) { $data[] = $stock['stock_desc']; } echo json_encode($data); exit; // In this case we don't need to load a view, instead we echo the JSON directly out of the controller. } } ?> Link to comment https://forums.phpfreaks.com/topic/291499-convert-to-ci/#findComment-1493016 Share on other sites More sharing options...
Frank_b Posted October 8, 2014 Share Posted October 8, 2014 Now you are able to test your code simply to open this path in your browser: http://yourdomain.com/stock/ajax/search Replace yourdomain for your own domain name and search for an applicable searchtext. You should than see a JSON string in your browser eg ["stock1","stock2","stock3"] Link to comment https://forums.phpfreaks.com/topic/291499-convert-to-ci/#findComment-1493018 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.