Minggu, 17 Oktober 2010

PHP: Dinamyc list of a combobox

Database requiered to fill the combobox list with dynamic data. we need simply steps. The steps are listed below:

1.  Create a table, you can use this script:
     CREATE TABLE `komputerbelajar`.`mahasiswa` (
     `NIM` VARCHAR( 25 ) NOT NULL ,
     `NAMA` VARCHAR( 25 ) NOT NULL ,
     `ALAMAT` VARCHAR( 25 ) NOT NULL ,
     PRIMARY KEY ( `NIM` )
     ) ENGINE = MYISAM
2.  Fill the table:
     INSERT INTO `komputerbelajar`.`mahasiswa` (
     `NIM` ,
     `NAMA` ,
     `ALAMAT`
     )
     VALUES (
     '550022551', 'Budi', 'Semarang'
     ), (
     '550022552', 'Najib', 'Surabaya'
     ), (
     '550022553', 'Ahmad', 'Bandung'
     ), (
     '550022554', 'Slamet', 'Jakarta'
     ), (
     '550022555', 'Sanjaya', 'Semarang'
     );

3.  Download connection file (php to mysql) in here or view it in here.

4. Create a php code like below:

     <?php

     include 'class.mysql.php';

     $mysql = new Mysql();
     $mysql->connect();

     if($mysql->execute("select nim, nama from mahasiswa")){
           $data = $mysql->getDataSet();
     }else{
           $data = 0;
     }

     ?>

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
     <title>Combo-Box</title>
     </head>

     <body>
     <select name="listmenu">
     <?php
     for($i=0;$i<count($data);$i++){
     ?>
          <option value="<?=$data[$i][0]?>"><?=$data[$i][1]?> </option>
     <?php
     }
     ?>
     </select>
     </body>
     </html>

5. Finish you can download in here and try it.... thanks.

Tidak ada komentar:

Posting Komentar