dongmufen8105 2017-01-13 12:55
浏览 24
已采纳

PHP Session仅保存最后一项[重复]

This question already has an answer here:

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.

</div>
  • 写回答

1条回答 默认 最新

  • dongqigu0429 2017-01-13 12:56
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路