SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist cturner01's Avatar
    Join Date
    Jun 2006
    Location
    My computer
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Populate drop down box?

    Can someone please tell me how I can populate a drop down box from a database and add data from a database into a text box after someone has selected a database entry? Thanks in advance.

  2. #2
    SitePoint Enthusiast Bare Koncept's Avatar
    Join Date
    May 2006
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thats a big job, well, here (in the body):
    I didn't test it, but it should work fine

    Code:
    <?php
    //Begin DB info
    $dbhost = ""; //Database host (localhost, maybe, if its hosted on the same server as your files)
    $dbuser = "";//Username
    $dbpass = "";//Password
    $dbname = "";//Database name
    $table = "";//Table name
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to database.');
    mysql_select_db($dbname);
    //End DB info
    $sqlq = "SELECT * FROM ".$table;
    $result = mysql_query($sqlq);
    echo '<select id="sbox" onchange="document.getElementById(\'datahere\').value=this.value;">';
    //Assuming you want everything, else put in conditionals
    while($row=mysql_fetch_array($result)) {
    //Change parameters based on collumn names
    echo '<option value="'.$row['somecollumn'].'">'.$row['somecollumn'].'</option>';
    }
    echo "</select>"
    //Close database connection
    mysql_close($conn);
    echo '<input type="text" id="datahere" />';
    ?>

  3. #3
    SitePoint Evangelist cturner01's Avatar
    Join Date
    Jun 2006
    Location
    My computer
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried your code and it works fine but what about adding data to multiple drop down boxes and adding data into a text box?

  4. #4
    SitePoint Enthusiast Bare Koncept's Avatar
    Join Date
    May 2006
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't quite know what you mean. Like adding to different drop down boxes based on parameters in the database?

  5. #5
    SitePoint Evangelist cturner01's Avatar
    Join Date
    Jun 2006
    Location
    My computer
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes that is what I mean Bare Koncept.

  6. #6
    SitePoint Enthusiast Bare Koncept's Avatar
    Join Date
    May 2006
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just duplicate the following code found in the original, add it below the original, and add some conditionals where it says "Assuming you want everything, else put in conditionals". For example, if you wanted only database entries in the second one where $row['somecollumn'] was "hat" you would add:

    PHP Code:
    $sqlq "SELECT * FROM ".$table;
    $result mysql_query($sqlq);
    echo 
    '<select id="sbox2" onchange="document.getElementById(\'datahere\').value=this.value;">';
    //Assuming you want everything, else put in conditionals
    while($row=mysql_fetch_array($result)) {
    if (
    $row['somecollumn'] == "hat"){
    //Change parameters based on collumn names
    echo '<option value="'.$row['somecollumn'].'">'.$row['somecollumn'].'</option>';
    }
    }
    echo 
    "</select>" 
    It would then all read:
    PHP Code:
    <?php
    //Begin DB info
    $dbhost ""//Database host (localhost, maybe, if its hosted on the same server as your files)
    $dbuser "";//Username
    $dbpass "";//Password
    $dbname "";//Database name
    $table "";//Table name
    $conn mysql_connect($dbhost$dbuser$dbpass) or die('Error connecting to database.');
    mysql_select_db($dbname);
    //End DB info
    $sqlq "SELECT * FROM ".$table;
    $result mysql_query($sqlq);
    echo 
    '<select id="sbox" onchange="document.getElementById(\'datahere\').value=this.value;">';
    //Assuming you want everything, else put in conditionals
    while($row=mysql_fetch_array($result)) {
    //Change parameters based on collumn names
    echo '<option value="'.$row['somecollumn'].'">'.$row['somecollumn'].'</option>';
    }
    echo 
    "</select>";
    $sqlq "SELECT * FROM ".$table;
    $result mysql_query($sqlq);
    echo 
    '<select id="sbox2" onchange="document.getElementById(\'datahere\').value=this.value;">';
    //Assuming you want everything, else put in conditionals
    while($row=mysql_fetch_array($result)) {
    if (
    $row['somecollumn'] == "hat"){
    //Change parameters based on collumn names
    echo '<option value="'.$row['somecollumn'].'">'.$row['somecollumn'].'</option>';
    }
    }
    echo 
    "</select>";
    //Close database connection
    mysql_close($conn);
    echo 
    '<input type="text" id="datahere" />';
    ?>

  7. #7
    SitePoint Evangelist cturner01's Avatar
    Join Date
    Jun 2006
    Location
    My computer
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried the second lot of code that you posted here. I have made some changes to the code and it is now duplicating the entries twice. Here is my code:
    PHP Code:
    $dbhost "localhost"//Database host (localhost, maybe, if its hosted on the same server as your files)
    $dbuser "username";//Username
    $dbpass "password";//Password
    $dbname "dbname";//Database name
    $table "table_name";//Table name
    $conn mysql_connect($dbhost$dbuser$dbpass) or die('Error connecting to database.');
    mysql_select_db($dbname);
    //End DB info
    $sqlq "SELECT * FROM ".$table;
    $result mysql_query($sqlq);
    echo 
    '<select id="sbox" onchange="document.getElementById(\'datahere\').value=this.value;">';
    //Assuming you want everything, else put in conditionals
    while($row=mysql_fetch_array($result)) {
    //Change parameters based on column names
    echo '<option value="'.$row['selectDay'].'">'.$row['selectDay'].'</option>';
    }
    echo 
    "</select>";

    $sqlq "SELECT * FROM ".$table;
    $result mysql_query($sqlq);
    echo 
    '<select id="sbox2" onchange="document.getElementById(\'datahere\').value=this.value;">';
    //Assuming you want everything, else put in conditionals
    while($row=mysql_fetch_array($result)) {
    //Change parameters based on column names
    echo '<option value="'.$row['selectMonth'].'">'.$row['selectMonth'].'</option>';
    }
    echo 
    "</select>";

    $sqlq "SELECT * FROM ".$table;
    $result mysql_query($sqlq);
    echo 
    '<select id="sbox2" onchange="document.getElementById(\'datahere\').value=this.value;">';
    //Assuming you want everything, else put in conditionals
    while($row=mysql_fetch_array($result)) {
    //Change parameters based on column names
    echo '<option value="'.$row['selectYear'].'">'.$row['selectYear'].'</option>';
    }
    echo 
    "</select>";
    echo 
    "<br />Diary Entry: ";

    $sqlq "SELECT * FROM ".$table;
    $result mysql_query($sqlq);
    while(
    $row=mysql_fetch_array($result)) {
    echo 
    '<textarea name="diary_entry" id="diary_entry" value="'.$row['diary_entry'].'"></textarea>';
    }
    //Close database connection
    mysql_close($conn); 

  8. #8
    SitePoint Enthusiast Bare Koncept's Avatar
    Join Date
    May 2006
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you missed my point: to narrow down what goes in the drop down, you must add conditionals.

    Take this piece of code found in every section:

    PHP Code:
    while($row=mysql_fetch_array($result)) { 
    //Change parameters based on column names 
    echo '<option value="'.$row['selectYear'].'">'.$row['selectYear'].'</option>'

    To narrow it down to only years > 1985:

    PHP Code:
    while($row=mysql_fetch_array($result)) { 
    if (
    $row['selectYear'] > 1985){
    //Change parameters based on column names 
    echo '<option value="'.$row['selectYear'].'">'.$row['selectYear'].'</option>'
    }



Bookmarks

Posting Permissions

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