Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-05-2006, 08:35 AM   PM User | #1
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,530
Thanks: 15
Thanked 128 Times in 121 Posts
chump2877 is on a distinguished road
problem using PHP to create select menu

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:

PHP Code:
<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

PHP Code:
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:

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.

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

Code:
<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.
__________________
Regards, R.J.
chump2877 is online now   Reply With Quote
Old 05-05-2006, 08:52 AM   PM User | #2
mio
New Coder

 
Join Date: May 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
mio is an unknown quantity at this point
hi,

Quote:
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:

PHP Code:
if ($mailbox == 'Inbox'
and then use the conditional instruction else if....How do you want it do go in this ?!!

need further explanation ?
mio is offline   Reply With Quote
Old 05-05-2006, 09:37 AM   PM User | #3
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,530
Thanks: 15
Thanked 128 Times in 121 Posts
chump2877 is on a distinguished road
Doh, I think you called it....I shouldn;t be nesting my if statements...this fixed it:

PHP Code:
// 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
__________________
Regards, R.J.
chump2877 is online now   Reply With Quote
Old 05-05-2006, 10:28 AM   PM User | #4
mio
New Coder

 
Join Date: May 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
mio is an unknown quantity at this point
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
PHP Code:
if ($mailbox == 'Inbox')... 
then
PHP Code:
elseif ($get_var == $mailbox
More "logically" you should have wrote:
PHP Code:
if ($mailbox == 'Inbox')... 
then
PHP Code:
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
PHP Code:
if ($get_var == $mailbox)... 
(and not else if)

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

Mio
mio is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:45 AM.


Advertisement
Log in to turn off these ads.