class Ajax1 extends Controller {
function Ajax1 ()
{
parent::Controller();
$this->load->model('ajax_model');
$this->load->model('ajax_model1');
$this->load->library('flexigrid_lib');
}
function index1()
{
//List of all fields that can be sortable. This is Optional.
//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
$valid_fields = array('id','iso','name','printable_name','iso3','numcode');
$this->flexigrid_lib->validate_post('id','asc',$valid_fields);
$records = $this->ajax_model->get_countries();
$this->output->set_header($this->config->item('json_header'));
/*
* Json build WITH json_encode. If you do not have this function please read
* http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
*/
foreach ($records['records']->result() as $row)
{
$record_items[] = array($row->id,
$row->id,
$row->iso,
$row->name,
'<span style=\'color:#ff4400\'>'.addslashes($row->printable_name).'</span>',
$row->iso3,
$row->numcode,
'<a href=\'#\'><img border=\'0\' src=\''.$this->config->item('base_url').'public/images/close.png\'></a> '
);
}
//Print please
$this->output->set_output($this->flexigrid_lib->json_build($records['record_count'],$record_items));
}
//Delete Country
function deletec()
{
$countries_ids_post_array = split(",",$this->input->post('items'));
foreach($countries_ids_post_array as $index => $country_id)
if (is_numeric($country_id) && $country_id > 1)
$this->ajax_model->delete_country($country_id);
$error = "Selected countries (id's: ".$this->input->post('items').") deleted with success";
$this->output->set_header($this->config->item('ajax_header'));
$this->output->set_output($error);
}
function index2()
{
//List of all fields that can be sortable. This is Optional.
//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
$valid_fields = array('id','name','last_name');
$this->flexigrid_lib->validate_post('id','asc',$valid_fields);
$records = $this->ajax_model1->get_domains();
$this->output->set_header($this->config->item('json_header'));
/*
* Json build WITH json_encode. If you do not have this function please read
* http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
*/
foreach ($records['records']->result() as $row)
{
$record_items[] = array($row->id,
$row->id,
$row->name,
$row->last_name
);
}
//Print please
$this->output->set_output($this->flexigrid_lib->json_build($records['record_count'],$record_items));
}
//Delete Country
function deletecDomain()
{
$domains_ids_post_array = split(",",$this->input->post('items'));
foreach($domains_ids_post_array as $index => $domain_id)
if (is_numeric($domain_id) && $domain_id > 1)
$this->ajax_model1->delete_domains($domain_id);
$error = "Selected domains (id's: ".$this->input->post('items').") deleted with success";
$this->output->set_header($this->config->item('ajax_header'));
$this->output->set_output($error);
}
}
?>
Model ajax_model.php/ajax_model1.php Code (PHP)
class Ajax_model extends Model
{
/**
* Instanciar o CI
*/
public function Ajax_model()
{
parent::Model();
$this->CI =& get_instance();
}
public function get_countries()
{
//Select table name
$table_name = "country";
//Build contents query
$this->db->select('id,iso,name,printable_name,iso3,numcode')->from($table_name);
$this->CI->flexigrid_lib->build_query();
//Get contents
$return['records'] = $this->db->get();
//Build count query
$this->db->select('count(id) as record_count')->from($table_name);
$this->CI->flexigrid_lib->build_query(FALSE);
$record_count = $this->db->get();
$row = $record_count->row();
//Get Record Count
$return['record_count'] = $row->record_count;
//Return all
return $return;
}
/**
* Remove country
* @param int country id
* @return boolean
*/
public function delete_country($country_id)
{
$delete_country = $this->db->query('DELETE FROM country WHERE id='.$country_id);
return TRUE;
}
Code (PHP)
class Ajax_model1 extends Model
{
/**
* Instanciar o CI
*/
public function Ajax_model1()
{
parent::Model();
$this->CI =& get_instance();
}
public function get_domains()
{
//Select table name
$table_name = "domains";
//Build contents query
$this->db->select('id,name,last_name')->from($table_name);
$this->CI->flexigrid_lib->build_query();
//Get contents
$return['records'] = $this->db->get();
//Build count query
$this->db->select('count(id) as record_count')->from($table_name);
$this->CI->flexigrid_lib->build_query(FALSE);
$record_count = $this->db->get();
$row = $record_count->row();
//Get Record Count
$return['record_count'] = $row->record_count;
//Return all
return $return;
}
/**
* Remove country
* @param int country id
* @return boolean
*/
public function delete_domains($domain_id)
{
$delete_domain = $this->db->query('delete FROM country WHERE id='.$domain_id);
return TRUE;
}
}
?>