Re: Elegance is the goal... Sticky form submit help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jason Pruim wrote:
Hi Tamara,



On Sep 11, 2010, at 12:39 PM, Tamara Temple wrote:

Rather than repeating all that code, I suggest the following:


<select name="type" id="type>
  <option value="0">-- select type --</option>
<option value="meeting" <?php echo (isset($_POST['hidSubmit'] && $_POST['hidSubmit'] == TRUE && $_POST['type'] == "meeting") ? "selected" : '' ?> <option value="event" <?php echo (isset($_POST['hidSubmit'] && $_POST['hidSubmit'] == TRUE && $_POST['type'] == "event") ? "selected" : '' ?>
</select>


That's actually what I'm trying to get away from. I was hoping to do it all in HEREDOC syntax. I've always thought it made it cleaner. But that is a personal opinion :)

For a month selector, try:

<?php
  // assume current month number is in $curr_month
$months = array(1 => "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); // will create an array of month names, with a starting base of 1 (instead of zero)

 echo "<select name=\"month\" id=\"month\">\n";
 foreach ($months as $m => $mname) {
   echo "<option value=\"$m\"";
   if ($curr_month == $m) echo " selected ";
   echo ">$mname</option>\n";
 }
 echo "</select>\n";
?>

There are other possiblities as well. One time, I didn't want to actually store the month names, perhaps allowing them to be localized. Instead I used strftime, which will return appropriate names based on locale:

<?php
 // assume current month number is in $curr_month

 echo "<select name=\"month\" id=\"month\">\n";
 for ($m = 1; $m <= 12; $m++) {
   echo "<option value=\"$m\"";
   if ($curr_month == $m) echo " selected ";
   echo ">";
$mname = strftime("%B", 0,0,0,2010, $m, 1); // time, year and day don't matter, all we're after is the appropriate month name set in the locale
   echo $mname;
   echo "</option>\n";
 }
 echo "</select>\n";
?>

I'm actually doing something similar to that right now...

<?PHP
if (isset($_POST)) {
$date = date("n", $i);
echo $date;
$i++;
}
?>

Prints just the numeric reference to the month.




Here is a combination of Tamara's method and they way that I would do it based off her example. Some of hers didn't work for me out of the box, so I modified it to my liking. Then I included your request to do HEREDOC syntax for outputting the list.

<?php

$o = array();
for ($m = 1; $m <= 12; $m++) {
  $sel = ($curr_month == $m ? ' selected="selected"':'');
  $mname = date('F', mktime(0,0,0,$m));

  $o[] = '        <option value="'.(int)$m.'"'.$sel.'>'.
         htmlspecialchars($mname).'</option>';
}
$select_month_options = join("\n", $o);

echo <<<HTML
        <select name="month" id="month">
{$select_month_options}
        </select>
HTML;

?>

Jim

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux