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 Chris - June 5, 2014

Hi Jerry,

You'll want to add a "name" attribute to that <select>, and you'll want to pass in both the month and the year to the "value" of the <option>s, like this:

<?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="dateAndYear" style="width:350px;" >
<option value="">Choose An Archive</option>
<?php while ($record = mysql_fetch_assoc($result)):?>
<option value="<?php echo $record['dateAndYear'] ?>"><?php echo $record['dateAndYear']; ?></option><?php endwhile ?></select>
<input type="submit" name="submit" value="Go To Archive" style="width:200px; "></form>

...And then on your archive list page, you can extract the month and year from $_REQUEST['dateAndYear']:

<?php
@list($month, $year) = explode(' ', @$_REQUEST['dateAndYear']); // split on space
...
?>

Does that help?

All the best,
Chris

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