SORTING BLOG POSTS BY KEYWORDS IN MULTIPLE FIELDS AND AN AVAILABLE DATE FILTER ON ONE VIEWER - Jan 7th, 2019


The previous recipes addressed a basic approach to creating a Blog or Articles editor (really the same thing unless you
are allowing user posts on your blog).

Here’s an approach that puts the pieces together.

It uses a single viewer, and allows visitors to search by multiple field keyword criteria, or to filter their results
from a list that only contains available dates (Month and Year), and put all of this in a neat set of pull down form
fields.

The approach uses 2 forms (one for the keyword searches and one for the available date filters) and sets a variable
called $archive depending on which one is used (I’m sure that there’s a more elegant way to do this, and welcome
anyone’s input).

The 2 fields that are set for keyword searches in this example are ‘article_type’ and ‘publication’, and the
date filter uses month and year.

You’ll need to set up 3 multi-record tables to follow this example:

A table called article_types with one text field called ‘title’
A table called publication_names with one text field called ‘title’
A table called Articles with the following minimum set of fields

A date field called publication_date
A text field called article_title
2 list fields, one called ‘article_type and the other called ‘publication’ (these get their option values and
option labels from the other 2 tables)

Here’s the code:

At the top of your page



<?php

require_once "/path_to_your_server/html/cmsAdmin/lib/viewer_functions.php";

list(
$article_typesRecords, $article_typesMetaData) = getRecords(array(
'tableName' => 'article_types',

));

list(
$publication_namesRecords, $publication_namesMetaData) = getRecords(array(
'tableName' => 'publication_names',

));

?>


And in the body:



<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="hidden" name="archive" value="0">
<table >
<tr>
<td><select name="article_type_keyword" style="width:350px; ">
<option value="">Choose An Article Type To Display</option>
<?php foreach ($article_typesRecords as $record): ?>
<option value="<?php echo $record['num'] ?>"><?php echo $record['type'] ?></option>
<?php endforeach ?>
</select></td>
<td><input type="submit" name="submit" value="Filter Your Page" style="width:200px; "></td>
</tr>
<tr>
<td><select name="publication_keyword" style="width:350px; " >
<option value="">And/Or Choose A Publication To Display</option>
<?php foreach ($publication_namesRecords as $record): ?>
<option value="<?php echo $record['num'] ?>"><?php echo $record['publication_name'] ?></option>
<?php endforeach ?>
</select></td>
<td><INPUT TYPE="submit" VALUE="Cancel Search Filters" style="width:200px; "></td>
</tr>
</table>
</FORM>
<br />
<?php // insert block 1 (Form) for displaying only current year's articles here ?>
<?php
list($articles_by_ericaRecords, $articles_by_ericaMetaData) = getRecords(array(
'tableName' => 'articles_by_erica',
'loadUploads' => true,
//'allowSearch' => false,
'orderBy' => 'publication_date DESC',
));

?>
<?php // get list of unique months and years with articles ?>
<?php // remove this code for years only: %M and , MONTH(publication_date in the $query ?>
<?php $query = "SELECT DATE_FORMAT(publication_date, '%M %Y') as dateAndYear, YEAR(publication_date) as year,
MONTH(publication_date) as month FROM cms_articles_by_erica WHERE `hidden` = 0 GROUP BY dateAndYear ORDER BY
publication_date DESC";
$result = mysql_query($query) or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n");
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<table >
<tr>
<td ><input type="hidden" name="archive" value="1">
<select name="dateAndYear" style="width:350px;" >
<option value="">Or, Display Articles By Publication Date</option>
<?php
while ($record = mysql_fetch_assoc($result)):?>
<option value="<?php echo $record['dateAndYear'] ?>"><?php echo $record['dateAndYear']; ?></option>
<?php endwhile ?>
</select></td>
<td><input type="submit" name="submit" value="Filter By Date" style="width:200px; "></td>
</tr>
</table>
</form>
<?php @$archive = $_REQUEST['archive']; ?>
<?php if(@$archive== 0 || @$archive==""):?>
<?php // beginning of replaced code block 2 for displaying only current year's articles ?>
<br />
<table >
<?php foreach ($articles_by_ericaRecords as $record): ?>
<tr>
<td ><?php echo $record['article_title'] ?>
<?php if (($record['publication'] && !$record['publication'] =="") ||($record['publication_date'] &&
!
$record['publication_date'] =="") ||($record['article_type'] && !$record['article_type'] =="")):?>
<br />
(
<?php endif ?>
<?php // end of replaced code block 2 for displaying only current year's articles ?>
<?php if ($record['publication'] && !$record['publication'] ==""):?>
<?php echo $record['publication:label'] ?> -
<?php endif ?>
<?php if ($record['publication_date'] && !$record['publication_date'] ==""):?>
<?php echo date("F Y ", strtotime($record['publication_date'])) ?> -
<?php endif ?>
<?php if ($record['article_type'] && !$record['article_type'] ==""):?>
<?php echo $record['article_type:label'] ?>
<?php endif?>
<?php if (($record['publication'] && !$record['publication'] =="") ||($record['publication_date'] &&
!
$record['publication_date'] =="") ||($record['article_type'] && !$record['article_type'] =="")):?>
)
<?php endif ?>
<br /><hr /></td>
</tr>

<?php endforeach; ?>
</table>
<?php endif ?>
<?php if(@$archive== 1):?>
<br />
<h3>Articles by Publication Date</h3>
<?php // remove this code for years only: $date_month, ?>
<?php @list($date_month, $date_year) = explode(' ', @$_REQUEST['dateAndYear']); // split on space
?>
<hr />
<table >
<?php foreach ($articles_by_ericaRecords as $record): ?>
<?php $record_year = date("Y", strtotime($record['publication_date'])) ?>
<?php // remove this code for years only: <?php $record_month = date("F", strtotime($record['publication_date'])) ?>

<?php $record_month = date("F", strtotime($record['publication_date'])) ?>
<?php // remove this code for years only: && ($date_month == $record_month) ?>
<?php if ((@$_REQUEST && $archive == 1) && ($date_year == $record_year) && ($date_month == $record_month) ):?>
<tr>
<td ><?php echo $record['article_title'] ?>
<?php if (($record['publication'] && !$record['publication'] =="")||($record['publication_date'] &&
!
$record['publication_date'] =="") ||($record['article_type'] && !$record['article_type'] =="")):?>
<br />
(
<?php endif ?>
<?php if ($record['publication'] && !$record['publication'] ==""):?>
<?php echo $record['publication:label'] ?> -
<?php endif ?>
<?php if ($record['publication_date'] && !$record['publication_date'] ==""):?>
<?php echo date("F Y ", strtotime($record['publication_date'])) ?> -
<?php endif ?>
<?php if ($record['article_type'] && !$record['article_type'] ==""):?>
<?php echo $record['article_type:label'] ?>
<?php endif?>
<?php if (($record['publication'] && !$record['publication'] =="")||($record['publication_date'] &&
!
$record['publication_date'] =="") ||($record['article_type'] && !$record['article_type'] =="")):?>
)
<?php endif ?>
<br />
<hr /></td>
</tr>
<?php endif?>
<?php endforeach; ?>
</table>
<?php endif?>


Two permutations of this approach is to show only the current years articles as default and to list 'sort by date'
archives by year instead of month and year.

A) To show only the current year's articles as default:
1)
Search for <?php // insert block 1 (Form) for displaying only current year's articles here ?> above and insert the
following code:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="hidden" name="submitted" value="0">
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left"><INPUT TYPE="submit" VALUE="Clear Other Filters And Show All <?php
echo date("Y")?> Articles" style="width:92%; background-color:#7F0000;"></td>
</tr>
</table>
</FORM>
<br />


2)
Search for <?php // beginning of replaced code block 2 for displaying only current year's articles ?> and <?php // end
of replaced code block 2 for displaying only current year's articles ?> above and replace that code with the code below:

<?php // begin show this year's articles only code ?>
<?php if(@$submitted == '' || @$submitted == "0"):?>
<br />
<h3>All <?php echo date("Y")?> Articles Only</h3>
<table align="center" width="100%">
<?php $curYearCount = '0' ?>
<?php foreach ($articles_by_ericaRecords as $record): ?>
<?php $pubYear1 = date("Y", strtotime($record['publication_date']))?>
<?php $curYear1 = date("Y")?>
<?php if ($pubYear1 == $curYear1):?>
<?php $curYearCount++ ; ?>
<?php endif ?>
<?php endforeach ?>
<?php foreach ($articles_by_ericaRecords as $record): ?>
<?php $pubYear = date("Y", strtotime($record['publication_date']) )?>
<?php $curYear = date("Y")?>
<?php if(($pubYear == $curYear) && $curYearCount >= 1 ):?>
<tr>
<td colspan="2"><span class="Medium-Text-Bold"><?php echo $record['article_title']
?></span><span class="Medium-Text">
<?php if (($record['publication'] && !$record['publication'] =="")
||(
$record['publication_date'] && !$record['publication_date'] =="") ||($record['article_type'] &&
!
$record['article_type'] =="")):?>
<br />
(
<?php endif ?>
<?php if ($record['publication'] && !$record['publication'] ==""):?>
<?php echo $record['publication:label'] ?> -
<?php endif ?>
<?php if ($record['publication_date'] && !$record['publication_date'] ==""):?>
<?php echo date("F Y ", strtotime($record['publication_date'])) ?> -
<?php endif ?>
<?php if ($record['article_type'] && !$record['article_type'] ==""):?>
<?php echo $record['article_type:label'] ?>
<?php endif?>
<?php if (($record['publication'] && !$record['publication'] =="")
||(
$record['publication_date'] && !$record['publication_date'] =="") ||($record['article_type'] &&
!
$record['article_type'] =="")):?>
)
<?php endif ?>
</span><br /></td>
</tr>
<tr>
<?php if ($record['article_url']): ?>
<?php if(!preg_match("/^https:\/\//i", $record['article_url'] )):?>
<?PHP if (!preg_match("/^http:\/\//i", $record['article_url'])) {
$record['article_url'] = "http://" . $record['article_url']; } ?>
<?php endif ?>
<?php endif ?>
<?php if ($record['image']):?>
<td style="padding-left:10px; padding-top:10px; text-align:left;" align="center" ><?php
$article_url = $record['article_url'] ?>
<?php foreach ($record['image'] as $index => $upload): ?>
<a target="_blank" href="<?php echo $record['article_url'] ?>"><img style="
padding-right:10px; border:hidden;" src="<?php echo $upload['thumbUrlPath2'] ?>" width="<?php echo
$upload['thumbWidth2'] ?>" height="<?php echo $upload['thumbHeight2'] ?>" alt="" /></a><br />
<?php if($upload['info1'] ):?>
<span class="Small-Text">Photo by:<<br />
<?php echo htmlencode($upload['info1']) ?></span>
<?php endif ?>
<?php endforeach ?></td>
<?php else :?>
<td>&nbsp;</td>
<?php endif ?>
<td <?php if(!$record['image'] ):?>colspan="2" <?php endif?> valign="top"
class="Medium-Text"><?php echo $record['article_description'] ?><br />
<a target="_blank" href="<?php echo $record['article_url'] ?>"><?php echo
$record['article_link_text'] ?>... (read more)</a>
<?php $the_article = $record['article_url'] ?>
<br />
<?php foreach ($on_line_article_commentsRecords as $record): ?>
<?php if ($record['link_url'] == $the_article ) :?>
<br />
<?php if(!preg_match("/^https:\/\//i", $record['link_url'] )):?>
<?PHP if (!preg_match("/^http:\/\//i", $record['link_url'])) {
$record['link_url'] = "http://" . $record['link_url']; } ?>
<?php endif ?>
<a href="http://www.ericaminer.com/comments.php?match_url=<?php echo
$record['link_url'] ?>"><?php echo $common_informationRecord['comments_link_text'] ?></a>
<?php break ?>
<?php endif ?>
<?php endforeach ?>
</td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<?php endif ?>
<?php endforeach; ?>
</table>

<?php endif?>
<?php if (@$curYearCount == 0 && (@$submitted == '' || @$submitted == "0") ): ?>
<h3>So far, there are no articles published for <?php echo ($curYear) ?>.<br /> <br />
Please use the pulldown above to select <?php echo ($curYear-1) ?> and earlier
articles.</h3><?php endif ?>
<?php // end show this year's articles only code?>


B) To modify the 'Search by Date' archive to display by year instead of month and date you'll need to remove all
references to 'month' in the code above. (There are 4 )
Search for the 4 occurrences of remove this code for years only: in the code above and remove the indicated code.
You can also change any notes to reflect your changes.



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service