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

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

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100