PHP CRUD JSON文件,而不是像mysql这样的数据库
我有点为难,我收到了一个创建CRUD应用程序的请求,以编辑数组中的JSON对象并上传它们。我做了一些研究,发现它并不真正符合我的要求 编辑:我也遇到了jtables,但它使用mysql,与之类似,但它也使用mysql。是否可以跳过数据库部分,直接写入JSON文件 因为这是一个非常小的JSON文件,所以拥有一个数据库似乎太过分了。我在JSON数组中有多个外部应用程序可以读取的对象PHP CRUD JSON文件,而不是像mysql这样的数据库,php,json,crud,Php,Json,Crud,我有点为难,我收到了一个创建CRUD应用程序的请求,以编辑数组中的JSON对象并上传它们。我做了一些研究,发现它并不真正符合我的要求 编辑:我也遇到了jtables,但它使用mysql,与之类似,但它也使用mysql。是否可以跳过数据库部分,直接写入JSON文件 因为这是一个非常小的JSON文件,所以拥有一个数据库似乎太过分了。我在JSON数组中有多个外部应用程序可以读取的对象 我有哪些可行的选择?理想情况下,应用程序需要将浏览器内容添加/编辑/删除到JSON文件中 目前,我能够在表格中相应地显
我有哪些可行的选择?理想情况下,应用程序需要将浏览器内容添加/编辑/删除到JSON文件中 目前,我能够在表格中相应地显示数据。我的代码如下所示: PHP:
<?php
$getfile = file_get_contents('test.json');
$jsonfile = json_decode($getfile);
?>
<table align="center">
<tr>
<th>Title</th>
<th>Background Image</th>
<th>Video URL (Link to Video)</th>
<th>Description of Video</th>
</tr>
<?php
foreach ($jsonfile->playlist as $obj) {
echo '<tr><td>' . $obj->title . '</td>';
echo '<td>' . $obj->title_bg . '</td>';
echo '<td>' . $obj->link . '</td>';
echo '<td>' . $obj->description . '</td></tr>';
}
?>
</table>
{
"playlist": [
{
"title": "Test title",
"title_bg": "link/to/image.png",
"link": "https://www.google.com",
"description": "This is a test JSON Object"
}
]
}
JSON文件将在播放列表中包含多个对象数组您可以通过以下方式执行此操作: test.json
{
"playlist": [
{
"title": "Test title1222212321321321",
"title_bg": "link\/to\/image.png",
"link": "https:\/\/www.google.com",
"description": "This is a test JSON Object"
}, {
"title": "sdfdasf",
"title_bg": "adsfdas",
"link": "fdasf",
"description": "dasfdasf"
}, {
"title": "This is a title ",
"title_bg": "This is a title bg",
"link": "This is a link2",
"description": "This is a description"
}
]
}
index.php
<?php
$getfile = file_get_contents('test.json');
$jsonfile = json_decode($getfile);
?>
<a href="http://localhost/test/add.php">Add</a>
<table align="center">
<tr>
<th>Title</th>
<th>Background Image</th>
<th>Video URL (Link to Video)</th>
<th>Description of Video</th>
<th></th>
</tr>
<tbody>
<?php foreach ($jsonfile->playlist as $index => $obj): ?>
<tr>
<td><?php echo $obj->title; ?></td>
<td><?php echo $obj->title_bg; ?></td>
<td><?php echo $obj->link; ?></td>
<td><?php echo $obj->description; ?></td>
<td>
<a href="http://localhost/test/edit.php?id=<?php echo $index; ?>">Edit</a>
<a href="http://localhost/test/delete.php?id=<?php echo $index; ?>">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
if (isset($_GET["id"])) {
$id = (int) $_GET["id"];
$getfile = file_get_contents('test.json');
$jsonfile = json_decode($getfile, true);
$jsonfile = $jsonfile["playlist"];
$jsonfile = $jsonfile[$id];
}
if (isset($_POST["id"])) {
$id = (int) $_POST["id"];
$getfile = file_get_contents('test.json');
$all = json_decode($getfile, true);
$jsonfile = $all["playlist"];
$jsonfile = $jsonfile[$id];
$post["title"] = isset($_POST["title"]) ? $_POST["title"] : "";
$post["title_bg"] = isset($_POST["title_bg"]) ? $_POST["title_bg"] : "";
$post["link"] = isset($_POST["link"]) ? $_POST["link"] : "";
$post["description"] = isset($_POST["description"]) ? $_POST["description"] : "";
if ($jsonfile) {
unset($all["playlist"][$id]);
$all["playlist"][$id] = $post;
$all["playlist"] = array_values($all["playlist"]);
file_put_contents("test.json", json_encode($all));
}
header("Location: http://localhost/test/index.php");
}
?>
<?php if (isset($_GET["id"])): ?>
<form action="http://localhost/test/edit.php" method="POST">
<input type="hidden" value="<?php echo $id ?>" name="id"/>
<input type="text" value="<?php echo $jsonfile["title"] ?>" name="title"/>
<input type="text" value="<?php echo $jsonfile["title_bg"] ?>" name="title_bg"/>
<input type="text" value="<?php echo $jsonfile["link"] ?>" name="link"/>
<input type="text" value="<?php echo $jsonfile["description"] ?>" name="description"/>
<input type="submit"/>
</form>
<?php endif; ?>
<?php
if (isset($_GET["id"])) {
$id = (int) $_GET["id"];
$all = file_get_contents('test.json');
$all = json_decode($all, true);
$jsonfile = $all["playlist"];
$jsonfile = $jsonfile[$id];
if ($jsonfile) {
unset($all["playlist"][$id]);
$all["playlist"] = array_values($all["playlist"]);
file_put_contents("test.json", json_encode($all));
}
header("Location: http://localhost/test/index.php");
}
<form action="http://localhost/test/add.php" method="POST">
<input type="text" name="title" placeholder="title"/>
<input type="text" name="title_bg" placeholder="title_bg"/>
<input type="text" name="link" placeholder="link"/>
<input type="text" name="description" placeholder="description"/>
<input type="submit" name="add"/>
</form>
<?php
if (isset($_POST["add"])) {
$file = file_get_contents('test.json');
$data = json_decode($file, true);
unset($_POST["add"]);
$data["playlist"] = array_values($data["playlist"]);
array_push($data["playlist"], $_POST);
file_put_contents("test.json", json_encode($data));
header("Location: http://localhost/test/index.php");
}
?>
标题
背景图像
视频URL(视频链接)
视频描述
edit.php
<?php
$getfile = file_get_contents('test.json');
$jsonfile = json_decode($getfile);
?>
<a href="http://localhost/test/add.php">Add</a>
<table align="center">
<tr>
<th>Title</th>
<th>Background Image</th>
<th>Video URL (Link to Video)</th>
<th>Description of Video</th>
<th></th>
</tr>
<tbody>
<?php foreach ($jsonfile->playlist as $index => $obj): ?>
<tr>
<td><?php echo $obj->title; ?></td>
<td><?php echo $obj->title_bg; ?></td>
<td><?php echo $obj->link; ?></td>
<td><?php echo $obj->description; ?></td>
<td>
<a href="http://localhost/test/edit.php?id=<?php echo $index; ?>">Edit</a>
<a href="http://localhost/test/delete.php?id=<?php echo $index; ?>">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
if (isset($_GET["id"])) {
$id = (int) $_GET["id"];
$getfile = file_get_contents('test.json');
$jsonfile = json_decode($getfile, true);
$jsonfile = $jsonfile["playlist"];
$jsonfile = $jsonfile[$id];
}
if (isset($_POST["id"])) {
$id = (int) $_POST["id"];
$getfile = file_get_contents('test.json');
$all = json_decode($getfile, true);
$jsonfile = $all["playlist"];
$jsonfile = $jsonfile[$id];
$post["title"] = isset($_POST["title"]) ? $_POST["title"] : "";
$post["title_bg"] = isset($_POST["title_bg"]) ? $_POST["title_bg"] : "";
$post["link"] = isset($_POST["link"]) ? $_POST["link"] : "";
$post["description"] = isset($_POST["description"]) ? $_POST["description"] : "";
if ($jsonfile) {
unset($all["playlist"][$id]);
$all["playlist"][$id] = $post;
$all["playlist"] = array_values($all["playlist"]);
file_put_contents("test.json", json_encode($all));
}
header("Location: http://localhost/test/index.php");
}
?>
<?php if (isset($_GET["id"])): ?>
<form action="http://localhost/test/edit.php" method="POST">
<input type="hidden" value="<?php echo $id ?>" name="id"/>
<input type="text" value="<?php echo $jsonfile["title"] ?>" name="title"/>
<input type="text" value="<?php echo $jsonfile["title_bg"] ?>" name="title_bg"/>
<input type="text" value="<?php echo $jsonfile["link"] ?>" name="link"/>
<input type="text" value="<?php echo $jsonfile["description"] ?>" name="description"/>
<input type="submit"/>
</form>
<?php endif; ?>
<?php
if (isset($_GET["id"])) {
$id = (int) $_GET["id"];
$all = file_get_contents('test.json');
$all = json_decode($all, true);
$jsonfile = $all["playlist"];
$jsonfile = $jsonfile[$id];
if ($jsonfile) {
unset($all["playlist"][$id]);
$all["playlist"] = array_values($all["playlist"]);
file_put_contents("test.json", json_encode($all));
}
header("Location: http://localhost/test/index.php");
}
<form action="http://localhost/test/add.php" method="POST">
<input type="text" name="title" placeholder="title"/>
<input type="text" name="title_bg" placeholder="title_bg"/>
<input type="text" name="link" placeholder="link"/>
<input type="text" name="description" placeholder="description"/>
<input type="submit" name="add"/>
</form>
<?php
if (isset($_POST["add"])) {
$file = file_get_contents('test.json');
$data = json_decode($file, true);
unset($_POST["add"]);
$data["playlist"] = array_values($data["playlist"]);
array_push($data["playlist"], $_POST);
file_put_contents("test.json", json_encode($data));
header("Location: http://localhost/test/index.php");
}
?>
此json无效!!尝试在处删除最后一个,
,这是一个测试JSON对象“,
啊,抱歉!我会编辑它!你怎么回事啊?将上面的脚本和代码结合起来?PHP中的实现?您想编辑/删除浏览器内容JSON文件吗?将浏览器内容添加/编辑/删除到JSON文件。我投票结束这个问题,因为我觉得它不适合本网站的格式,如中所述。这不是一个要求人们为你编写代码的网站,也不适合对实现某种目标的方法进行深入讨论。如果到目前为止您所编写的代码存在特定问题,您可以编写一个更为集中的问题。因此,只需添加isset和unset,创建函数并将它们链接在一起即可。谢谢