要求仿照该代码进行编写<?php $code=0; $data=[]; $msg=["获取面板数据失败","获取面板成功"]; include("conn.php"); include("functions.php"); // $sql="select * from panels p left join users u on p.user_id=u.user_id order by p.panel_is_top desc,p.updated_time desc"; $sql="select p.panel_id,p.panel_title,p.panel_content,p.panel_likeit_nums,p.panel_is_top,p.panel_is_essence,p.panel_date,u.user_name from panels as p left join users as u on p.user_id=u.user_id order by p.panel_is_top desc,p.updated_time desc"; $rs=mysqli_query($conn,$sql); $i=0; //拼装data部分的对象数据 while($row=mysqli_fetch_array($rs)){ //读取记录行 $data[$i]["id"]=$row["panel_id"]; $data[$i]["user"]=$row["user_name"]; $data[$i]["likeitNums"]=$row["panel_likeit_nums"]; $data[$i]["topPanel"]=$row["panel_is_top"]; $data[$i]["essencePanels"]=$row["panel_is_essence"]; $data[$i]["date"]=$row["panel_date"]; $data[$i]["content"]=$row["panel_content"]; $data[$i]["title"]=$row["panel_title"]; //get comments $panelId=$row["panel_id"]; $sql="select c.panel_id,c.comment_id,c.comment_content,u.user_name from comments c left join users as u on c.user_id=u.user_id where c.panel_id=$panelId order by c.updated_time desc"; $rs1=mysqli_query($conn,$sql); $j=0; $comments=[]; while($row1=mysqli_fetch_array($rs1)){ $comments[$j]["id"]=$row1["comment_id"]; $comments[$j]["user"]=$row1["user_name"]; $comments[$j]["content"]=$row1["comment_content"]; $j++; } $data[$i]["comments"]=$comments; $rs1=null; $row1=null; $i++; } if(count($data)>0) $code=1; header("content-type:application/json"); echo getApiResult($code,$data,$msg); ?>
时间: 2024-01-31 09:02:47 浏览: 129
好的,根据您提供的代码,以下是经过修改后的 PHP 代码,可以将面板数据以 API 的形式返回给前端:
```php
<?php
$code = 0;
$data = [];
$msg = ["获取面板数据失败", "获取面板成功"];
include("conn.php");
include("functions.php");
$sql = "select p.panel_id,p.panel_title,p.panel_content,p.panel_likeit_nums,p.panel_is_top,p.panel_is_essence,p.panel_date,u.user_name from panels as p left join users as u on p.user_id=u.user_id order by p.panel_is_top desc,p.updated_time desc";
$rs = mysqli_query($conn, $sql);
$i = 0;
while($row = mysqli_fetch_array($rs)) {
$panelId = $row["panel_id"];
$panel = [
"id" => $row["panel_id"],
"user" => $row["user_name"],
"likeitNums" => $row["panel_likeit_nums"],
"topPanel" => $row["panel_is_top"],
"essencePanels" => $row["panel_is_essence"],
"date" => $row["panel_date"],
"content" => $row["panel_content"],
"title" => $row["panel_title"],
"comments" => []
];
$sql = "select c.panel_id,c.comment_id,c.comment_content,u.user_name from comments c left join users as u on c.user_id=u.user_id where c.panel_id=$panelId order by c.updated_time desc";
$rs1 = mysqli_query($conn, $sql);
$j = 0;
while($row1 = mysqli_fetch_array($rs1)) {
$comment = [
"id" => $row1["comment_id"],
"user" => $row1["user_name"],
"content" => $row1["comment_content"]
];
array_push($panel["comments"], $comment);
$j++;
}
array_push($data, $panel);
$i++;
}
if(count($data) > 0) {
$code = 1;
}
header("content-type:application/json");
echo getApiResult($code, $data, $msg);
?>
```
这段代码中,我们首先执行了 SQL 查询,获取到了面板和评论的数据,然后遍历面板数据,通过拼装一个面板对象,并在对象中包含该面板的评论数据,最终将面板对象添加到 $data 数组中。最后,根据 $data 数组的长度,判断是否成功获取到了面板数据,返回对应的 $code 和 $msg。在返回数据之前,使用 header() 函数设置响应头,告诉浏览器返回的是 JSON 格式的数据。
阅读全文