Count News Items That Match Month & Year (and link to them)

9 posts by 2 authors in: Forums > CMS Builder
Last Post: August 9, 2012   (RSS)

By benedict - July 2, 2012

Hi guys,

I am using your new RSS feature with my news_list/news_detail pages and now to make the blog style complete, I would like to put a list of months/years down the right hand side of the page so users can click through to archives of posts:

June 2012 (6 posts)
May 2012 (4 posts)
Apr 2012 (11 posts)
etc...

Two questions:

1. How do I create the link to posts that match BOTH the month/year?
2. How do I create the count of the number of posts as above in brackets?

Thanks!

Re: [benedict] Count News Items That Match Month & Year (and link to them)

By Jason - July 2, 2012

Hi,

Here is an untested piece of example code:

<?php foreach (range(date("m"), 1) as $month): ?>
<?php

$month = str_pad($month, 2, 0, STR_PAD_LEFT);
$monthYear = date("Y")."-".$month;
$monthYearString = date("F Y", strtotime($monthYear."-01"));
$count = mysql_count("news", "createdDate LIKE '$monthYear%'");

?>
<a href = "newsList.php?createdDate_keyword=<?php echo $monthYear;?>"><?php echo $monthYearString;?> (<?php echo $count;?> posts)</a><br/>


<?php endforeach ?>


NOTE: You will probably have to change url and field names based on your set up.

This code counts down the months from the current month, to January of the current year. It also bases it's date searches on the createdDate field of the "news" section. It also assumes that you have a page called "newsList.php" to show a list of articles.

Hope this helps get you started
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Count News Items That Match Month & Year (and link to them)

By benedict - July 3, 2012

Works great except for the link:

Example:

<a href = "latest-newslist.php?createdDate_keyword=<?php echo $monthYear;?>"><?php echo $monthYearString;?> (<?php echo $count;?> posts)</a>

Is producing a link:

latest-newslist.php?createdDate=2012-04

Unfortunately, this shows all the records, not just the April ones. I tried dropping 'keyword' as the search method, but still the same. Any ideas?

Re: [benedict] Count News Items That Match Month & Year (and link to them)

By Jason - July 3, 2012

Hi,

Can you post the getRecords() call that you have on latest-newslist.php so I can take a look?

Thanks,
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Count News Items That Match Month & Year (and link to them)

By benedict - July 3, 2012

Ahh - you got me. I had

'allowSearch' => false,

Sorry about that. Thanks for your help - I'll be rolling this out to all my clients.

Re: [Jason] Count News Items That Match Month & Year (and link to them)

By benedict - August 7, 2012

Hi Jason,

Following on from this, I now need to put the count of articles by category instead of by date.

I have this:

<ul class="simple-nav">
<!--SIMPLE UL -->
<li><a href="candy-buffet-blog.php?category=Candy%20In%20The%20News">Candy In The News</a> (21)</li>
<li><a href="candy-buffet-blog.php?category=Candy%20Buffet%20Trends">Candy Buffet Trends</a> (21)</li>
<li><a href="candy-buffet-blog.php?category=Candy%20Buffet%20Co.%20News">Candy Buffet Co. News</a> (21)</li>
</ul>


Two things -

1. How do I count blog posts per category?
2. Am I able to load categories dynamically instead of hardcoding as per above (the categories are just entries in a list field, not pulled from another section)

Cheers.

Re: [benedict] Count News Items That Match Month & Year (and link to them)

By Jason - August 7, 2012

Hi,

Sure, you can use getListOptions() to dynamically loop through your list of categories.

Example:

<ul class="simple-nav">

<?php foreach (getListOptions('news', 'category') as $value => $label): ?>

<?php $count = mysql_count("news", "category = '".mysql_escape($value)."'"); ?>
<li><a href = "candy-buffet-blog.php?category=<?php echo urlencode($value);?>"><?php echo $label;?></a> (<?php echo $count; ?>) </li>

<?php endforeach ?>

</ul>


Note: I've assumed that a news record can only belong to a single category.

Hope this helps get you started
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Count News Items That Match Month & Year (and link to them)

By benedict - August 9, 2012

Works great - thanks!