从选项中选择多个选项

时间:2015-05-13 13:53:46

标签: php html mysql forms mysqli

所以我有一个从数据库中获取信息的选择器。

当我从选择器中选择某个内容并按:Add to list时,它会生成一个包含所选信息的表格。

what it looks like after i selected one

现在这应该做什么。但现在,当我选择其他结果并按Add to list时。它删除旧的并用新的替换它 what it looks like when i add a new one

但我实际上不希望它删除旧的,但在它下面新建一行。这样表变大了。我该怎么做?

选择器代码:

<!--Selector-->
<?php
    //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 to list'/>" . "</td>";
        echo "</form>";
    if(isset($_POST["train_name"])){

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

        //Make table headers
        ?>
        <div id="train_select_table">
        <form name="selector" method="post" action="customer_list.php?user_id=<?php echo $_SESSION['user_id']?>">
            <table>
                <tr>
                    <th>Train name</th>
                    <th>Number of bogies</th>
                    <th>Number of axles</th>
                    <th>Delete</th>
                    <th>More info</th>
                    <th>Check</th>
                <!--Only for admins (later!)-->
                <!--<th>XML</th>
                    <th>SQL</th>    -->
                </tr>
            <div id="looprow">
                <?php      
                    foreach($results as $res){
                    //Loop trough results, generate a tablerow every time
                ?>
                <tr>     
                    <td name="train_name"><?php echo $res['train_name']?></td>
                    <td><?php echo $res['number_of_bogies']?></td>
                    <td><?php echo $res['number_of_axles']?></td>
                    <td><a href="remove_from_table.php?train_id=<?php echo $res['train_id']?>">Delete</a></td>
                    <td><a href="expand_info.php?train_id=<?php echo $res['train_id']?>">More Information</a></td> 
                    <td><input type="checkbox" name="checkbox[]" value="<?php echo $res['train_id']?>"></td>
                                                <!--Only for admins (later!)--> 
                    <!--<td><a href="convert_to_xml.php?train_id=<?php echo $res['train_id']?>">XML</a></td>
                    <td><a href="convert_to_sql.php?train_id=<?php echo $res['train_id']?>">SQL</a></td>-->
                </tr>
            <?php
                }
            ?>
            </div>
        </table><br />
        <input name="Add to list" type="submit" id="add_to_list" value="add_to_list">
    </form>
    </div>
    <?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_name, train_id FROM train_information";
        $sth = $this->pdo->prepare($sql);   
        $sth->execute();
        return $sth->fetchAll();
    }

我理解为什么它一直在替换旧行。这是因为我发送了一个新查询。但我不知道如何保留旧的。

2 个答案:

答案 0 :(得分:0)

因为每次用户选择单个值而不是多个值时,所以如果您也想使用新值和旧值,则应使用cookiesession存储旧值或以该形式隐藏的输入。这取决于你。

E.g。在开始时,这样做:

<?php
session_start();//make it be before your output if you want to use session or cookie
if(!$_SESSION['train_name']) $_SESSION['train_name'] = array();
if(isset($_POST["train_name"])) $_SESSION['train_name'] = array_merge ($_SESSION['train_name'], $_POST["train_name"]);
.......
//next step, inquiry with $_SESSION['train_name']

答案 1 :(得分:0)

只需将多个选择列表的名称从train_name更改为train_name[],如下所示:

...
echo "<select name='train_name[]' id='train_name' multiple='multiple'>";
...

通过这种方式,您的$_POST['train_name']将成为传递给bindParam

的数组