I guess there could be times when a database is overkill. They're rare, to be sure, and I can't think of a very practical example, but sometimes you just don't need the overhead. Here's some code that keeps a list of names. It can add and remove names, and they are stored on the server in a serialized array.
Use such techniques at your own risk.
Live Demo Link:
http://my.oi-share.com/name_array
Code: Select all
<html>
<head>
<title>My Little Array</title>
</head>
<body>
<h1>My Little Array</h1>
<p>This uses PHP to store values in an array. They are saved on the server.</p>
<?php
//Check to see if names should be added or deleted, and do so.
if($_GET['add']){
$namesArray;
//Read file into namesArray if it is there
if(is_file('names.array')){
$namesArray = unserialize(file_get_contents('names.array'));
}
//Add the new name on to the array. Each is noted by the timestamp so it has a unique id. Note, however, that this is a weak scheme that will break if there is more than one submission a second.
$namesArray[] = stripslashes($_GET['add']);
//re-key array
$namesArray = array_values($namesArray);
//write array back to file
file_put_contents('names.array', serialize($namesArray));
}elseif($_GET['delete']){
$namesArray;
//Read file into namesArray if it is there
if(is_file('names.array')){
$namesArray = unserialize(file_get_contents('names.array'));
}
//unset the array element by key
unset($namesArray[$_GET['key']]);
//re-key array
$namesArray = array_values($namesArray);
//write array back to file
file_put_contents('names.array', serialize($namesArray));
}
//Check if the file names.array exists. If it does, loop through it and list the names along with "delete" links. I want to re-read the array in case something was added or deleted above.
if(is_file('names.array')){
$namesArray = unserialize(file_get_contents('names.array'));
foreach(array_keys($namesArray) as $nameKey){
$name = $namesArray[$nameKey];
echo $nameKey.' | '.$name.' <a href="?delete=true&key='.$nameKey.'">X</a><br/>';
}
}
?>
<!-- Draw a form to be used to add a name -->
<form action="?" method="get">
<input type="text" name="add" /> <input type="submit">
</form>
</body>
</html>
Just to talk about this a bit...
The approach here is to manage the data in an array. In PHP, arrays are easy to work with, sort, add to and remove values from, and they can become a pretty powerful data structure when you create a multi-dimensional array, or even create an array of arrays. This example is not object-oriented at all, of course, you could make it object oriented. If you were developing any more complex use of this, I would recommend that you at least abstract things into functions so that your code is less cluttered. As for the actual performance of this script? It's certainly fast, fast enough that you could have hundreds of things in your array and not notice any performance problems- but it's a simple array. If you are going to be storing several dozen profiles of information, avoid arrays all-together and go with a database. If you feel your project is too small for a full DB like MySQL, try SQLite as a fully relational alternative.
Good Luck!