Another Blog Question - Archives

3 posts by 2 authors in: Forums > CMS Builder
Last Post: January 24, 2012   (RSS)

By dwellingproductions - January 24, 2012

One more question pertaining to the blog I'm creating. :-)

You can see the my sample page here: http://www.ihelpinc.net/Blog/category.php

The main blog list is here: http://www.ihelpinc.net/Blog/index.php

In order to create an archive function of the blog (to list blog posts by month and year, etc.), I used the suggestion given here: http://www.interactivetools.com/iforum/gforum.cgi?post=83311;search_string=blog;t=search_engine#83311. (THANKS DONNA!) :-)

It works great. However, I seem to be experiencing an unwanted effect when selecting a date from the archive.

** When clicking an "archive" link (such as "January 2012"), not only does the page display a list of all blog posts from January 2012 (which is correct), but the "Recent Posts" list is also filtered. Obviously, we want the "Recent Posts" to always show the same recent posts and not be changed or filtered by selections made in the "Archive" or "Category" sections. Is there a way to close off the "Recent Posts" section to keep it from being affected by "Archive" or "Category" selections?

Here is some of the code I'm using:

FROM PAGE TOP:

// load records
list($blog_contentRecords, $blog_contentMetaData) = getRecords(array(
'tableName' => 'blog_content',
'loadUploads' => '0',
));
$blog_contentRecord = @$blog_contentRecords[0]; // get first record


// load records
list($blog_categoriesRecords, $blog_categoriesMetaData) = getRecords(array(
'tableName' => 'blog_categories',
'loadUploads' => '0',
'allowSearch' => '0',
));



FROM PAGE BODY:

<!-- RECENT POSTS MODULE -->
<div id="BigRightBottom">
<h3 style="margin-left:-10px; color:#333;">RECENT POSTS</h3>
<?php
// load records
list($blog_contentRecords, $blog_contentMetaData) = getRecords(array(
'tableName' => 'blog_content',
'limit' => '7',
'loadUploads' => '0',
));
?>
<p style="margin: 0px 10px 0px 10px;">
<?php foreach ($blog_contentRecords as $record): ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br />
<?php endforeach ?>
</p>
</div>
<!-- /end RECENT POSTS MODULE -->




<!-- BLOG ARCHIVE MODULE -->
<div id="BigRightBottom">
<h3 style="margin-left:-10px; color:#333;">ARCHIVE</h3>
<p style="margin: 0px 10px 0px 10px;">
<?php require_once "/home/content/66/8739966/html/cmsAdmin/lib/viewer_functions.php"; ?>

<?php
// get list of unique months and years with articles
$query = "SELECT DATE_FORMAT(date, '%M %Y') as dateAndYear, YEAR(date) as year, MONTH(date) as month FROM cms_blog_content GROUP BY dateAndYear ORDER BY date DESC";
$result = mysql_query($query) or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n");
while ($record = mysql_fetch_assoc($result)):
?>

<a href="index.php?date_year=<?php echo $record['year'] ?>&date_month=<?php echo $record['month'] ?>"><?php echo $record['dateAndYear']; ?></a><br/>

<?php endwhile ?>
</p>
</div>
<!-- /end BLOG ARCHIVE MODULE -->



Thanks for any assistance!

Kindest regards,
Jeremy
---------------------------

Dwelling Productions

www.dwellingproductions.com

Re: [dwelling] Another Blog Question - Archives

By Jason - January 24, 2012

Hi Jeremy,

What's happening here is you are using 2 separate queries that both use the variable name $blog_contentRecords. So your second query actually overwrites the results of your first. Also, you'll want to turn off automatic searching in your second query like this:

example
<!-- RECENT POSTS MODULE -->
<div id="BigRightBottom">
<h3 style="margin-left:-10px; color:#333;">RECENT POSTS</h3>
<?php
// load records
list($blog_recentRecords, $blog_recentMetaData) = getRecords(array(
'tableName' => 'blog_content',
'limit' => '7',
'loadUploads' => '0',
'orderBy' => 'createdDate DESC',
'allowSearch' => false,

));
?>
<p style="margin: 0px 10px 0px 10px;">
<?php foreach ($blog_recentRecords as $record): ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br />
<?php endforeach ?>
</p>
</div>


I also added an 'order' option to make sure the 7 most recent records get sorted to the top of your list.


The other thing I noticed is that you have a require_once statement part way down you page that includes viewer_functions.php. This line isn't necessary since you are already requiring this file up at the top of your page. This won't cause any errors, but removing it will help keep your code cleaner:


<!-- BLOG ARCHIVE MODULE -->
<div id="BigRightBottom">
<h3 style="margin-left:-10px; color:#333;">ARCHIVE</h3>
<p style="margin: 0px 10px 0px 10px;">
<?php require_once "/home/content/66/8739966/html/cmsAdmin/lib/viewer_functions.php"; ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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