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 7 of 7
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation using functions to populate listboxes, need some advice please

    Hi All

    I have the following function which populates list boxes with a list of names
    PHP Code:
    function playerSelect()
    {
        
    $sql "SELECT staffName, staffID, staffType FROM tbl_staff WHERE staffType = 'player' ORDER BY staffName ASC";
        
    $rs mysql_query($sql);
        
        for(
    $i 0$i mysql_num_rows$rs ); $i++)
        {
            
    $tmp mysql_fetch_row$rs );
            print(
    "<OPTION value=\"$tmp[0]:$tmp[1]\">$tmp[0]</OPTION>\n");
        }

    a couple of questions if i may.

    1/ how do i have the default value set to "Please Select" instead of the first name in the db?

    2/ what does $tmp[0] and $tmp[1] do in the following code? <OPTION value=\"$tmp[0]:$tmp[1]\">

    3/ is it possible to remove selections from the list once they have been selected so that once the function is run again the name that has already been selected in no longer available to select again?

    many thanks
    Luke

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,912
    Thanks
    15
    Thanked 227 Times in 227 Posts
    I'll answer you first question, just make a single line of code that prints that option before the for loop runs.

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    cool thank you!!!!!

    im guessing with the code below if i were to submit the form the values returned from the list boxes will be something like players ID and players Name which is what $tmp[0] and $tmp[1] are? instead of the actual value selected? have i got that right?

    PHP Code:
    print("<OPTION value=\"$tmp[0]:$tmp[1]\">$tmp[0]</OPTION>\n"); 

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,912
    Thanks
    15
    Thanked 227 Times in 227 Posts
    Just look at the source code for the generated page to see what they are. I don't see any quotes with dots around the variables in question, so I don't know how they will be interpreted by the parser.
    PHP Code:
    print("<OPTION value=\"".$tmp[0].":".$tmp[1]."\">".$tmp[0]."</OPTION>\n"); 
    That may be what you need instead.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ah thats a good idea never thought of that lol cheers mate

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    3/ is it possible to remove selections from the list once they have been selected so that once the function is run again the name that has already been selected in no longer available to select again?

    You would add an if statement and compare the selection with a stored value. You could use cookies, sessions or a DB to hold the stored value.


    This is a quick example, there probably is syntax errors. But it should give you an idea of what to do.
    PHP Code:
    for($i 0$i mysql_num_rows$rs ); $i++) {
    $tmp mysql_fetch_row$rs );
    if (
    $stored_value != $tmp[0]:$tmp[1]) {
    print(
    "<OPTION value=\"$tmp[0]:$tmp[1]\">$tmp[0]</OPTION>\n");
    }



    ----
    Last edited by Len Whistler; 08-17-2012 at 04:39 PM.
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    LJackson (08-19-2012)

  • #7
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,912
    Thanks
    15
    Thanked 227 Times in 227 Posts
    In addition to what Len Whistler has said you can use javascript to remove an option from a drop down, but you have to pass the code to it, and remenber it the next time the page loads.

  • Users who have thanked DrDOS for this post:

    LJackson (08-19-2012)


  •  

    Posting Permissions

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