选项从DB获取数据

时间:2015-03-11 08:28:54

标签: php html mysql sql

我的数据库中有这个表:

**train_information**

train_id
country_id
user_id
train_name
tare_weight
number_of_bogies
number_of_axles
wheel_diameter_min
wheel_diameter_max

然后我有2个.php页面(1只用于课程)

Selector.php

<!--Selector-->
<form name='form' id='selector'>
    <?php
        $selector = $database->selector();
    ?>
        <select onchange= "mySelection()" id ="selector" name="selector">
        <option selected="true" disabled="disabled">Selecteer een trein</option>
    <?php 
        foreach ($selector as $selector) {
            print "<h3>" . $selector['train_id'] . "==" . $selector_id . "</h3>";
            if ($selector['train_id'] == $selector_id) {
                echo "<option value=" . $selector['train_id'] . " selected='selected'> " . $selector['train_name'] . "</option>";
            } else {
                echo "<option value=" . $selector['train_id'] . "> " . $selector['train_name'] . "</option>";
            } }     
        ?>
    </select>
</form>

然后是class.php

function selector() {
        $sql = "SELECT train_id, train_name FROM train_information";
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        return $sth->fetchAll();
    }

选择器显示选择中的列车名称。这样很好。 但是当我选择它时我想要它,并按下如下按钮:选择。它选择所选列车的信息并在页面上显示。 而且我不想只显示1,但如果我想在此之后选择其他列车,则会在页面上显示两列列车信息。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作

将此方法添加到class.php

function select_train_details($trainId) {
         $sql = "SELECT * FROM train_information where train_id = " . $trainId;
         $sth = $this->pdo->prepare($sql);
         $sth->execute();
         return $sth->fetchAll();
     }

将以下内容添加到selector.php

 <form name='form' id='selector'>
     <?php
     $selector = $database->selector();
     if (isset($_POST['selector'])) {
         $selectorDetail = $database->select_train_details();
     }
     ?>
     <select onchange= "this.form.submit" id ="selector" name="selector">
         <option selected="true" disabled="disabled">Selecteer een trein</option>
         <?php
         foreach ($selector as $selector) {
             print "<h3>" . $selector['train_id'] . "==" . $selector_id . "</h3>";
             if ($selector['train_id'] == $selector_id) {
                 echo "<option value=" . $selector['train_id'] . " selected='selected'> " . $selector['train_name'] . "</option>";
             } else {
                 echo "<option value=" . $selector['train_id'] . "> " . $selector['train_name'] . "</option>";
             }
         }
         ?>
     </select> </form> <?php if (isset($selectorDetail)) {
     echo "<pre>";
     $selectorDetail; } ?>