With the current application you may do the following – enter an expense and select what kind of expense it is. I have predefined some of the expenses, but you may add your own later.
What the PHP application actually does is the following – it gives you an ability to sort my expenses by type and then you will be able to have the total sum per type. Check it out by yourself:
Expenses Sorter GitHub Application
So how does it work?
At first, we see a table, which shows all the expenses, which we have made so far. We may filter the expenses, using the filter button. This is the code for this table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<?php $pageTitle = 'List'; include 'includes/header.php'; if ($_POST) { $selectedGroup = 0; $selectedGroup = (int) $_POST['filter']; } ?> <form method ="POST"> <a href ="form.php">Add</a> <select name="filter"> <?php echo '<option value = "0">All</option>'; foreach ($groups as $key => $value) { echo '<option value="' . $key . '">' . $value . '</option>'; } ?> </select> <input type="submit" value="Filter" /> </form> <table border ="1"> <tr> <td>Date</td> <td>Expense</td> <td>Sum</td> <td>Type</td> </tr> <?php if (file_exists('data.txt')) { $result = file('data.txt'); $selectedGroup = 0; if ($_POST) { $selectedGroup = (int) $_POST['filter']; } $sum = 0; foreach ($result as $value) { $columns = explode('!', $value); if (count($columns) < 4) { continue; } if ($selectedGroup != $columns[3] && $selectedGroup != 0) { continue; } $sum += (float) $columns[2]; echo '<tr> <td>' . $columns[0] . '</td> <td>' . $columns[1] . '</td> <td>' . $columns[2] . '</td> <td>' . $groups[trim($columns[3])] . '</td> </tr>'; } echo '<tr> <td>' . '---' . '</td> <td>' . '---' . '</td> <td>' . $sum . '</td> <td>' . '---' . '</td> </tr>'; } ?> </table> <?php include 'includes/footer.php'; ?> |
The second thing we have here is a small interface, used to enter data in the table. This interface is accessible through the small link “Add”, visible in the top left corner of the application. This is the code, behind the submission form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php mb_internal_encoding('UTF-8'); $pageTitle = 'Форма'; include 'includes/header.php'; if($_POST){ $name=trim($_POST['name']); $name= str_replace('!', '', $name); $cost=trim($_POST['cost']); $cost= (float)str_replace('!', '', $cost); $selectedGroup=(int)$_POST['group']; $error=false; if(mb_strlen($name)<2){ echo '<p>The name is too short! Please, try again!</p>'; $error=true; } if($cost<0){ echo '<p>Invalid sum</p>'; $error=true; } if(!array_key_exists($selectedGroup, $groups)){ echo '<p>Invalid Group</p>'; $error=true; } $today = getdate(); // print_r($today); if(!$error){ $result=$today['mday'].'.'.$today['mon'].'.'.$today['year'].'!'.$name.'!'.$cost.'!'.$selectedGroup."\n\r"; if(file_put_contents('data.txt', $result,FILE_APPEND)) { echo 'Success'; } } } ?> <a href ="index.php">Show list</a> <form method="POST"> <div>Description:<input type="text" name="name" /></div> <div>Price:<input type="text" name="cost" /></div> <div> <span>Type:</span> <select name="group"> <?php foreach ($groups as $key => $value) { echo '<option value="' . $key . '">' . $value . '</option>'; } ?> </select> </div> <div><input type="submit" value="Add" /></div> </form> <?php include 'includes/footer.php'; ?> |
The third thing, you may need to create such a basic application is a file, where you save all the data, constants, footer and a header.
The header and the footer are set as separated files, which are used from both the form and the list.
Pretty much, that is it. It is really beautiful and it can be really useful, if you set the correct procedures for entering data.
Enjoy the code! It is absolutely freeware!
😀