How to display total number of ads in Classifieds website?

Displaying total number of ads will encourage site visitors to post their ads and will give some credibility to your classifieds website. Also this will work positive if you have more than 100 ads, other vise your site may give impression of dead website to new visitor.

In Classibase we have very flexible “Text” widget which can be regular text, JavaScript or PHP. For displaying total number of ads, ads posted last month and last day we will use PHP. Getting total number of ads in Classibase may be tricky for some users that is why we created this tutorial to give exact PHP code that you can use for your website.

Here is PHP code that will get number of ads total, last month and last day:

Total ads <b><?php echo Ad::countByClass('Ad', 'listed=?', array(1));?></b>, last month <b><?php echo Ad::countByClass('Ad', 'listed=? AND added_at>?', array(1,floor(REQUEST_TIME/3600)*3600-3600*24*30));?></b>, last day <b><?php echo Ad::countByClass('Ad', 'listed=? AND added_at>?', array(1,floor(REQUEST_TIME/3600)*3600-3600*24));?></b>

Paste it to your text widget and select text format as PHP as shown in image. To read more about how to place text widget read this tutorial.

This code will display result like displayed below:

Total ads 3439, last month 865, last day 28


If you have multilingual classifieds website then remember pasting given text for all languages, otherwise it will not show for other languages.

We used Ad::countByClass('Ad', 'listed=?', array(1)); method to get total number of live ads.

For day and month totals we used floor(REQUEST_TIME/3600)*3600 to round time because Ad::countByClass method have built in caching feature which will use every parameter as cache key. So this will keep database queries to your server at minimum level by caching results.

Also you can style your code using HTML and CSS if you want total number of ads to catch attention of your visitors.

191 (+1) visits