Simple List
← Return
This demo shows a simple list of items which are stored in a text file.
Simple List Code
Download
ITEMS/index.php ▾
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
main {
width: 600px;
margin: auto;
color: darkblue;
text-align: left;
}
a {
text-decoration: none;
}
h1{
text-align: center;
color: darkblue;
}
h4, i {
color: darkmagenta;
}
</style>
</head>
<body>
<main>
<a href = '../../simple-list'>← Return</a><br><br>
<a href = 'index.php'><h1>Your Items</h1></a>
<i>(Number of items limited to 7, number of submissions limited to 10)</i>
<?php
//Reset Items each time app is started from Projects page
$item2 = '';
if (isset ($_GET['flag'])) {
if ($_GET['flag'] === 'start') {
$items = 'Item1,Item2,Item3';
file_put_contents ('items.txt', $items);
}
}
$string1 = file_get_contents ('items.txt');
$array1 = explode (',', $string1);
if ($_SERVER ["REQUEST_METHOD"] == "POST" ) {
$newitem = $_POST['newitem'];
if ($newitem ) {
$newitem = preg_replace('/[^A-Za-z0-9-]/', '', $newitem);
if (in_array ( $newitem, $array1) ) {
echo "<h4>" . $newitem . " already exists</h4>";
}
else {
if (count ($array1) >= 7) {
array_shift ($array1);
echo "<h4>Limit reached - first item removed</h4>";
}
array_push ($array1, $newitem);
$string1 = implode (',', $array1);
ltrim ($string1, ',');
file_put_contents ('items.txt', $string1);
echo "<h4>" . $newitem . " added </h4>";
}
}
}
$string1 = file_get_contents ('items.txt');
$array1 = explode (',', $string1);
if ($string1) {
echo "<h3>Current Items:</h3>";
echo "<ul>";
foreach ($array1 as $item1) {
if ($item1) {
echo "<li>" . $item1 . "</li>";
}
}
echo "</ul>";
}
?>
<br><h3>Add New Item</h3>
<form action="index.php?page=add-list" method="post" >
<input type = 'text' name = 'newitem' /><br><br>
<input class = 'submitbutton' type = 'submit' value = 'Submit' name = 'submit-new'>
</form>
</main>
</body>
</html>
