我有一个选择器,可以从我的数据库中选择所有数据。
<?php
$results = $database->Selector();
?>
<form name='form' method='POST' id='selector'>
<select name='train_id[]' id='train_name' multiple='multiple'>
<?php
foreach($results as $train_details){ //Look through every result in DB
echo "<option value=" . $train_details['train_id'] . ">" . $train_details['train_name'] . "</option>";
}
?>
</select><br />
<td>
<input type='submit' name='Add' value='Add to list'/>
</td>
</form>
<?php
include 'train_selector_result.php'; //This will display the results (will show page later)
&#39; train_selector_result.php
&#39;页:
<?php
if(isset($_POST['train_id'])){
//Table stuff here (Won't place here because too long)
//And then the table results:
foreach ($_POST['train_id'] as $selectedOption){
$test = $database->testingSelector($selectedOption);
foreach($test as $lol){
?>
<tr>
<td name="train_name"><?= $lol['train_name']?></td>
<td><?= $lol['number_of_bogies']?></td>
<td><?= $lol['number_of_axles']?></td>
<td><a href="expand_info.php?train_id=<?= $lol['train_id']?>">More Information</a></td>
<td><a href="train_model.php?train_id=<?= $lol['train_id']?>">Model</a></td>
<td><input type="checkbox" name="checkbox[]" value="<?= $lol['train_id']?>"></td>
</tr>
<?php
}
}
}
功能:
function testingSelector($selectedOption){
$sql = "SELECT * FROM train_information WHERE train_id = :train_id";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(":train_id", $selectedOption, PDO::PARAM_STR);
$sth->execute();
return $sth->fetchAll();
}
function selector() {
$sql = "SELECT train_name, train_id FROM train_information ORDER BY train_name";
$sth = $this->pdo->prepare($sql);
$sth->execute();
return $sth->fetchAll();
}
这一切都运作良好。当我从选择器中选择多个项目并按下提交按钮时,它会显示我选择的项目。
但是,每当我添加另一个或进入主屏幕时,我都不希望它们消失。所以我想把它们放在一个会话中。
我试着把它们放在一个会话中,但它没有用。 (得到了无差错的索引错误)。 我是怎么做到的:
<?php
if(isset($_POST['train_id'])){
//Table stuff here (Won't place here because too long)
//And then the table results:
foreach ($_POST['train_id'] as $selectedOption){
$test = $database->testingSelector($selectedOption);
$_SESSION['selector'] = $test
foreach($_SESSION['selector'] as $lol){
?>
<tr>
<td name="train_name"><?= $lol['train_name']?></td>
<td><?= $lol['number_of_bogies']?></td>
<td><?= $lol['number_of_axles']?></td>
<td><a href="expand_info.php?train_id=<?= $lol['train_id']?>">More Information</a></td>
<td><a href="train_model.php?train_id=<?= $lol['train_id']?>">Model</a></td>
<td><input type="checkbox" name="checkbox[]" value="<?= $lol['train_id']?>"></td>
</tr>
<?php
}
}
}
然而这不起作用......(已经开始了一个会话,因为我已经登录了!)
修改
当我选择3列火车并按下提交时它的样子:
当我再添加1个时,它看起来像这样:
如您所见,表格被重置。虽然我实际上希望它添加1个额外的列!
答案 0 :(得分:1)
正如我们在聊天中所讨论的那样:
在您的train_selector_result.php
页面中更改为此
foreach ($_POST['train_id'] as $selectedOption){
//if has selected selector, then add to session
$test = $database->testingSelector($selectedOption);
$_SESSION["selector"][] = $test;
}
foreach ($_SESSION["selector"] as $result){
$lol = $result[0];
?>
<tr>
<td name="train_name"><?= $lol['train_name']?></td>
<td><?= $lol['number_of_bogies']?></td>
<td><?= $lol['number_of_axles']?></td>
<td><a href="expand_info.php?train_id=<?= $lol['train_id']?>">More Information</a></td>
<td><a href="train_model.php?train_id=<?= $lol['train_id']?>">Model</a></td>
<td><input type="checkbox" name="checkbox[]" value="<?= $lol['train_id']?>"></td>
</tr>
<?php
}