PageRenderTime 75ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/api.php

https://gitlab.com/swimly/api
PHP | 76 lines | 74 code | 1 blank | 1 comment | 4 complexity | 2f55703244805d3585cea47d908f230d MD5 | raw file
  1. <?php
  2. // 指定允许其他域名访问
  3. header('Access-Control-Allow-Origin: *');
  4. header('Access-Control-Allow-Credentials: true');
  5. include_once("conn.php");
  6. $type=isset($_GET["type"])?$_GET["type"]:null; /*get是获取、update是修改、delete是删除、add是添加*/
  7. $id=isset($_GET["id"])?$_GET["id"]:null;
  8. switch ($type){
  9. case "getProjects":
  10. getLists($conn,'projects',8,'desc','id,title,thumb,time,type');/*('数据库连接','表名称','每页条数','排序','查询字段')*/
  11. break;
  12. case "getProjectInfo":
  13. getOne($conn,'projects',$id,'*');/*('数据库连接','表名','id','查询字段')*/
  14. break;
  15. case "getArticles":
  16. getLists($conn,'article',8,'desc','id,title,thumb,time,type');
  17. break;
  18. case "getarticleInfo":
  19. getOne($conn,'article',$id,'*');
  20. break;
  21. case "insertProjects":
  22. addOne($conn,"projects");
  23. break;
  24. default:
  25. echo("没有参数");
  26. }
  27. function getLists($conn,$table,$size,$sort,$field){
  28. mysql_query("SET NAMES 'UTF8'",$conn);
  29. $page =isset($_GET['page'])?$_GET['page']:1; //获取查询页数,如果没有get到page,默认就是第一页!
  30. if(!preg_match('/^\d+$/',$page) || $page < 1) $page = 1; //如果输入的不是数字 或者小于1 默认第一页
  31. $table=$table; //获取要查询的数据表
  32. $pageSize =$size; //每页多少条
  33. $result_pag_num = mysql_query("SELECT COUNT(*) AS count FROM $table"); //获取数据表中总记录条数
  34. $row = mysql_fetch_array($result_pag_num);
  35. $count = $row['count']; //返回记录总条数
  36. $no_of_paginations = ceil($count / $pageSize); //计算出总页数
  37. if($page > $no_of_paginations) $page = $no_of_paginations; //如果请求页码大于总页数 默认最后一页
  38. $start = ($page - 1) * $pageSize; //sql查询起始位置
  39. $query_pag_data = "SELECT $field from $table order by id $sort LIMIT $start, $pageSize";
  40. $result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
  41. $arrList = array(); //初始化列表数组
  42. $arr=array();
  43. while($row = mysql_fetch_array($result_pag_data)){
  44. array_push($arrList,$row); //将每条信息push到列表数组中
  45. }
  46. $array = array(
  47. "count" => $count, //总条数
  48. "pageSize" => $pageSize, //每页条数
  49. "pageCount" => $no_of_paginations, //总页数
  50. "thisPage" => $page,//当前页码
  51. "list" => $arrList //列表
  52. );
  53. echo json_encode ($array);
  54. }
  55. function getOne($conn,$table,$id,$field){
  56. $sql="select $field from $table where id='$id'";
  57. $info=array();
  58. mysql_query("set names 'utf8'");
  59. mysql_query("SET CHARACTER SET UTF8");
  60. mysql_query("SET CHARACTER_SET_RESULTS=UTF8'");
  61. $result=@mysql_query($sql) or die();
  62. while($row=mysql_fetch_array($result)){
  63. $info=$row;
  64. }
  65. echo json_encode($info);
  66. }
  67. function addOne($conn,$table){
  68. $data=$_GET["data"];
  69. $arr=explode(",",$data);
  70. foreach($arr as $key=>$value){
  71. echo $key."=>".$value;
  72. }
  73. }
  74. close();
  75. ?>