...

View Full Version : problem using PHP to create select menu



chump2877
05-05-2006, 09:35 AM
Alright, I;ve been staring at this forever and I can;t seem to find the problem.

I've got a GET variable called $_GET['mailbox']. this variable is either equal to 'Inbox', 'Sent_Items', 'Drafts', or 'Deleted_Items'.

I have this code for a select box:


<select name="mailbox" class="mbox" onchange="listThem()">
<option>--Select a Mailbox--</option>

<?

$get_var = trim($_GET['mailbox']);
print selectMailbox('Inbox',$get_var);
print selectMailbox('Sent_Items',$get_var);
print selectMailbox('Drafts',$get_var);
print selectMailbox('Deleted_Items',$get_var);

?>

</select>

And this function that builds the <option> tag content:

// Select Mailbox function


function selectMailbox($mailbox, $get_var)
{
$output .= '<option value="' . $mailbox . '" ';

if ($mailbox == 'Inbox')
{
if (empty($get_var) && empty($_SESSION['mailbox']))
{
$output .= 'selected="selected"';
}
}
elseif ($get_var == $mailbox)
{
unset($_SESSION['mailbox']);
$_SESSION['mailbox'] = $get_var;
$output .= 'selected="selected"';
}
elseif ($_SESSION['mailbox'] == $mailbox)
{
$output .= 'selected="selected"';
}

$output .= '>' . str_replace("_"," ",$mailbox) . '</option>';

return $output;
}

The problem is, when $_GET['mailbox'] = 'Inbox' and $mailbox = 'Inbox', the code inside this conditional:


elseif ($get_var == $mailbox)
{
unset($_SESSION['mailbox']);
$_SESSION['mailbox'] = $get_var;
$output .= 'selected="selected"';
}

is NOT executed. Inbox is NOT made the selected option in the select menu for some reason.

Also, when $_GET['mailbox'] = 'Inbox', the relevant source code looks like this:


<select name="mailbox" class="mbox" onchange="listThem()">
<option>--Select a Mailbox--</option>

<option value="Inbox" >Inbox</option><option value="Sent_Items" >Sent Items</option><option value="Drafts" >Drafts</option><option value="Deleted_Items" >Deleted Items</option>
</select>

You can see that the option with value 'Inbox' is not selected as it should be.

Also, I've echoed out the variables, and everything checks out OK.

Can anyone see the problem? thanks.

mio
05-05-2006, 09:52 AM
hi,



The problem is, when $_GET['mailbox'] = 'Inbox' and $mailbox = 'Inbox', the code inside this conditional:

PHP Code:
elseif ($get_var == $mailbox)
{
unset($_SESSION['mailbox']);
$_SESSION['mailbox'] = $get_var;
$output .= 'selected="selected"';
}

is NOT executed. Inbox is NOT made the selected option in the select menu for some reason.



...for the reason that just before you do something that begin this way:



if ($mailbox == 'Inbox')


and then use the conditional instruction else if....How do you want it do go in this ?!!

need further explanation ? ;)

chump2877
05-05-2006, 10:37 AM
Doh, I think you called it....I shouldn;t be nesting my if statements...this fixed it:


// Select Mailbox function

function selectMailbox($mailbox, $get_var)
{
$output .= '<option value="' . $mailbox . '" ';

if ($mailbox == 'Inbox' && empty($get_var) && empty($_SESSION['mailbox']))
{
$output .= 'selected="selected"';
}
elseif ($get_var == $mailbox)
{
unset($_SESSION['mailbox']);
$_SESSION['mailbox'] = $get_var;
$output .= 'selected="selected"';
}
elseif ($_SESSION['mailbox'] == $mailbox)
{
$output .= 'selected="selected"';
}

$output .= '>' . str_replace("_"," ",$mailbox) . '</option>';

return $output;
}

thanks mio :)

mio
05-05-2006, 11:28 AM
happy you get it fixed :)

juste one little little little thing,

when you have a few "different" conditionnal statements using different var, isolate them as if you worked with a switch statement i.e the conditionnal statement is conditionned by the same var(s).
E.g.:
you first declare


if ($mailbox == 'Inbox')...

then


elseif ($get_var == $mailbox)

More "logically" you should have wrote:


if ($mailbox == 'Inbox')...

then


elseif ($mailbox == $get_var)

assuming that the structure of your conditionnal statement is the global unit suit of if and else if working around the same var here $mailbox.

or you should have declared a new conditionnal statement only declaring


if ($get_var == $mailbox)...

(and not else if)

that was just a little trick to make the code easier to understand :thumbsup:

Mio



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum