问题描述
我有一个选择器,可以从我的数据库获取数据。
当我选择1项并按添加到列表
时,它会生成一个表:
这一切的代码是:
<! - Selector-- >
//从数据库获取名称和ID数据。在assoc数组中
$ results = $ database-> Selector();
echo< form name ='form'method ='POST'id ='selector'>;
echo< select name ='train_name'id ='train_name'multiple ='multiple'>;
//循环结果并为每个train_name提供一个选项
foreach($ results as $ res){
echo< option value = 。 $ res ['train_name']。 > 中。 $ res ['train_name']。 < /选项> 中;
}
echo< / select>;
回声< br /> 。 < TD> 中。 < input type ='submit'name ='Add'value ='添加到列表'/> 。 < / TD> 中;
回声< / form>;
if(isset($ _ POST [train_name])){
//从关联数组中获取数据库中的所有数据
$ results = $ database-> getAllAssoc();
//创建表头
?>
< div id =train_select_table>
< form name =selectormethod =postaction =customer_list.php?user_id =<?php
echo $ _SESSION ['user_id']?>>
< table>
< tr>
列车名称< / th>
转向架的数量< / th>
轴的数量< / th>
< th>删除< / th>
< th>更多资讯< / th>
< th>检查< / th>
<! - < th> XML< / th>
< th> SQL< / th> - >
< / tr>
< div id =looprow>
<?php
foreach($ results as $ res){
//循环低谷结果,每次生成一个表格
?> ;
< 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']?>>删除< / a>< TD>
< td>< a href =expand_info.php?train_id =<?php
echo $ res ['train_id']?>>更多信息< / a>< / TD>
< td>< input type =checkboxname =checkboxvalue =<?php
echo $ res ['train_id']?>>< / td> ;
<! - < td>< a href =convert_to_xml.php?train_id =<?php
echo $ res ['train_id']?>> XML<一个>< / 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 listtype =submitid =add_to_listvalue =add_to_list>
< / form>
< / div>
<?php
}
?>
函数:
函数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 select(){
$ 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个结果。
< td>< input type =checkboxname = checkbox []value =<?php echo $ res ['train_id']?>>< / td>
customer_list.php代码:
<$ p $
<?php
function getAllAssoc_id($ id){
$ sql =SELECT * FROM train_information WHERE id =:id;
$ sth = $ this-> pdo-> prepare($ sql);
$ sth-> bindParam(:id,$ id);
$ sth-> execute();
return $ sth-> fetchAll();
}
// echo $ _POST [checkbox];
echo< table>< tr>< td> Train Name< / td>< td> Train Id< / td>< / tr>;
foreach($ _ POST [checkbox] as $ key => $ val){
$ data = getAllAssoc_id($ val);
echo< tr>< td>。$ data ['train_name']。< / td>< td>。$ data ['id']。< / td> < / TR>中;
}
echo< / table;
?>
I have a selector wich gets data from my Database.
When i select 1 item and press Add to list
, it generates a table:
the code for this all is:
<!--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();
}
When i Check the check button, and press Add to list
. You will go to customer_list.php
. On this page i want to show the information of the selected item. What i have right now is:
<?php
echo $_POST["checkbox"];
?>
This shows me a number (when i selected a item) wich is the ID of the train/item.
But how do i show all the information of the selected checkbox?Also if i have multiple trains in the table, for example 10. And i only select 7 and press the button. i want the next page customer_list.php
to show those specific 7 results.
You need to edit checkbox code as :
<td><input type="checkbox" name="checkbox[]" value="<?php echo $res['train_id']?>"></td>
customer_list.php code:
<?php
function getAllAssoc_id($id) {
$sql = "SELECT * FROM train_information WHERE id = :id";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(":id", $id);
$sth->execute();
return $sth->fetchAll();
}
//echo $_POST["checkbox"];
echo"<table><tr><td>Train Name</td><td>Train Id</td></tr>";
foreach($_POST["checkbox"] as $key=>$val){
$data=getAllAssoc_id($val);
echo"<tr><td>".$data['train_name']."</td><td>".$data['id']."</td></tr>";
}
echo"</table";
?>
这篇关于表格传递选择到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!