2015-06-09 65 views
1

我有一個選擇器可以從我的數據庫中選擇所有數據。在會話中保存選擇並在表中顯示

<?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) 

和「train_selector_result.php頁面:

<?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列火車,然後按提交:

What it looks

當我添加1以上,它看起來像這樣:

What it looks when i add 1 more

正如你可以看到,該表被重置了。雖然我真的希望它增加1個額外的列!

+0

加上'action'屬性='selector'>'標籤,所以它會進入它應該賦予會話值的頁面。 – alexandreferris

+0

但我不想那樣。因爲選擇器在主頁上,我想在主頁上看到結果。 – Mitch

+0

好吧,我想我明白了。你有沒有在你的主頁的地方檢查用戶是否已經檢查/點擊了一個「選擇器」,因此它增加了「SESSION」? – alexandreferris

回答

1

正如我們在聊天討論:

train_selector_result.php頁變化在`<形式名稱= '形式' 方法= 'POST' id來此

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 
} 
+1

非常感謝您抽出時間幫助我! – Mitch

+1

我們隨時爲您服務! :d – alexandreferris

相關問題