hi

smarty ( and pear ? ) provide some interresant function
http://smarty.php.net/manual/en/language.function.html.options.php


on my own scripts i have somthing like :
<?php
/**
* Boolean function.
*
* Compare first element with second and return the third.
* If the return argument is an array, first element of the array is TRUE, and
second FALSE.
*
* @param  string  $a       First element.
* @param  string  $b       Second element.
* @param  mixed   $return  Mixed return input.
*/
// if my function is so long, it's cause i use it for lot of
// other comparaisons like bool(md5($_POST['passwd']),$encrypted_passwd) or
die;
// it should be more performant without some if-then-else
function bool($a,$b,$return=FALSE){
  if ( ($return===FALSE) || ($return===0) )   $r=array(FALSE,TRUE);
  elseif ( ($return===TRUE) || ($return===1) )$r=array('0','1');
  elseif ($return=='s')                       $r=array(NULL,' selected');
  elseif (($return) && (!is_array($return)))  $r=array(NULL,$return);
  elseif (is_array($return))                  $r=$return;
  else trigger_error ("Wrong parameter for bool()", E_USER_WARNING);
  if ($a==$b)
    return $r[1];
  else
    return $r[0];
}
?>
exemple :
   <select name=foo>
    <option value=a<?=bool($_POST['foo'],'a','s');?>>a</option>
    <option value=b<?=bool($_POST['foo'],'b','s');?>>b</option>
    <option value=c<?=bool($_POST['foo'],'c','s');?>>c</option>
   </select>
or :
<select name=foo>
<?php foreach($a_select as $value) {
printf("<option
value=\"%1\$s\"%2\$s>%1\$s</option>\n",$value,bool($_POST['foo'],$value,'s'));
} ?>

( sorry for the long code )



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

Reply via email to