USING A PULL DOWN FORM FIELD TO SHOW AVAILABLE DATES - Jun 7th, 2014
|
Showing available dates issue got a bit more complicated when I wanted to use a pull down form field instead of a list to show the available date options.
I've made this a separate example to keep the code as simple as possible.
For this example you’ll need to create a multi-record section with at least a Date field called ‘publication_date’ and a text field called ‘title’ and a checkbox field called ‘hidden’.
Note: change ‘your_table_name’ to the name of the table that you’ve created (in 2 places). Put a viewer_functions.php call at the top of both viewers, and a list records call for your table at the top of your available_date_test2.php viewer.
With a bit of help from Chris Waddell from Interactive Tools, I came up with the following code for my available_date_test.php viewer (the one with the form):
<?php // get list of unique months and years with records $query = "SELECT DATE_FORMAT(publication_date, '%M %Y') as dateAndYear, YEAR(publication_date) as year, MONTH(publication_date) as month FROM cms_your_table_name 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="available_date_test2.php"> <select name="dateAndYear" style="width:350px;" > <option value="">Choose A Date</option> <?php while ($record = mysql_fetch_assoc($result)):?> <option class="body-text" value="<?php echo $record['dateAndYear'] ?>"><?php echo $record['dateAndYear']; ?></option><?php endwhile ?></select> <input type="submit" name="submit" value="Go To Records" style="width:200px; "></form>
And this code for the available_date_test2.php page that show records filtered by the date chosen:
<?php @list($date_month, $date_year) = explode(' ', @$_REQUEST['dateAndYear']); // create the variables $date_month and $date_year split on space ?> <?php $date = @$date_year."-".@$date_month."-28";
// According to Jason Sauchuk - This creates a "date" variable based on the 2 variables in the string (we use 28 for the "day" since all months will have at least 28 days. This number isn't important, but it helps php decipher which variables are months, days, and years).;
$formattedDate = date("F Y", strtotime($date));
// This formats that date into the form Month Year. You can then output $formattedDate where ever you want to. ?> Records with a date field of <?php echo $formattedDate ?> <?php foreach ($your_tableRecords as $record): ?> <?php $record_year = date("Y", strtotime($record['publication_date'])) // format the $record_year variable to show only the 4 digit year ?> <?php $record_month = date("F", strtotime($record['publication_date'])) // format the $record_month variable to show the full name of the month ?> <?php if (($date_year == $record_year) && ($date_month == $record_month) ):?> <?php echo $record['title'] ?><hr> <?php endif ?> <?php endforeach; ?>
|
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.