save selection in session and show in table

241 views Asked by At

I have a selector that selects all the data from my database.

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

And the 'train_selector_result.php' page:

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

The functions:

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

This is all working well. When i select multiple items from the selector and press the Submit button, it will show me those selected items.

However, i don't want them to disappear whenever i add an other one or go to the home screen. So i would like to put them in a session.

I tryed to put them in a session but it did not work. (Got undifind Index Errors). How i did it:

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

However this did not work... (Already started a session, because i'm logged in!)

EDIT:

What it looks when i select 3 trains and press submit:

What it looks

And when i add 1 more, it looks like this:

What it looks when i add 1 more

As you can see, the table gets resetted. While i actually want it to add 1 extra column!

1

There are 1 answers

2
alexandreferris On BEST ANSWER

As we discussed in chat:

in your train_selector_result.php page change to this

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 
}