Results 1 to 4 of 4

Thread: Function fill dropdown

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Location
    San Isidro
    Posts
    326

    Function fill dropdown

    How can i make a function out of this code

    where all colored red variable is dynamic...meaning i can change the variable
    according to dept table, branch table

    Code:
    $query4="SELECT * FROM tblbranch";
    $result4 = mysql_query ($query4);
    echo "<select name=Branch id=textbox value=''>Branch</option>";
    // printing the list box select command
    while($nt=mysql_fetch_array($result4)){//Array or records stored in $nt
    echo '<option '.($nt['branchcode'] == $bra ? ' selected ' : '').' value="'.$nt['branchcode'].'" >'.$nt['branchcode'].'-'.$nt['branchdesc'].'</option>'; 
    }
    echo "</select>";// Closing of list box */
    ?>
    I want to learn more
    grace

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Function fill dropdown

    So you want this to be able to change dynamically or load once when the page loads?

    Either way, assuming that you don't need to worry about people manually modifying the branch number, you could have a php page that serves this up and grabs the branch number from the querystring.

    If you need to have it change dynamically, then you would want to look into AJAX. A good starter tutorial can be found here

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Location
    San Isidro
    Posts
    326

    Re: Function fill dropdown

    Quote Originally Posted by kfcSmitty View Post
    So you want this to be able to change dynamically or load once when the page loads?

    Either way, assuming that you don't need to worry about people manually modifying the branch number, you could have a php page that serves this up and grabs the branch number from the querystring.

    If you need to have it change dynamically, then you would want to look into AJAX. A good starter tutorial can be found here

    no what i mean i am making a function out of that code...i created somtthing like this

    Code:
    function filldropdown($tbname,$drpname,$rowid,$rowname,$rowsel){
    $query4="SELECT * FROM $tbname";
    $result4 = mysql_query ($query4);
    				
    			
    echo "<select name=$drpname id=textbox value=''>$drpname";
    // printing the list box select command
    while($nt=mysql_fetch_array($result4)){//Array or records stored in $nt
                   
    echo '<option '.($nt['$rowid'] == $rowsel ? 'selected':'').' value="'.$nt['$rowid'].'" >'.$nt['$rowid'].'-'.$nt['$rowname'].'</option>'; 
    
    }
    if i do a function like the code above...it doesn't select a value, colored red is the code for selecting value
    I want to learn more
    grace

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Function fill dropdown

    if you have any variables encased in single quotes, PHP will take those variables as a literal string rather than processing them. eg.
    PHP Code:
    $hello "Greetings!";
    echo 
    '$hello'//echos: $hello
    echo "$hello"//echos: Greetings!
    echo $hello//echos: Greetings! 
    so, every time you reference $nt['$rowid'], PHP is looking for the key (LITERAL STRING) $rowid, rather than the actual value of $rowid. reference $nt[$rowid] instead. likewise for $nt['$rowname']; use $nt[$rowname].

Posting Permissions

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



Click Here to Expand Forum to Full Width