Combining option values before form submission

6 posts by 2 authors in: Forums > CMS Builder
Last Post: May 21, 2013   (RSS)

By gkornbluth - May 15, 2013 - edited: May 16, 2013

Hi All,

I may be missing the obvious, but here goes...

BACKGROUND
My search results require that a _keyword search of the field “attended” be submitted in the format mm-yy.

I’m using the code below to create a drop down of month options (January thru December) and a drop down of year options (2000 through the current year).

The code produces option values for months in the format mm, and for years in the format -yy.

QUESTION
How can I combine the search criteria into the correct format (mm-yy) before submission to search for a “_keyword” in the field “attended” ?


<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<?php
$lowestYear = 2000;
$highestYear = date("Y");
?>
Month:
<select name="month">
<?php foreach(range(1,12) as $month): ?>

<option value="<?php echo date("m",strtotime("0000-$month"));?>"><?php echo date("F",strtotime("0000-$month"));?></option>
<?php endforeach ?>
</select>
Year:
<select name="year">
<?php foreach (range($highestYear,$lowestYear) as $year):?>
<option value="<?php $two_digit_year = substr($year, -2);?>-<?php echo $two_digit_year;?>"><?php echo $year;?></option>
<?php endforeach?>
</select>
<input type="submit" name="submit" value="Search">
</form>

Thanks,

Jerry Kornbluth

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 gregThomas - May 16, 2013

Hi Jerry,

What about doing something like this:

  if(@$_REQUEST['month'] && @$_REQUEST['year']){
    $_REQUEST['attended_keyword'] = $_REQUEST['month'].'-'.$_REQUEST['year'];
  }

So if both the year and month fields have been submitted, they are combined into a request array called attended_keyword. You need to make sure that your getRecords function appears after this code, and that search is set to true. I also made a couple of changes to your form as well:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
  <?php
  $lowestYear = 2000;
  $highestYear = date("Y");
  ?>
  Month:
  <select name="month">
    <?php foreach(range(1,12) as $month): ?>
      <?php $timeValue = strtotime("2012-$month"); ?>
      <option value="<?php echo date("m",$timeValue);?>"><?php echo date("F",$timeValue);?></option>
    <?php endforeach ?>
  </select>
  Year:
  <select name="year">
    <?php foreach (range($highestYear,$lowestYear) as $year):?>
      <option value="<?php echo substr($year, -2);?>" ><?php echo $year;?></option>
    <?php endforeach?>
  </select>
  <input type="submit" name="submit" value="Search">
</form>

Before there was a '-' at the beginning of the year value which I've removed. I've also changed the month foreach loop so that it only creates the strtotime value once. 

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gkornbluth - May 16, 2013

Thanks Greg,

I'll give it a go in the morning.

Jerry Kornbluth

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 - May 17, 2013

Hi Greg,

The attached, based on your suggestion works perfectly, but I need to be able to show the alpha values in the “salons to display” options instead of the original record number values.

Let me explain further.

My “salon_name” field is a multi value check box list field that gets it’s option values from the “num” field and it’s label values from the “title” field in a database called “salon_names”.

At present, I’m creating an array of all unique values in the salon_names field, and using those values to determine my search criteria. The searches work, however they display the numerical values in the “salons to display” list, and I can’t seem to get them to display the alpha values.

I tried mucking about with this numToName  function that I used in another application, but couldn’t figure out how to modify it to work here.
 

<?php   
$numToName = array();
foreach ($master_exhibition_listRecords as $record){
  $numToName[$record['num']] = $record['title'];
}
?>


I’ve attached the current search form so you can see exactly what’s going on.

Thanks,

Jerry Kornbluth

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
Attachments:

salons-stripped.php 7K

By gregThomas - May 21, 2013

Hi Jerry,

Sorry for the delay in reply. I've had a look through your code, and I think the example coe you gave in your last post was correct, but you've got a slight error in the code in salons-stripped.php

$selectArray = array(); 
//foreach section item
foreach($salon_listingsRecords as $record){ 
  //If the record has something selected for the "salon_name" category
  if(@$record['salon_name']){    //If the selected value isn't already in the array
    if(!in_array(@$record['salon_name'],$selectArray)){ 
      //Add it to the $selectArray
      $selectArray[$record['num']] = $record['salon_name']; 
    }
  } 
}

I think the issue is that you are storing the salon_name value in both the key and the value of the selectArray array, when it should just have stored the num value in the key. I've highlighted the change I've made above green.

Let me know if this doesn't work.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com