PHP CRUD JSON file instead of a database like mysql





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







5















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.



EDIT: 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?



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.



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"
}
]
}


The JSON file will have multiple objects in the playlist array










share|improve this question

























  • This json is invalid!! Try to remove last , at This is a test JSON Object",

    – Saty
    Jul 11 '16 at 7:38













  • Ah apologies! WIll edit it!

    – fupuchu
    Jul 11 '16 at 7:41











  • What's your problem? With a combination of the above your script with your code? Implementations in the PHP? Do you want to be able to edit / delete from the browser contents JSON file?

    – rad11
    Jul 11 '16 at 9:28











  • Add/Edit/Delete from the browser contents to JSON file.

    – fupuchu
    Jul 11 '16 at 9:30











  • I'm voting to close this question, because I don't feel like it fits the format of this site as discussed in the help center. This isn't a site for requesting people to write code for you, nor is it suited to extended discussion on the ways of achieving something. If there's a specifc problem with the code you've written so far, you could write a more focussed question about that.

    – IMSoP
    Jul 11 '16 at 9:54


















5















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.



EDIT: 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?



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.



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"
}
]
}


The JSON file will have multiple objects in the playlist array










share|improve this question

























  • This json is invalid!! Try to remove last , at This is a test JSON Object",

    – Saty
    Jul 11 '16 at 7:38













  • Ah apologies! WIll edit it!

    – fupuchu
    Jul 11 '16 at 7:41











  • What's your problem? With a combination of the above your script with your code? Implementations in the PHP? Do you want to be able to edit / delete from the browser contents JSON file?

    – rad11
    Jul 11 '16 at 9:28











  • Add/Edit/Delete from the browser contents to JSON file.

    – fupuchu
    Jul 11 '16 at 9:30











  • I'm voting to close this question, because I don't feel like it fits the format of this site as discussed in the help center. This isn't a site for requesting people to write code for you, nor is it suited to extended discussion on the ways of achieving something. If there's a specifc problem with the code you've written so far, you could write a more focussed question about that.

    – IMSoP
    Jul 11 '16 at 9:54














5












5








5


2






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.



EDIT: 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?



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.



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"
}
]
}


The JSON file will have multiple objects in the playlist array










share|improve this question
















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.



EDIT: 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?



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.



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"
}
]
}


The JSON file will have multiple objects in the playlist array







php json crud






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 11 '16 at 9:29







fupuchu

















asked Jul 11 '16 at 7:34









fupuchufupuchu

7218




7218













  • This json is invalid!! Try to remove last , at This is a test JSON Object",

    – Saty
    Jul 11 '16 at 7:38













  • Ah apologies! WIll edit it!

    – fupuchu
    Jul 11 '16 at 7:41











  • What's your problem? With a combination of the above your script with your code? Implementations in the PHP? Do you want to be able to edit / delete from the browser contents JSON file?

    – rad11
    Jul 11 '16 at 9:28











  • Add/Edit/Delete from the browser contents to JSON file.

    – fupuchu
    Jul 11 '16 at 9:30











  • I'm voting to close this question, because I don't feel like it fits the format of this site as discussed in the help center. This isn't a site for requesting people to write code for you, nor is it suited to extended discussion on the ways of achieving something. If there's a specifc problem with the code you've written so far, you could write a more focussed question about that.

    – IMSoP
    Jul 11 '16 at 9:54



















  • This json is invalid!! Try to remove last , at This is a test JSON Object",

    – Saty
    Jul 11 '16 at 7:38













  • Ah apologies! WIll edit it!

    – fupuchu
    Jul 11 '16 at 7:41











  • What's your problem? With a combination of the above your script with your code? Implementations in the PHP? Do you want to be able to edit / delete from the browser contents JSON file?

    – rad11
    Jul 11 '16 at 9:28











  • Add/Edit/Delete from the browser contents to JSON file.

    – fupuchu
    Jul 11 '16 at 9:30











  • I'm voting to close this question, because I don't feel like it fits the format of this site as discussed in the help center. This isn't a site for requesting people to write code for you, nor is it suited to extended discussion on the ways of achieving something. If there's a specifc problem with the code you've written so far, you could write a more focussed question about that.

    – IMSoP
    Jul 11 '16 at 9:54

















This json is invalid!! Try to remove last , at This is a test JSON Object",

– Saty
Jul 11 '16 at 7:38







This json is invalid!! Try to remove last , at This is a test JSON Object",

– Saty
Jul 11 '16 at 7:38















Ah apologies! WIll edit it!

– fupuchu
Jul 11 '16 at 7:41





Ah apologies! WIll edit it!

