Page 1 of 3 123 LastLast
Results 1 to 10 of 23
  1. #1

    Default PHP: dropdown values from a Database


    mga PHP gurus. pwede ko mag ask ug simple PHP codes in which ang dropdown sa form kay values from a database. kana lang simple na code para dali ra nako masabtan. ayaw lang na complicated kay beginner pa ko. then if naay mga comments mas maayo para mas sayon nako pag sabot sa codes ninyo.

    thnx daan mga PHP gurus.

  2. #2
    Quote Originally Posted by winly_ang_batang_bronsy View Post
    mga PHP gurus. pwede ko mag ask ug simple PHP codes in which ang dropdown sa form kay values from a database. kana lang simple na code para dali ra nako masabtan. ayaw lang na complicated kay beginner pa ko. then if naay mga comments mas maayo para mas sayon nako pag sabot sa codes ninyo.

    thnx daan mga PHP gurus.
    OPEN THIS LINK:
    PHP: mysql_fetch_assoc - Manual

    same as usual.. u have to use those syntax to access to the database. Within the loop just "concatenate" <option> tags inside. :P
    You can understand the codes how the concept goes. You don't need spoon feeding for it
    Last edited by dodie; 01-08-2011 at 02:35 PM.

  3. #3
    Adding from what dodie suggested, one straight forward solution would be to extract the data from DB and display them using a loop. Let's assume that you have the values from the DB stored in an array, then using a looping statement echo each item in the array.

    Code:
    <select name="fromDB">
    <?php
    // lets assume that $arrayfromDB has the all the values.
    
    foreach($arrayfromDB as $item){
      echo "<option>".$item."</option>";
    }
    ?>
    </select>

  4. #4
    Quote Originally Posted by ChaosOrb View Post
    Adding from what dodie suggested, one straight forward solution would be to extract the data from DB and display them using a loop. Let's assume that you have the values from the DB stored in an array, then using a looping statement echo each item in the array.

    Code:
    <select name="fromDB">
    <?php
    // lets assume that $arrayfromDB has the all the values.
    
    foreach($arrayfromDB as $item){
      echo "<option>".$item."</option>";
    }
    ?>
    </select>
    apir choi!!!

    modified code for ChaosOrb
    Code:
    foreach($arrayfromDB as $item){
      echo '<option value="'.addslashes($item).'">'.$item.'</option>';
    }
    ang container quotes replaced into sinqle quotes.... aron dili na maaglibog2x sa quotes usage, specially in values attribute. hehehe :3

  5. #5
    Quote Originally Posted by dodie View Post
    apir choi!!!

    modified code for ChaosOrb
    Code:
    foreach($arrayfromDB as $item){
      echo '<option value="'.addslashes($item).'">'.$item.'</option>';
    }
    ang container quotes replaced into sinqle quotes.... aron dili na maaglibog2x sa quotes usage, specially in values attribute. hehehe :3
    modify nato gamay, kini pwede maka select ug specific option thru $_REQUEST

    PHP Code:
    foreach($arrayfromDB as $item){
      echo 
    '<option value="'.addslashes($item).'"'.($_REQUEST['key'] == addslashes($item) ? ' selected="selected"' '').'>'.$item.'</option>';

    if libug na, ignore nalang ni...

  6. #6
    thanks sa mga replies ninyong tanan...

  7. #7
    hi guys i need your help. please ko usab ani nga code kay mag error. thanks daan guys.

    <?php
    //Connect to MySQL:
    $db_connection = mysql_connect ("localhost","root","");

    //Select the database
    mysql_select_db("tutorial");

    //I have the following SQL statement and execution:
    $get_list = "SELECT first_name FROM employee";
    $get_list_result = mysql_query($get_list) or die (mysql_error());
    ?>
    //Then I want to display the value from database in the drop down list with the code below:
    <html>
    <body>
    <td><select name="sel_id">
    <?php
    while ($recs = mysql_fetch_array($get_list_result)){
    $id = $recs['AREA_ID'];
    $display_list = $recs['RESP_AREA'];
    <option value="$id">echo '$display_list';</option>
    }// end while
    ?>
    </select>
    </td>
    </body>
    </html>

  8. #8
    i think nag error ni cya kay tungod sa imo Select, ang iya ra i return is ang "firstname" nya nag kuha ka og "AREA_ID" and "RESP_AREA"

    try changing it to *

  9. #9
    mag error man japon sis/bro. or pwede paki butang daw sample code na naay usa ka dropdown then ang value niya is from a column on a database.

    let's just say:

    column name is country

    so sa dropdown it would be list of country which is sulod sa column country.

    from start til end daw na code sis then paki put comments lang para easier for me to understand...


    thanks.

  10. #10
    Quote Originally Posted by winly_ang_batang_bronsy View Post
    hi guys i need your help. please ko usab ani nga code kay mag error. thanks daan guys.

    <?php
    //Connect to MySQL:
    $db_connection = mysql_connect ("localhost","root","");

    //Select the database
    mysql_select_db("tutorial");

    //I have the following SQL statement and execution:
    $get_list = "SELECT first_name FROM employee";
    $get_list_result = mysql_query($get_list) or die (mysql_error());
    ?>
    //Then I want to display the value from database in the drop down list with the code below:
    <html>
    <body>
    <td><select name="sel_id">
    <?php
    while ($recs = mysql_fetch_array($get_list_result)){
    $id = $recs['AREA_ID'];
    $display_list = $recs['RESP_AREA'];
    <option value="$id">echo '$display_list';</option>
    }// end while
    ?>
    </select>
    </td>
    </body>
    </html>
    - murag diri ka nasangit...

    <option value="$id">echo '$display_list';</option>
    - try daw ug..

    echo '<option value="'.$id.'">'.$display_list.'</option>';

    unya, kadto imu sql statement, ibutang ang * as suggested...


  11.    Advertisement

Page 1 of 3 123 LastLast

Similar Threads

 
  1. vb 6 to database date from to from table how?
    By ventura in forum Programming
    Replies: 5
    Last Post: 11-23-2007, 02:40 PM
  2. HOW TO ADD RECORD to database from ASP using jscript
    By hybrid_X in forum Programming
    Replies: 4
    Last Post: 05-22-2007, 04:56 PM
  3. JAVASCRIPT: how to object value from window.open()
    By hybrid_X in forum Programming
    Replies: 0
    Last Post: 04-09-2007, 01:45 PM
  4. Replies: 1
    Last Post: 10-04-2006, 09:18 AM
  5. What can beat this all-in-one cellphone from BIRD@ only 12k Php?
    By ftray in forum Gizmos & Gadgets (Old)
    Replies: 4
    Last Post: 11-07-2005, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top