显示详细信息(详细信息按钮点击(AJAX) 显示 弹窗)
封装类代码:
1 <?php 2 class DBDA 3 { 4 public $host="localhost";//服务器地址 5 public $uid="root";//用户名 6 public $pwd="";//密码 7 8 public $conn;//连接对象 9 //操作数据库的方法 10 //$sql代表需要执行的SQL语句 11 //$type代表SQL语句的类型,1代表查询,0代表增删改 12 //$db代表要操作的数据库名称 13 //如果是查询,返回二维数组 14 //如果是其他语句,返回true或false 15 function __construct($db="mydb") 16 { 17 //造连接对象 18 $this->conn = new MySQLi($this->host,$this->uid,$this->pwd,$db); 19 } 20 public function Query($sql,$type=1) 21 { 22 //判断是否出错 23 !mysqli_connect_error() or die("连接失败!"); 24 //执行SQL语句 25 $result = $this->conn->query($sql); 26 27 //判断SQL语句类型 28 if($type==1) 29 { 30 //如果是查询语句,返回结果集的二维数组 31 return $result->fetch_all(); 32 } 33 else 34 { 35 //如果是其他语句,返回true或false 36 return $result; 37 } 38 } 39 //Ajax调用返回JSON 40 public function JsonQuery($sql,$type=1,$db="mydb") 41 { 42 //定义数据源 43 $dsn = "mysql:dbname={$db};host={$this->host}"; 44 //造pdo对象 45 $pdo = new PDO($dsn,"{$this->uid}","{$this->pwd}"); 46 //准备执行SQL语句 47 $st = $pdo->prepare($sql); 48 //执行预处理SQL语句 49 if($st->execute()) 50 { 51 if($type==1) 52 { 53 $sttr = $st->fetchAll(PDO::FETCH_ASSOC); 54 return json_encode($attr); 55 } 56 else 57 { 58 if($st) 59 { 60 return "OK"; 61 } 62 else 63 { 64 return "NO"; 65 } 66 } 67 } 68 else 69 { 70 echo "执行失败!"; 71 } 72 } 73 //Ajax调用返回字符串 74 public function StrQuery($sql,$type=1) 75 { 76 //判断连接是否成功 77 !mysqli_connect_error() or die("连接失败!"); 78 //执行SQL语句 79 $result = $this->conn->query($sql); 80 //判断SQL语句类型 81 if($type==1) 82 { 83 $attr = $result->fetch_all(); 84 $str = ""; 85 //如果是查询语句返回字符串 86 for($i=0;$i<count($attr);$i++) 87 { 88 for($j=0;$j<count($attr[$i]);$j++) 89 { 90 $str = $str.$attr[$i][$j]; 91 $str = $str."^"; 92 } 93 $str = substr($str,0,strlen($str)-1); 94 $str = $str."|"; 95 } 96 $str = substr($str,0,strlen($str)-1); 97 return $str; 98 } 99 else 100 { 101 //如果是其他语句,返回true或false 102 if($result) 103 { 104 return "OK"; 105 } 106 else 107 { 108 return "NO"; 109 } 110 } 111 } 112 function PdoQuery($sql,$type=1,$db="mydb") 113 { 114 //造数据源 115 $dns = "mysql:host={$this->host};dbname={$db}"; 116 //造pdo对象 117 $pdo = new PDO($dns,$this->uid,$this->pwd); 118 //准备一条SQL语句 119 $stm = $pdo->prepare($sql); 120 //执行预处理语句 121 $r = $stm->execute(); 122 if($r) 123 { 124 if($type==1) 125 { 126 return $stm->fetchAll(); 127 } 128 else 129 { 130 return "OK"; 131 } 132 } 133 else 134 { 135 return "NO"; 136 } 137 138 } 139 }
主页面代码:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 <link href="../tanchuang.css" rel="stylesheet" type="text/css" /> 7 </head> 8 <script src="../jquery-1.11.2.min.js"></script> 9 <script src="../tanchuang.js"></script> 10 11 <?php 12 include("../DBDA.php"); 13 $db = new DBDA(); 14 15 ?> 16 <body> 17 <table width="100%" border="1" cellpadding="0" cellspacing="0"> 18 <tr> 19 <td>汽车名称</td> 20 <td>汽车类型</td> 21 <td>上市时间</td> 22 <td>操作</td> 23 </tr> 24 <?php 25 $sql = "select * from car"; 26 27 $attr = $db->Query($sql); 28 29 foreach($attr as $v) 30 { 31 echo " 32 <tr> 33 <td>$v[1]</td> 34 <td>$v[2]</td> 35 <td>$v[3]</td> 36 <td><input type='button' value='查看详情' class='ck' code='{$v[0]}' /></td> 37 </tr> 38 "; 39 } 40 ?> 41 </table> 42 </body> 43 <script type="text/javascript"> 44 $(document).ready(function(e) { 45 $(".ck").click(function() { 46 //找主键值 47 var code = $(this).attr("code"); 48 49 //根据主键查询具体数据 50 $.ajax({ 51 url:"xqchuli.php", 52 data:{code:code}, 53 type:"POST", 54 dataType:"TEXT", 55 success: function(data){ 56 var lie = data.split("^"); 57 var html = "<div class='xq'>代号: "+lie[0]+"</div><div class='xq'>汽车名称: "+lie[1]+"</div><div class='xq'>汽车系列:"+lie[2]+"</div><div class='xq'>上市时间: "+lie[3]+"</div><div class='xq'>油耗: "+lie[4]+"</div><div class='xq'>功率: "+lie[5]+"</div><div class='xq'>排量: "+lie[6]+"</div><div class='xq'>价格: "+lie[7]+"</div>"; 58 //var button =""; 59 var win = new Window({ 60 61 width : 300, //宽度 62 height : 400, //高度 63 title : '汽车详情信息', //标题 64 content : html, //内容 65 isMask : true, //是否遮罩 66 //buttons : button, //按钮 67 isDrag:true, //是否移动 68 69 }); 70 } 71 }); 72 }); 73 }); 74 </script> 75 </html>
处理页面代码:
1 <?php 2 $code = $_POST["code"]; 3 include("../DBDA.php"); 4 $db = new DBDA(); 5 $sql = "select * from car where code = '{$code}'"; 6 $attr = $db->Query($sql); 7 echo implode("^",$attr[0]);
显示结果: