Displaying simple blog or news category list in side bar, and archives

4 posts by 2 authors in: Forums > CMS Builder
Last Post: November 17, 2015   (RSS)

By geminiman7 - November 6, 2015

Hi everyone,

I've searched the forums for some time and not finding the right answer that I'm looking for. I'm basically just trying to figure out how to display a list of categories for blog posts or news articles, as well as a simple "Month, Year" archive links.

Please see the example web page below, then look in the sidebar underneath the "Subscribe for Updates" section and you'll see exactly what I'm trying to accomplish:

http://goo.gl/cSLcLT

In CMSB, I have a section called "blog", and a field list named "category", with an alphabetized list of category names to choose from for each post. What I'd like to achieve is:

  1. For the Archives, have each hyperlink that says "Month, Year" link to a page that only shows all articles for that month/year.
  2. For the Categories, have each hyperlink that says "CatName" link to a page that shows all articles associated with that specific category.

I can't seem to figure out how to display the links in the sidebar properly, and also need help generating the correct page that filters the results based on Month/Year and Category.

Thanks in advance...

Attachments:

SideBarCapture.JPG 60K

By Daryl - November 10, 2015

Hi geminiman7,

To build a link for Archives and Categories sidebar, load the blog records first and get each records' date and category.
I.e. blog dates are from the 'createdDate' field which will be stored as array in $archiveDates, categories are from 'category' field which will be stored as array in $archiveCategories

list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'loadUploads' => false,
    'allowSearch' => false,
  ));

$archiveDates      = array(); // for "ARCHIVES" sidebar links
$archiveCategories = array(); // for "CATEGORIES" sidebar links
foreach ($blogRecords as $record){
  $archiveDates[date('Y-m', strtotime($record['createdDate']))] = date('F, Y', strtotime($record['createdDate']));
  $archiveCategories[$record['category']]                       = $record['category:label'];
}

And to display the links for the "Archives", loop through the $archiveDates. Ie:

// display M, Y links
foreach ($archiveDates as $linkParam => $monthYear){
  echo "<a href='blog-archive.php?date=".htmlencode($linkParam)."'>".htmlencode($monthYear)."</a>";
}

Do the same for "Categories". Ie:

// display category links
foreach ($archiveCategories as $linkParam => $categoryName){
  echo "<a href='blog-archive.php?categoryNum=".htmlencode($linkParam)."'>".htmlencode($categoryName)."</a>";
}

Finally, to select the blog records depending on the selected date or category, you can build a where clause based from the URL parameter. Ie:

// archive list page
$where = 'TRUE';
if (@$_REQUEST['date']){
  $where .= " AND createdDate LIKE '".mysql_escape($_REQUEST['date'])."%'";
}
elseif (@$_REQUEST['categoryNum']){
  $where .= " AND category = '".mysql_escape($_REQUEST['categoryNum'])."'";
}

list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'where'       => $where,
    'loadUploads' => true,
    'allowSearch' => false,
  ));

Hope that helps!

Cheers,

Daryl Maximo
PHP Programmer - interactivetools.com

By geminiman7 - November 12, 2015

Tremendous, thank you! I've been playing around with this the past few days and got the archive and categories to display in the sidebar. I couldn't get the categories to appear at first however, but after troubleshooting the code some I noticed that you used $record['category:label']; for some reason. I don't understand the ":label" part, so I removed it just to see what would happen and then it started working. What exactly does :label do?

The only issue I am running into now is when you click on an archive page or category page link, the archive and category links in the sidebar now display this:

Notice: Undefined variable: archiveDates in /home/chroniccc/public_html/blog/archive.php on line 264 Warning: Invalid argument supplied for foreach() in /home/chroniccc/public_html/blog/archive.php on line 264

Notice: Undefined variable: archiveCategories in /home/chroniccc/public_html/blog/archive.php on line 272 Warning: Invalid argument supplied for foreach() in /home/chroniccc/public_html/blog/archive.php on line 272

I figured that this has something to do with the additional code on the archive.php page. Is there any way to make it so that the archive and category links still appear on the archive.php page? Screenshot is attached.

Thanks again!

Attachments:

ArchivePageError.JPG 129K

By Daryl - November 17, 2015

Hi geminiman7,

The $record['listFieldName:label'] is a variable added by getRecords() function when you have a list field with "fieldValue|fieldLabel" options set in section editor. 
For example, you have a list field with options like below wherein Category B selected:
1|Category A
2|Category B
3|Category C

The $record['listFieldName'] will return '2'. While $record['listFieldName:label'] will return 'Category B'. 

For the issue with missing variables, you should also add the getRecords() and foreach block for constructing the $archiveDates and $archiveCategories and rename the $blogRecords variable so it doesn't conflict with the one you used for displaying the archive list.
For example:

// blog records for sidebar archive menu
list($sidebarArchiveBlogRecords, $archiveBlogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'loadUploads' => false,
    'allowSearch' => false,
  ));

$archiveDates      = array(); // for "ARCHIVES" sidebar links
$archiveCategories = array(); // for "CATEGORIES" sidebar links
foreach ($sidebarArchiveBlogRecords as $record){
  $archiveDates[date('Y-m', strtotime($record['createdDate']))] = date('F, Y', strtotime($record['createdDate']));
  $archiveCategories[$record['category']]                       = $record['category:label'];
}

If you're going to have the sidebar in other pages, you might want to put the sidebar code into another file then use "include()" to display it in other pages.

Let me know any questions.

Cheers,

Daryl Maximo
PHP Programmer - interactivetools.com