Jump to content

Recommended Posts

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

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.