471,596 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

Stumped with select function

I have a function that creates a select list:

function make_select_ns($name,$array,$persist){
$string = "<select name=\"$name\">";
$string .="<option selected>" . ucfirst($name) . "</option>\n";
if ($name == $array[0]){array_shift($array);} //removes duplicate when
table name equals 1st item in array
foreach ($array AS $value) {
if (($value == $persist) && is_string($persist)){$selected =
"selected";}
else {$selected = "";}
$string .= "<option $selected value='" .
str_pad($value,2,"0",STR_PAD_LEFT) . "'>$value</option>\n";
}
$string.="</select>";
print $string;
}

I have arrays for Months, Days, Years, Hours, and Minutes.

All of the arrays work as expected except for the minutes array which
starts at 0 and goes to 55 in increments of 5. Whenever this loads
after a post the value 0 is selected. What am I missing here? Why
does the zero seem to be causing me problems?

Let me know if you need more information.

Thanks,

-Mark

Aug 30 '05 #1
1 1303


supline wrote:
I have a function that creates a select list:

function make_select_ns($name,$array,$persist){
$string = "<select name=\"$name\">";
$string .="<option selected>" . ucfirst($name) . "</option>\n";
if ($name == $array[0]){array_shift($array);} //removes duplicate when
table name equals 1st item in array
foreach ($array AS $value) {
if (($value == $persist) && is_string($persist)){$selected =
"selected";}
else {$selected = "";}
$string .= "<option $selected value='" .
str_pad($value,2,"0",STR_PAD_LEFT) . "'>$value</option>\n";
}
$string.="</select>";
print $string;
}

I have arrays for Months, Days, Years, Hours, and Minutes.

All of the arrays work as expected except for the minutes array which
starts at 0 and goes to 55 in increments of 5. Whenever this loads
after a post the value 0 is selected. What am I missing here? Why
does the zero seem to be causing me problems?

Let me know if you need more information.


Alternative solution (with PEAR):

$f = new HTML_QuickForm('life_is_simple');

// assuming $array contains our selections and $selected is
// an array of selected values
$array = array_unique($array); // to remove duplicates
$my_select =& $f->addElement('select', 'my_name', 'Select me:', $array);
$my_select->setSelected($selected)
$f->addElement('submit',null,'Submit');
// or some othe way of displaying it
$f->display();

// end

//Marcin
Aug 30 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Phil Powell | last post: by
1 post views Thread by PBR | last post: by
2 posts views Thread by awilliam | last post: by
4 posts views Thread by J | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.