【发布时间】:2015-07-24 15:23:16
【问题描述】:
我有一个从我的数据库中获取数据的选择器。
当我选择一项并按下Add to list时,它会生成一个表格:
这一切的代码是:
<!--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();
}
当我检查检查按钮时,然后按 Add to list 。你会去customer_list.php。在此页面上,我想显示所选项目的信息。我现在拥有的是:
<?php
echo $_POST["checkbox"];
?>
这向我显示了一个数字(当我选择了一个项目时),它是火车/项目的 ID。
但是如何显示选中复选框的所有信息呢?
此外,如果我在表中有多列火车,例如 10 列。我只选择 7 列并按下按钮。我希望下一页 customer_list.php 显示这 7 个特定结果。
【问题讨论】:
-
使用你的逻辑。处理你在 customer_list.php 中得到的值。
-
但是怎么做呢?因为我得到了正确的选择值。这是一个ID。但是当我喜欢时:
并将其发布在下一页上,就像选择一样。它不会发布任何内容
标签: php html mysql forms mysqli