gpt4 book ai didi

php - 表格传递选择到下一页

转载 作者:行者123 更新时间:2023-11-29 22:26:45 25 4
gpt4 key购买 nike

我有一个从我的数据库获取数据的选择器。

The selector

当我选择 1 个项目并按添加到列表时,它会生成一个表格:

When I selected something

这一切的代码是:

    <!--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
}

?>

Functions:

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();
}

当我选中复选按钮,然后按添加到列表时。您将转到 customer_list.php 。在此页面上我想显示所选项目的信息。我现在拥有的是:

<?php
echo $_POST["checkbox"];
?>

这会显示一个数字(当我选择一个项目时),它是火车/项目的 ID。

但是如何显示所选复选框的所有信息呢?另外,如果表中有多趟列车,例如 10 趟。我只选择 7 趟并按下按钮。我希望下一页 customer_list.php 显示这些特定的 7 个结果。

最佳答案

有两件事:

1.更改

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

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

2.改变

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 getAllAssoc() {
$sql = "SELECT * FROM train_information WHERE train_name IN(:train_name)";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(":train_name", implode(",",$_POST["train_name"]));
$sth->execute();
return $sth->fetchAll();
}

只需调整查询中的引号即可,因为我没有测试过这段代码,感觉单引号会有问题。

上面所做的是,我们将输入放入数组中,并根据该数组输入相应地获取结果。

希望你能明白!!

关于php - 表格传递选择到下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30213135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com