Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,827
    Thanks
    19
    Thanked 157 Times in 148 Posts

    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.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #2
    mio
    mio is offline
    New Coder
    Join Date
    May 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    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 ?

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,827
    Thanks
    19
    Thanked 157 Times in 148 Posts
    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.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #4
    mio
    mio is offline
    New Coder
    Join Date
    May 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •