0

I have a custom page resources-search.php template which handles search results for the search form. And in my function.php I have the following array:

$YearList = array('2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013');

Yes, You guessed it right. Its an array of year. In my resources-search.php I' am using this code:

<select name="year" id="opt_year">
<option value="0" selected="true" disabled="true">Year</option>
<?php
foreach($YearList as $key => $value){
echo '<option value="'.$value.'">'.$value.'</option>'; //close your tags!!
}
 ?>
</select>

Its a drop-down menu from the array above. This method doesn't actually works for me. In other pages it does work but not in resources-search.php.

Any ideas anyone?

1
  • Ever tried to use range()? How about range( 2000, date("Y") );? ;)
    – kaiser
    Commented Jul 27, 2013 at 0:22

2 Answers 2

0

You need to declare the variable as global in resources-search.php:

<?php

global $YearList;

// Rest of file...

May I ask why you're hardcoding the years, instead of...

$YearList = range( 2000, 2013 );
$YearList = range( 2000, date( 'Y' ) ); // From 2000 to current year
0

Just because you define the variable in functions.php doesn't mean you can access it in resources-search.php. You instead need to put your array as part of the $GLOBALS variable and then you can access it anywhere:

In functions.php

$GLOBALS['year_list'] = array('2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013');

In resources-search.php

foreach( $GLOBALS['year_list'] as $key => $value ) {
    echo '<option value="'.$value.'">'.$value.'</option>'; //close your tags!!
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.