Reputation: 77
I am working on redesigning a somewhat poorly designed system and have run across the following issue. I have a time field where the user can select the hour, minute, and time zone for an event. These are done with an HTML select drop down menu with several different options. My predecessor felt it necessary to enable people to select an exact minute whereas in my particular application 15 minute increments will suffice, (not to mention make the list about a million times easier to navigate through for the users).
The issue I have is this, if an event was created to occur at say 1:17 PM previously but 17 is no longer in my minute drop down menu It will display the default --select-- option. But I would prefer that in these cases the 17 appear in the menu and be selected as the default. My issue is I don't know how exactly to check if my minute value already exists in the list, and then add it if it isn't. I suppose I could be creating a PHP array in parallel with my drop down options, but I would prefer to be able to look directly at the list of options and determine if the value is already there, if so move on, if not add it to the end. Is there a way to get this kind of information from an HTML object in PHP?
Upvotes: 0
Views: 1611
Reputation: 174997
PHP runs during the HTML renders, if you wish to alter a <select>
with PHP, that rendering must be done before the list is presented. Your best solution is to create an array and add your custom value in there.
define('DEFAULT_SELECT', array(0, 15, 30, 45));
...
$select_options = DEFAULT_SELECT;
if (!in_array($new_option, $select_options)) { array_push($new_option, $select_options)); }
Upvotes: 0
Reputation: 19745
Why don't you just create the entire object (select/option) and remove it from the webpage you have? You'd still have the HTML page. You just don't put the HTML for the select/option ahead of time. You build it in the PHP and put it on the page.
You use the PHP to decide the values that need to be in Option.
Upvotes: 0
Reputation: 47776
No, short of parsing your HTML using a PHP parser and get the values, there is no good way of knowing that beforehand. The best solution for that would be to have an array with the values in your dropdown and check with that array.
Alternatively, I would suggest selecting the nearest of the new possible values to the user's previous choice. You could get it like that:
round($minute, 15) * 15; //14 will give 15, 35 will give 30, etc
Upvotes: 0
Reputation: 34224
No you can't access this information from PHP. Only thing you could do is to buffer the html you are sending and then use DOM to access the drop down. But I suggest you use a php array, just as you mentioned, and create the drop down from it.
$values = array(0,15,30,45);
if(!in_array($values, $minute)) {
$values[] = $minute;
}
// Generate select box..
[...]
Upvotes: 1
Reputation: 4187
You could use modulus (%) to work out if the value is divisible by 15 (assuming you are using the minutes 0, 15, 30 and 45 in the new list) e.g.
if($value !== 0 && $value % 15 !== 0) { // add it to the list }
Upvotes: 0