Skip to content

Instantly share code, notes, and snippets.

@ModsByMorgue
Last active January 10, 2021 23:39
Show Gist options
  • Save ModsByMorgue/703ff60b2eadc137810d35f7074f6b41 to your computer and use it in GitHub Desktop.
Save ModsByMorgue/703ff60b2eadc137810d35f7074f6b41 to your computer and use it in GitHub Desktop.
Minimal News System Example Project - PHP & JSON
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Bare - Start Bootstrap Template</title>
<link rel="icon" href="https://getbootstrap.com/docs/5.0/assets/img/favicons/favicon.ico">
<!-- Bootstrap core CSS --><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<style type="text/css">
</style>
</head>
<body>
<!-- Page Content -->
<div class="container bg-light">
<?php
$file_name = 'database.json';
if (isset($_GET['reset'])) {
file_put_contents($file_name, '');
echo '<h1>Database reset! Reloading page..</h1>';
echo '<meta http-equiv="refresh" content="1; URL=./" />';
exit;
}
if (isset($_GET['upload'])) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (file_put_contents($file_name, get_and_write())) {
echo '<h1>Upload completed! Reloading page..</h1>';
echo '<meta http-equiv="refresh" content="1; URL=./" />';
exit;
} else {
echo '<h1>Error updating the contents of file "'.$file_name.'"...</h1>';
exit;
}
} else {
echo '<h1>INVALID POST</h1>';
exit;
}
} else {
echo '<h1>Create Article [<a href="./?reset">Reset Database</a>]</h1>';
echo '<form method="post" action="./?upload">';
echo '<input type="text" name="title" required placeholder="Title Here">';
echo '<input type="text" name="message" required placeholder="Message Here">';
echo '<input type="text" name="author" placeholder="Anonymous">';
echo '<input id="submit" type="submit" name="submit" value="Submit">';
echo '</form>';
}
echo '<hr>';
if (isset($_GET['read'])) {
$request_id = $_GET['read'];
read_and_print($request_id);
} else {
read_and_print();
}
function get_and_write() {
global $file_name;
// Make sure the file exists.
if (file_exists($file_name) == false) {
$file = fopen($file_name, "w");
fclose($file);
}
// Get the file contents.
$file_data = file_get_contents($file_name);
// Decode the file contents.
$data_list = json_decode($file_data, true);
// Add the new data to the file contents.
$addition = array(
'Title' => $_POST['title'],
'Message' => $_POST['message'],
'Author' => $_POST['author'],
'Date' => date('F d, Y (h:i:s)')
);
$data_list[] = $addition;
// Write the data to the file.
return json_encode($data_list);
}
function read_and_print($request_id = -1) {
global $file_name;
// Make sure file exists.
if (file_exists($file_name) == false) {
die('File "'.$file_name.'" does not exist.');
exit;
}
// Get file contents.
$json_file = file_get_contents($file_name);
if ($json_file == false) {
die('No data found in file.');
}
// Decode file contents.
$json_data = json_decode($json_file, true);
if ($json_data == null) {
die('JSON data returned null.');
}
// Print the file contents onto the page (in reverse order).
echo '<h1>News Archive [<a href="./">View All</a>]</h1>';
$length = count($json_data);
$index = -1;
foreach (array_reverse($json_data) as $object) {
$index++;
$read_id = ($length - $index);
if ($request_id == -1) {
echo '<p><b>[<a href="./?read='.$read_id.'">#'.$read_id.'</a>]'.$object['Title'].'</b> <small>'.$object['Date'].'</small></h3>';
echo '<p>'.$object['Message'].'</p>';
echo '<hr>';
} else {
if ($read_id == $request_id) {
echo '<p><b>[<a href="./?read='.$read_id.'">#'.$read_id.'</a>]'.$object['Title'].'</b> <small>'.$object['Date'].'</small></h3>';
echo '<p>'.$object['Message'].'</p>';
echo '<hr>';
return;
}
}
}
}
?>
</div><!-- /.container -->
<!-- Bootstrap core JavaScript --><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment