Using Options List Populated From MYSQL Query to Filter Article Records By Month and Year

3 posts by 2 authors in: Forums > CMS Builder
Last Post: June 5, 2014   (RSS)

By gkornbluth - May 24, 2014

Hi All,

I’m stumped again, and again I’m sure that it’s something silly and obvious that I’m missing, but here goes...

I’ve been successfully using the concept in post #2193447 to filter article records by Month and Year.

For a site that I’m working on, I need to use an options list to present the existing articles, but can’t get the code to pass even a single value (month) to the filtered page.

I also don’t know how to pass the 2 values (month and year).

Here’s the code that works (much the same as the example in post #2193447) followed by the code that doesn’t...

Thanks for looking...

Jerry Kornbluth

<?php
// get list of unique months and years with articles
$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 GROUP BY dateAndYear ORDER BY date";
$result = mysql_query($query) or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n");
while ($record = mysql_fetch_assoc($result)):
?>
Select An Archive
<br>
<a href="articlestest2.php?date_year=<?php echo $record['year'] ?>&date_month=<?php echo $record['month'] ?>"><?php echo $record['dateAndYear']; ?> </a>
<?php endwhile ?>



Note: The code below doesn’t pass the variable or value for “date_year” to the filtered page. “date_month=<?php echo $record['month'] ?>” will also need to be added as a second variable.

<?php
// get list of unique months and years with articles
$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="articlestest2.php">
<select name="" style="width:350px;" >
<option value="">Choose An Archive</option>
 <?php while ($record = mysql_fetch_assoc($result)):?>
<Option value="date_year=<?php echo $record['year'] ?>"><?php echo $record['dateAndYear']; ?></option><?php endwhile ?></select>
<input type="submit" name="submit" value="Go To Archive"  style="width:200px; "></form>

And the filtered page code

<?php $date_year = $_REQUEST['date_year']; ?>
<?php $date_month = $_REQUEST['date_month']; ?>
<?php
$date = @$_REQUEST['date_year']."-".@$_REQUEST['date_month']."-28";
$formattedDate = date("F Y", strtotime($date));
?>
Articles For <?php echo $formattedDate ?>
<br>
<table align="center" width="100%">
<?php foreach ($articles_by_ericaRecords as $record): ?>
<?php $record_year = date("Y", strtotime($record['publication_date'])) ?>
<?php $record_month = date("n", strtotime($record['publication_date'])) ?>

<?php if (($date_year == $record_year) && ($date_month == $record_month) ):?>
<tr>
<td>
Filtered Article fields
</td>
</tr>
<?php endif ?>
<?php endforeach; ?>
</table>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - June 5, 2014

Hi Chris,

It certainly does help...

Couldn't have done it without you (Again...)

Thank you

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php