I decided that it would be a great feature if the web app could display how many homes had been listed in the last month (30 days) from the current date. So after a late night, for such a small amount of code, I managed to add this feature. One of the reasons it took so long was that I had trouble working out if it was the php or the query that was stopping it from working. I resolved this by running the query from within phpmyadmin (sql tab), which showed me the query was not working, as it will report an error if the query is not valid. After several attempts I got the query to work and with a few small tweaks to the php it worked perfectly. I was quite surprised that I did not come across testing querys this way online?
The query uses the date in the table (input from the form) to identify listings from the last thirty days. Its quite a simple piece of code really, but was very satisfying to see it working in the web app and it demonstrates using data from the database that is automatically updated.
This image shows it working on the site.
This is the code (it resides in menuheader.php so is on every page)
<?php //Displays how many properties have been listed in the database in the last 30 days.
$q = “SELECT `date_added` FROM `posts` where `date_added` < NOW()-30”;
$month = mysqli_query($connect, $q);
$num_rows = $month->num_rows;
echo “  Homes listed in the last month”;