1.循环输出查到的数据的某一列
后台代码
<?php
public function index(){
$friend = D("friend");
$arr["id"] = $_SESSION["id"];
$re = $friend->limit(10)->where($arr)->select();//limit代表查几条
$this->assign('sname',$re);
}
前台代码
<volist name="sname" id="vo">
{$vo.name}
</volist>
2.ajaxJSON返回数据处理方法
后台代码
public function chaxun(){
$user = D("user");//创建表
$id = $_POST["id"];//接收传过来的post数据
$arr['user_id'] = $id;//将数据存到数组里
$re = $user->where($arr)->select();//根据条件查数据
if (empty($re))//判断有没有数据
{
$data = 1;
$this->ajaxReturn($data,'json');//没有数据就返回1
}
else
{
$this->ajaxReturn($re,'json');//数据存在返回JSON格式
}
}
前台代码
<script type="text/javascript" >
$("#tianjia").click(function () {
var id = $("#zhanghao1").val();
$.ajax({
url:"{:U('Index/chaxun')}",
data:{id:id},
type:"POST",
dataType:"json",
success:function (data) {
if (data === 1)
{
alert("数据不存在");
}
else
{
var str = "";
for(var sj in data)//循环传过来的JSON数据
{
//alert(data[sj].uname);
str = str+"<div>"+data[sj].uname+"<button id='uid' type='button' code='"+data[sj].user_id+"'>添加</button></div>";
}
}
}
})
});
</script>