jQuery(document).ready(function(){
jQuery("#save_btn").click(function(){
kabina=jQuery("#d_id").val();
var city = $('.city option:selected').map(function(idx, elem) {
return $(elem).html();
}).get();//alert(city);
//city=jQuery("#ct_title").val();
$.ajax({
type:'POST',
url:'<?php echo $menulink;?>/checkunique',
data:{city:city,kabina:kabina},
success:function(data){
//alert(data);
if(data == 'True')
{
toastr.success("Building Created",'Attention',{"positionClass": "toast-bottom-full-width" });
$("#city_frm").submit();
}
else
{
toastr.error("Building Created",'Attention',{"positionClass": "toast-bottom-full-width" });
$("#city_frm").submit();
}
//alert(data);
}
})
})
})
以下是ajax,从中调用控制器check unique方法这是控制器
public function checkunique(){
$count=array();
if(isset($_POST['kabina'])){
//$ct_id = $_POST['ct_id'];
$kabina = $_POST['kabina'];
$city = $_POST['city'];//print_r($city);
foreach($city as $ct_title)
{//print_r($ct_title);
$ct_count=$this->mod_atyatbox_cities->city_count($ct_title,$kabina);//print_r($ct_count);die;;
$count[]=$ct_count;
}//print_r($count);die;
if(in_array(1,$count))
{
echo "False";
}
else if(in_array(0,$count))
{
echo "True";
}
}
以下是编写查询以检查重复条目的模型
function city_count($ct_title,$kabina_id)
{
$query="select ct_title from city where ct_title='$ct_title' and kabina_id=$kabina_id";
$result=$this->db->query($query)->num_rows();//echo $result;die;
return $result;
}
在这里,如果我打印查询,它会打印出来。但当我返回数据时,即使有1,它也会返回0。
检查来自数组的所有查询
foreach($city as $ct_title){
$ct_count=$this->mod_atyatbox_cities->city_count($ct_title,$kabina);
$count[]=$ct_count;
$queries[]=$this->db->last_query();
}
print_r($queries);die;