Welcome Guest, Not a member yet? Register   Sign In
Problem with empty variable and 0
#1

[eluser]Total Shop UK[/eluser]
Code:
function minutes($name,$extra='',$selected=''){
    $select=array();
    for ($i=0; $i<=59; $i++) {
        $select[$i] = $i;
    }
    $html = '<select name="'.$name.'" id="'.$name.'"'.$extra.'><option value="">- Minutes -</option>';
    foreach ($select as $key => $value) {
        if ($selected==$key){
            $html.= '<option value="'.$key.'" SELECTED>'.$value.'</option>';
        }else{
            $html.= '<option value="'.$key.'">'.$value.'</option>';
        }
    }
    $html.= '</select>';
    return $html;
}

I have 0 as the default but I am trying to have '' as my default unless something is selected??

Any help would be appreciated, thanks.
#2

[eluser]mddd[/eluser]
The line saying
Code:
if ($selected==$key){
will be true for $selected = 0 because 0 == ''.
You can check for type by using
Code:
$selected===$key
. This only gives 'true' when the variables are of the same type!
Another solution could be to check if $selected contains a number:
Code:
if (is_numeric($selected) && $selected==$key)
.

By the way, why are you making an array first, and then only looping through that array? The function could be written shorter:
Code:
function minutes($name,$extra='',$selected='')
{
    $html = '<select name="'.$name.'" id="'.$name.'"'.$extra.'><option value="">- Minutes -</option>';
    for ($i=0; $i<60; $i++)
    {
        $html.= '<option value="'.$i.'"';
        if ($selected === $i) $html .= ' selected';
        $html.= '>'.$i.'</option>';
    }
    $html.= '</select>';
    return $html;
}
#3

[eluser]rogierb[/eluser]
I'm not realy getting what it is what you want but:
Code:
function minutes($name,$extra=null,$selected=null){
    $select=array();
    for ($i=0; $i<=59; $i++) {
        $select[$i] = $i;
    }
    $html = '<select name="'.$name.'" id="'.$name.'"'.$extra.'><option value=""'.(is_null($selected)?'SELECTED':'').'>- Minutes -</option>';
    foreach ($select as $key => $value) {
        if (!is_null($selected) && $selected==$key){
            $html.= '<option value="'.$key.'" SELECTED>'.$value.'</option>';
        }else{
            $html.= '<option value="'.$key.'">'.$value.'</option>';
        }
    }
    $html.= '</select>';
    return $html;
}

I think you want something like this.


Edit: @mddd, your solution is much shorter:-) I forgot all about the ===.
#4

[eluser]Total Shop UK[/eluser]
Many thanks for your quick replies,
The quick fix of checking 'if a number' worked perfect

Code:
if (is_numeric($selected) && $selected==$key)

Edit: I was just copying the code down from other selects which don't contain the for loop but thanks for the shorter version I'll use that.
#5

[eluser]mddd[/eluser]
Glad it works. I thought about it and "is_numeric" is probably the best solution because it is possible to get information from a form, and that information will be encoded as string. So you could get $_POST['select_value'] with a value of "1" (which is a string!) and that would not match the number 1 when using ===.
So, for robustness the "is_numeric" is the best I would say, as is_numeric(1) and is_numeric("1") will both give the correct result.




Theme © iAndrew 2016 - Forum software by © MyBB