Foreach循环表来自DB

时间:2015-03-23 11:27:46

标签: php html mysql phpmyadmin

Render.php

<?php
class render {
    private $pdo;

function makeSelector($database){
    //Get name and id data from the db. In an assoc array
    $results = $database->Selector();
    echo "<form name='form' method='POST' id='selector'>";
    echo "<select name='train_name' id='train_name' multiple='multiple'>";
    // Loop trough the results and make an option of every train_name
    foreach($results as $res){
                echo "<option value=" . $res['train_name'] . ">" . $res['train_name'] . "</option>";
        }  
    echo "</select>";
    echo "<br />" . "<td>" . "<input type='submit' name='Add' value='Add'/>" . "</td>";
    echo "</form>";
    }


function makeDetailTable($database){
    //Get all data from database, in an assoc array
    $results = $database->getAllAssoc();

    //Make table headers
    ?>
    <div id="train_select_table">
    <table>
        <tr>
            <th>Train name</th>
            <th>Tare weight</th>
            <th>Number of bogies</th>
            <th>Number of axles</th>
            <th>min Wheel diameter</th>
            <th>Max wheel diameter</th>
            <th>Delete</th>
            <th>XML</th>
            <th>SQL</th>
        </tr>
    <div id="loopRow">
        <?php      
            foreach($results as $res){
            //Loop trough results, generate a tablerow every time
        ?>
        <tr>        
            <?php 
            echo "<td>" . $res['train_name'] . "</td>";
            echo "<td>" . $res['tare_weight'] . "</td>";
            echo "<td>" . $res['number_of_bogies'] . "</td>";
            echo "<td>" . $res['number_of_axles'] . "</td>";
            echo "<td>" . $res['wheel_diameter_min'] . "</td>";
            echo "<td>" . $res['wheel_diameter_max'] . "</td>";
            ?>
            <td> <a href='remove_from_table.php?trainID= <?= $res["train_id"] ?>' > Delete </a></td>
            <td> <a href='convert_to_xml.php?trainID= <?= $res["train_id"] ?>' > XML </a></td>
            <td> <a href='convert_to_sql.php?trainID= <?= $res["train_id"] ?>' > SQL </a></td>
        </tr>
    <?php
        }
    ?>
    </div>
    </table>
    </div>
    <?php
}


}
?>

Selector.php

Center <br /><br /><br />
<!--Selector-->
<?php

    $render->makeSelector($database);
    if(isset($_POST["train_name"])){

    $render->makeDetailTable($database);
}
?>

功能(db_login.php)

function getAllAssoc() {
        $sql = "SELECT * FROM train_information WHERE train_name = :train_name";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(":train_name", $_POST["train_name"]);
        $sth->execute();
        return $sth->fetchAll();
     }

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

现在,这一切都有效。

当我在我的页面上时,我看到了这一点:

Start of the page

然后,当我选择一列火车并按下“添加”按钮时,它会显示我:

After i hit the Add button

这样才有用。 但现在,当我想选择第二列火车时,按下添加。它取代了已经在表中的这个。但我希望它添加另一行而不删除第一行。像这样:

What i want to happend

另外,当我回到index.php时(这个页面打开但是selector.php在那里是一个require)并刷新页面。它再次删除了整个表格。 如何使表格保持不变,直到我喜欢关闭应用程序(站点)

此外,在选择器中,您会看到名称:trein met spaties。 (用空间训练)。当我选择表格没有显示任何东西时。 (普罗比因为这个名字包含空格..)我如何解决这个问题?

0 个答案:

没有答案