Number of Listings in the Last Month Feature

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.

2Capture

This is the code (it resides in menuheader.php so is on every page)

<pre>

<?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 $num_rows;
echo “&nbsp Homes listed in the last month”;
?>

</pre>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s