counts not working properly

3 posts by 2 authors in: Forums > CMS Builder
Last Post: October 31   (RSS)

By craig_bcd - October 27, 2023

Hi All - 

I am sure this is staring me in the face but I don't know why it isn't working.  I have a blog on this new site and when it counts the archive articles per month on the main page the article numbers are incorrect (it attributes 4 articles to each month) it counts all articles rather than just the articles for that month.  However, when you click on a month to see those articles the counts are correct (2 articles each month) and it is the same code on the same page driving it.

You can see the in process site here: https://craigh103.sg-host.com/blog.php

I attached the page as well as images of the numbers when it initially loads vs when you select one of the archive months.

Any assitance is appreciated. 

Craig

By Steve99 - October 31

Hi Craig,

$blogMetaData['totalRecords'] provides a count of all records returned from getRecords.

Modifying your existing code, you can run a query inside the following foreach loop to get counts for each Year/Month archive.

Try changing this:

<?php foreach($months as $date => $name): ?>
    <li><a href="blog.php?createdDate_keyword=<?php echo "$date";?>" ><?php echo $name;?> <?php echo $year;?><span><?php echo $blogMetaData['totalRecords']; ?></span><i class=" icon-right-open"></i></a></li>
<?php endforeach ?>

To this:

<?php foreach($months as $date => $name): ?>
<?php $archiveMonthCount = mysql_count('blog', "`createdDate` LIKE '$date%'"); ?>
    <li><a href="blog.php?createdDate_keyword=<?php echo "$date";?>" ><?php echo $name;?> <?php echo $year;?><span><?php echo $archiveMonthCount; ?></span><i class=" icon-right-open"></i></a></li>
<?php endforeach ?>

mysql_count is an internal CMSB function that returns the number of records found. For the WHERE clause, we're searching createdDate using the LIKE operator with wildcard % on your $date string (ex. 2023-09). This will match createdDate fields starting with your $date string value.

Hope this helps!

Best,
Steve