PHP CRUD JSON文件而不是mysql之类的数据库 [英] PHP CRUD JSON file instead of a database like mysql

查看:89
本文介绍了PHP CRUD JSON文件而不是mysql之类的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点咸菜,我收到一个创建CRUD应用程序的请求,以编辑数组中的JSON对象并上传它们.我做了一些研究,发现 http://www.taffydb.com/却没有完全符合我的要求.

I'm in a bit of pickle, I received a request to create CRUD app to edit JSON object in an array and upload them. I did a bit of research and came across http://www.taffydb.com/ but it doesn't really fit my requirements.

我也遇到过jtables,但是它使用mysql,类似于 http://www.jeasyui.com/tutorial/app/crud.php ,但它也使用mysql.是否可以跳过数据库部分并直接写入JSON文件?

I also came across jtables, but it use mysql, similar to http://www.jeasyui.com/tutorial/app/crud.php but it also uses mysql. Is it possible to skip the database portion and write directly to the JSON file?

由于这是一个非常小的JSON文件,因此拥有数据库似乎过多.我在JSON数组中有多个对象,外部应用程序将从中读取这些对象.

As this is a very small JSON file, having a database would seem excessive. I have multiple objects in the JSON array which an external app would read from.

我可以选择哪些可行的选择?理想情况下,应用程序需要从浏览器内容添加/编辑/删除到JSON文件.

What are the viable options I could go for? Ideally the app needs to Add/Edit/Delete from the browser contents to JSON file.

目前,我可以在表格中相应地显示数据.我的代码如下:

Currently I'm able to display the data accordingly in tables. My code looks like this:

PHP:

<?php
$getfile = file_get_contents('test.json');
$jsonfile = json_decode($getfile);
?>

HTML:

<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>

JSON:

{
  "playlist": [
    {
      "title": "Test title",
      "title_bg": "link/to/image.png",
      "link": "https://www.google.com",
      "description": "This is a test JSON Object"
    }
  ]
}

JSON文件在playlist数组中将有多个对象

The JSON file will have multiple objects in the playlist array

推荐答案

您可以通过以下方式做到这一点:

You can do it in this way:

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>

edit.php

<?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; ?>

delete.php

<?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");
}

add.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.您还必须更改环境链接的地址.

To run the script you must specify in each of these files, a file path JSON. You have to also change the address of links to your environment.

这篇关于PHP CRUD JSON文件而不是mysql之类的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