[eluser]Unknown[/eluser]
hello here we leave this example of how to use JSON with CodeIgniter
controller home.php
Code:
class Home extends Controller {
function Home()
{
parent::Controller();
$this->load->model('function_ciudad');
$this->load->database();
}
function index(){
$data['data']=$this->function_ciudad->show_city();
$this->load->view('index_home/index',$data);
}
}
controller universidad.php
Code:
class Universidad extends Controller {
function Universidad()
{
parent::Controller();
$this->load->model('function_universidad');
$this->load->database();
}
function find($id){
$data['data']=$this->function_universidad->show_university('','','where id_ciudad='.$id);
$this->load->view('index_university/find',$data);
}
}
Model function_ciudad.php
Code:
class Function_ciudad extends Model {
function Function_ciudad(){
parent::Model();
}
function show_city($limit='',$id = '',$where = ''){
if($id !=''){
$where="where id='".$id."' order by fecha_crea desc";
}
if($limit !=''){
$limit="limit ".$limit;
}
$rs_city=$this->db->query("select * from ciudades ".$where." ".$limit." ");
if ($rs_city->num_rows() > 0) {
$array_result['confirm']=true;
foreach ($rs_city->result() as $row_city=>$row_city_value){
foreach ($row_city_value as $row_city_index_=>$row_city_value_){
$array_result_city[$row_city_index_]=$row_city_value_;
}
$array_data_city[]=$array_result_city;
}
$array_result['data_city']=$array_data_city;
return $array_result;
}else{
$array_result['confirm']=false;
$array_result['msn_result']="En estos momentos no hay noticias disponibles.";
return $array_result;
}
}
}
Model function_universidad.php
Code:
class Function_universidad extends Model {
function Function_universidad(){
parent::Model();
}
function show_university($limit='',$id = '',$where = ''){
if($id !=''){
$where="where id=".$id."";
}
if($limit !=''){
$limit="limit ".$limit;
}
$rs_university=$this->db->query("select * from universidades ".$where." ".$limit." ");
if ($rs_university->num_rows() > 0) {
$array_result['confirm']=true;
foreach ($rs_university->result() as $row_university=>$row_university_value){
foreach ($row_university_value as $row_university_index_=>$row_university_value_){
$array_result_university[$row_university_index_]=$row_university_value_;
}
$array_data_university[]=$array_result_university;
}
$array_result['data_university']=$array_data_university;
return $array_result;
}else{
$array_result['confirm']=false;
$array_result['msn_result']="En estos momentos no hay noticias disponibles.";
return $array_result;
}
}
}
The file Javascript
Code:
function find_university(id,div){
html='';
$('loading').style.display='block';
new Ajax.Request(base_url+'universidad/find/'+id,{method:'post',onSuccess:
function(x_data){
obj= eval("("+x_data.responseText+")");
if (obj.confirmar){
html="<select name='sel_university' id='sel_university'>";
for(i=0;i<=obj.data.length-1;i++){
html+="<option value='"+obj.data[i]['id']+"'>"+obj.data[i]['nombre']+"</option>";
}
html+="</select>";
$(div)[removed]=html;
}else{
alert(obj.msg_error);
$('loading').style.display='none';
}
}
});
}
The view
Code:
<div id="sel_uni"></div>
<?
if($data['confirm']){
?>
<select name="sel_city" id="sel_city" onchange="find_university(this.value,'sel_resul')">
<option value="" selected="selected">Seleccione . . .</option>
<?
foreach($data['data_city'] as $index_data=>$values_data){
?>
<option value="<?=$values_data['id']?>"><?=$values_data['nombre']?></option>
<?
}
?>
</select>
<?
}else{ ?>
<div id="msn_error">
<?=$data['msn_result']?>
</div>
<? }
?>
</div>
<div id="sel_resul"></div>
I hope they can serve