-3

In follow up on this question: Save database row id in session for use later

I'm now running into the following problem. The session is now (thankfully) stored. And the next page's content is filled dynamically. But ALL pages have the same database id and not the id that is specific for that link/item.

I read something about the fact that I'm over writing the session ID's again and again. And I need to store the session in an Array.

So what I did was the following, I changed this:

$identi = $row['Id'];
$_SESSION["product_id"] = $identi;

Into this:

$identi = $row['Id'];
if(!is_array($_SESSION['product_id'])) {
        $_SESSION['product_id']=array();
    }
        $_SESSION['product_id']=$row['Id'];;

But this still only stores the ID of the last item in the session an not the individual items. So I can have each item link with a specific ID.

For good measure my SQL Query:

<?php $sql = "SELECT * FROM assortiment WHERE Categorie = '$productid' ORDER BY Id DESC ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

        echo "<a href='/dbtest/dinner/amsterdam/".$row["Slug"]. "/" . $row["Id"] . "'><div class='products'><div class='col-sm-3'><div class='product-img'>";
        $identi = $row['Id'];
        if(!is_array($_SESSION['product_id'])) {
            $_SESSION['product_id'] = array();
            }
            $_SESSION['product_id'] = $row['Id'];;

        echo "<img src='http://www.example.nl/Uploaded_files/Thumbs/" .$row['Fotomateriaal']. ".jpg'>";
        echo "</div></div><div class='col-sm-6'>";
        echo "<div class='h2-container'><h2>" . $row["Product"]. "</h2></div>" .  $row["Samenvatting"]. "";
        echo "</div><div class='col-sm-3'><div class='col-sm-12 text-right'>
                    <div class='border'>
                        <p style='margin-bottom:20px;width:30px;'><i class='fa fa-heart' aria-hidden='true'></i></p>
                    </div>
                </div>";
        echo "<table>
                <tr>
                    <td><i class='fa fa-users' aria-hidden='true'></i></td>
                    <td><p>vanaf 10 personen</p></td>
                </tr>
                <tr>
                    <td><i class='fa fa-clock-o' aria-hidden='true'></i></td>
                    <td>" . $row["Tijdsduur"] .  " uur</td>
                </tr>
                <tr>
                    <td><i class='fa fa-euro' aria-hidden='true'></i></td>
                    <td>vanaf " . $row["VerkoopPP40"] ." p.p. <small>excl btw</small></td>
                </tr>
            </table></div></div></a>";

    if(isset($_SESSION['product_id'])){
    echo $_SESSION['product_id'];
    };

    }
} else {
    echo "0 results";
}?>

Can anybody help me out with this one? As always thanks for the help in advance.

Community
  • 1
  • 1
Steggie
  • 525
  • 8
  • 31

1 Answers1

2

If you want to add new items to an array you do it like this :

$_SESSION['product_id'][] = $row['Id'];

What you did is only overwrite the value of the product_id in your $_SESSION array.

EDIT:

Please take a look at this arrays

Vasil Rashkov
  • 1,818
  • 15
  • 27