– fupuchu
Jul 11 '16 at 7:41













What's your problem? With a combination of the above your script with your code? Implementations in the PHP? Do you want to be able to edit / delete from the browser contents JSON file?

– rad11
Jul 11 '16 at 9:28





What's your problem? With a combination of the above your script with your code? Implementations in the PHP? Do you want to be able to edit / delete from the browser contents JSON file?

– rad11
Jul 11 '16 at 9:28













Add/Edit/Delete from the browser contents to JSON file.

– fupuchu
Jul 11 '16 at 9:30





Add/Edit/Delete from the browser contents to JSON file.

– fupuchu
Jul 11 '16 at 9:30













I'm voting to close this question, because I don't feel like it fits the format of this site as discussed in the help center. This isn't a site for requesting people to write code for you, nor is it suited to extended discussion on the ways of achieving something. If there's a specifc problem with the code you've written so far, you could write a more focussed question about that.

– IMSoP
Jul 11 '16 at 9:54





I'm voting to close this question, because I don't feel like it fits the format of this site as discussed in the help center. This isn't a site for requesting people to write code for you, nor is it suited to extended discussion on the ways of achieving something. If there's a specifc problem with the code you've written so far, you could write a more focussed question about that.

– IMSoP
Jul 11 '16 at 9:54












1 Answer
1






active

oldest

votes


















10














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


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.






share|improve this answer


























  • So it is simply adding isset and unset, creating the functions and linking them together?

    – fupuchu
    Jul 11 '16 at 10:11











  • Yes, it is simple to manage on a json file

    – rad11
    Jul 11 '16 at 10:13











  • Ahh, I see, thanks so much!

    – fupuchu
    Jul 11 '16 at 10:16











  • Thank you for this

    – Carl
    Nov 8 '18 at 11:14












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38301457%2fphp-crud-json-file-instead-of-a-database-like-mysql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









10














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


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.






share|improve this answer


























  • So it is simply adding isset and unset, creating the functions and linking them together?

    – fupuchu
    Jul 11 '16 at 10:11











  • Yes, it is simple to manage on a json file

    – rad11
    Jul 11 '16 at 10:13











  • Ahh, I see, thanks so much!

    – fupuchu
    Jul 11 '16 at 10:16











  • Thank you for this

    – Carl
    Nov 8 '18 at 11:14
















10














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


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.






share|improve this answer


























  • So it is simply adding isset and unset, creating the functions and linking them together?

    – fupuchu
    Jul 11 '16 at 10:11











  • Yes, it is simple to manage on a json file

    – rad11
    Jul 11 '16 at 10:13











  • Ahh, I see, thanks so much!

    – fupuchu
    Jul 11 '16 at 10:16











  • Thank you for this

    – Carl
    Nov 8 '18 at 11:14














10












10








10







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


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.






share|improve this answer















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


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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 11 '16 at 10:00

























answered Jul 11 '16 at 9:51









rad11rad11

1,07421222




1,07421222













  • So it is simply adding isset and unset, creating the functions and linking them together?

    – fupuchu
    Jul 11 '16 at 10:11











  • Yes, it is simple to manage on a json file

    – rad11
    Jul 11 '16 at 10:13











  • Ahh, I see, thanks so much!

    – fupuchu
    Jul 11 '16 at 10:16











  • Thank you for this

    – Carl
    Nov 8 '18 at 11:14



















  • So it is simply adding isset and unset, creating the functions and linking them together?

    – fupuchu
    Jul 11 '16 at 10:11











  • Yes, it is simple to manage on a json file

    – rad11
    Jul 11 '16 at 10:13











  • Ahh, I see, thanks so much!

    – fupuchu
    Jul 11 '16 at 10:16











  • Thank you for this

    – Carl
    Nov 8 '18 at 11:14

















So it is simply adding isset and unset, creating the functions and linking them together?

– fupuchu
Jul 11 '16 at 10:11





So it is simply adding isset and unset, creating the functions and linking them together?

– fupuchu
Jul 11 '16 at 10:11













Yes, it is simple to manage on a json file

– rad11
Jul 11 '16 at 10:13





Yes, it is simple to manage on a json file

– rad11
Jul 11 '16 at 10:13













Ahh, I see, thanks so much!

– fupuchu
Jul 11 '16 at 10:16





Ahh, I see, thanks so much!

– fupuchu
Jul 11 '16 at 10:16













Thank you for this

– Carl
Nov 8 '18 at 11:14





Thank you for this

– Carl
Nov 8 '18 at 11:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38301457%2fphp-crud-json-file-instead-of-a-database-like-mysql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy