dqgg25493 2013-05-12 06:38
浏览 19
已采纳

与PHP和mysql数据库相关的下拉列表

I am new in PHP. I have a database like the attached in the dropbox file. It has 4 column

  1. Division Name
  2. District Name
  3. Thana Name
  4. Union name

These are the administrative unit of our country. I want to create four dropdown list/combobox those will populate from the database. One thing is must to related each other. If someone select Division name it will show only the district name those are belongs to that Division. Similarly if the district selected it will only show Thana name in the 3rd dropdown list. Similarly the 4th dropdown will be filled up.

Database Schema:

https://dl.dropboxusercontent.com/u/81313785/database_schema.xls

Here is the code I used:

  <?php
$con = pg_connect("data connection");
if (!$con)
  {
  die("Could not connect: " . pg_last_error());
  }

  $locfindsql = "SELECT unionname, thananame, distname, divname from union_bgd";

//echo $sql;
$result = pg_query($con, $locfindsql);

$row = array();
?>
<div id="leftpanel" style="position: absolute; top: 12%; left: 1%; bottom: 2%; width: 20%; height: 86%; box-shadow: 4px 4px 4px #888888;
         background: #5e5e5c; z-index:-888;">

    <div id="findPlace" style= "position: absolute; left: 8px; top: 8px; right: 8px; height: 275px; background: rgb(245, 246, 246); border-radius: 4px;" >
        <h4>Find a location</h4>
        Division:</br>
        <select name="Division">
            <?
            while($row = pg_fetch_array($result))
              {
              echo "<option>$row[divname]</option>";
              }
            ?>
        </select></br>
        District:</br>
        <select name="District">
            <?
            while($row = pg_fetch_array($result))
              {
              echo "<option>$row[distname]</option>";
              }
            ?>
        </select></br>
        Upazila:</br>
        <select name="Union">
            <?
            while($row = pg_fetch_array($result))
              {
              echo "<option>$row[thananame]</option>";
              }
            ?>
        </select></br>
        Union:</br>
        <select name="Union">
            <?
            while($row = pg_fetch_array($result))
              {
              echo "<option>$row[unionname]</option>";
              }
            ?>
        </select></br>
            <?
            pg_close($con);
            ?>

        <button id="placebutton" style="font-size: small; font-family: Arial;"><a href="javascript:onPlaceBtnClick('findPlaceButton')">Find the Place</a></button>
    </div>

    </div>
  • 写回答

2条回答 默认 最新

  • doutui839638 2013-05-12 07:01
    关注

    I'm not sure if this is the kind of thing you're talking about but I hope it will be of help to you. This is code populates a drop down list from the database. This isn't production code but it shows one way to feed the database results into the form. If you're totally new to PHP and want to learn the basics quickly, you can try w3schools.com and see if it's helpful for you. There's also documentation at php.net where you can dive into the depths of PHP.

        // GET LIST AND DISPLAY IN FORM
        $link = mysqli_connect($db_connection, $db_user, $db_passwd, $db_name);
        // check connection
        if (mysqli_connect_errno()) 
        {
          printf("Connect failed: %s
    ", mysqli_connect_error());
          exit();
        }
        // else echo "<p>Connected to database.</p>";
        // Get value for new student id 
        $query = "SELECT custid, firstname, lastname FROM customer ORDER BY lastname, firstname ASC";
        if($result = mysqli_query($link, $query)) 
        {
            echo '<p><form action="./index.php" method="post">';
            //
            //SELECT THE CUSTOMER MAKING THE PAYMENT
            echo'<select name="loadcustomer">';
            echo'<option value="" selected="selected" disabled="disabled">Select a Customer</option>';
            while ($idresult = mysqli_fetch_row($result))
            {
                $custid = $idresult[0];
                $firstname = $idresult[1];
                $lastname = $idresult[2];
    
                echo'<option value="' . $custid . '">' . $firstname . '&nbsp;' . $lastname . '</option>';
            }
            echo'</select></p><p>';
            //
            // SELECT THE NUMBER OF HOURS TO BE PAID
            echo'<select name="numberofhours">';
            echo'<option value="" selected="selected" disabled="disabled">Select # of Hours</option>';
            echo'<option value="1">1</option>';
            echo'<option value="2">2</option>';
            echo'<option value="3">3</option>';
            echo'<option value="4">4</option>';
            echo'<option value="5">5</option>';
            echo'<option value="6">6</option>';
            echo'<option value="7">7</option>';
            echo'<option value="8">8</option>';
            echo'</select></p><p>';
            echo'<input type="submit" value="Load Customer" />';
            echo'</form></p>';  
        }
        mysqli_free_result($result);
        // close connection 
        mysqli_close($link);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能