0

我是 PHP 新手。我有一个类似于 Dropbox 文件中附件的数据库。它有 4 列

  1. 部门名称
  2. 区名
  3. 塔纳名字
  4. 工会名称

这些是我国的行政单位。我想创建四个将从数据库中填充的下拉列表/组合框。一件事是必须相互关联。如果有人选择部门名称,它将仅显示属于该部门的地区名称。同样,如果选择的地区将仅在第三个下拉列表中显示 Thana 名称。同样,第 4 个下拉列表将被填满。

数据库架构:

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

这是我使用的代码:

  <?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>
4

2 回答 2

2

我不确定这是否是你所说的那种事情,但我希望它会对你有所帮助。这是从数据库中填充下拉列表的代码。这不是生产代码,但它显示了一种将数据库结果输入表单的方法。如果您对 PHP 完全陌生并且想快速学习基础知识,您可以尝试 w3schools.com 看看它是否对您有帮助。php.net 上还有文档,您可以在其中深入了解 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\n", 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);
于 2013-05-12T07:01:10.193 回答
1

如果我理解正确,以下代码将正常工作,只需正确设置 mysql

        <?php
    $con=mysqli_connect("your host","mysql_user","user_password","your_db");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $result = mysqli_query($con,"SELECT destrict, thana FROM your table");
    ?>
    <select>
    <?
    while($row = mysqli_fetch_array($result))
      {
      echo "<option>$row[destrict]</option>";

      }
    ?>
    </select>
    <?
    mysqli_close($con);
    ?>
于 2013-05-12T06:47:35.970 回答