dousi1097 2017-09-23 16:00
浏览 62
已采纳

Php在while中分配会话数组

I would give the datas in the databases and assign to session as array in PHP. I assign value session in while loop. I was think the session is an array but i looked and i view is not an array. I would want to do create session in the while i give the datas of the databases and give the datas of the databases as array in this session. After use this session with foreach in another page.

Code :

<?php

$vericek15 = $baglanti10 -> prepare("select * from sepet where kulad = :kulad");
$vericek15 -> bindParam(':kulad', $kulad8);
$vericek15 -> execute();
$vericek16 = $baglanti10 -> prepare("select * from sepet where kulad = ?");
$vericek16 -> bindParam(1, $kulad8);
$vericek16 -> execute();
$vericek16say = $vericek16 -> rowCount();

if ($vericek16say == 0) {
?>
    <div id="surunyokl">
        <label>Your cart is empty</label>
    </div>

    <style type="text/css">
    #surunyokl{
        position: absolute;
        top: 325px;
        left: 625px;
    }
    </style>

<?php
} else {
?>
<div id="sepettablo">
    <table>
        <tr>
            <th style="text-align: center;">Image of product</th>
            <th style="text-align: center;">Product</th>
            <th style="text-align: center;">Piece</th>
            <th style="text-align: center;">Price</th>
            <th style="text-align: center;">Delete</th>
        </tr>

<?php
while($bilgiler15 = $vericek15 -> fetch(PDO::FETCH_ASSOC)){

    $vericek17 = $baglanti10 -> prepare("select * from urunlist where urunad = :urunad");
    $vericek17 -> bindParam(':urunad', $bilgiler15['urunad']);
    $vericek17 -> execute();

    $vericek18 = $baglanti10 -> prepare("select * from urunlistresim where urunad = :urunad2");
    $vericek18 -> bindParam(':urunad2', $bilgiler15['urunad']);
    $vericek18 -> execute();

    $bilgi18 = $vericek18 -> fetch();

    while($bilgiler17 = $vericek17 -> fetch(PDO::FETCH_ASSOC)){

        $_SESSION['b'] = $bilgiler15['urunad'];
        $_SESSION['urunadi'] = $bilgiler15['urunad'];
        $_SESSION['urunadet'] = $bilgiler15['urunadet'];
        $_SESSION['urunfiyat'] = $bilgiler17['urunfiyat'];

        if ($bilgiler17['urunadet'] == 0) {

        }

        echo '<tr>';
        echo '<td><img src="'.$bilgi18['resimyol'].'" height="100" width="100"></td>';
        echo '<td style="width:500px;text-align:center;">'.$bilgiler15['urunad'].'</td>';

        if ($bilgiler17['urunadet'] == 0) {

          echo '<td style="width:125px;text-align:center;"><input type="button" value="-" onclick="azalt('.$bilgiler15['no'].');">'.$bilgiler15['urunadet'].'</td>';

        }else {

          echo '<td style="width:125px;text-align:center;"><input type="button" value="-" onclick="azalt('.$bilgiler15['no'].');">'.$bilgiler15['urunadet'].'<input type="button" value="+" onclick="artir('.$bilgiler15['no'].');"></td>';

        }
        echo '<td style="width:75px;text-align:center;">'.$bilgiler17['urunfiyat'].'<label><b> € </b></label></td>';
        echo '<td><input type="button" value="Delete" onclick="sil('.$bilgiler15['no'].')"></td>';

        echo '</tr>';

        @$fiyat += $bilgiler17['urunfiyat'] * $bilgiler15['urunadet'];
        @$toplamadet += $bilgiler15['urunadet'];
        if ($toplamadet == 1) {
            $kargo = 5;
        }elseif ($toplamadet > 1) {
            $kargo = 5 + ($toplamadet - 1);
        }else{
        }
        $toplam = $fiyat + $kargo;
    }

}

I tried but i not couldn't.

How can i do this?

Please share code model.

I need your help.

Note : My code is complicated. Pardon me. I hoe you understand.

  • 写回答

1条回答 默认 最新

  • drn9573 2017-09-23 16:26
    关注

    Sorry I have no idea what the code is doing but you are basically not setting arrays into the SESSIOn you are overwriting data each time round the while loop

        $_SESSION['b'] = $bilgiler15['urunad'];
        $_SESSION['urunadi'] = $bilgiler15['urunad'];
        $_SESSION['urunadet'] = $bilgiler15['urunadet'];
        $_SESSION['urunfiyat'] = $bilgiler17['urunfiyat'];
    

    Amend it so you are adding a new item to the session each time round the while loop

        $_SESSION['b'][] = $bilgiler15['urunad'];
        $_SESSION['urunadi'][] = $bilgiler15['urunad'];
        $_SESSION['urunadet'][] = $bilgiler15['urunadet'];
        $_SESSION['urunfiyat'][] = $bilgiler17['urunfiyat'];
    

    Now you will have many $_SESSION['b'] occurances etc

    It woudl probably be easier to do this so each row is added as a new occurance in the array like this

    $_SESSION['bilgiler15'][] = array(
                                    'b'         => $bilgiler15['urunad'],
                                    'urunadi'   => $bilgiler15['urunad'],
                                    'urunadet'  => $bilgiler15['urunadet'],
                                    'urunfiyat' => $bilgiler17['urunfiyat'],
                                    );
    

    Or maybe

    $_SESSION['bilgiler15'][$bilgiler15['urunad']] = 
                            array(
                                    'urunadi'   => $bilgiler15['urunad'],
                                    'urunadet'  => $bilgiler15['urunadet'],
                                    'urunfiyat' => $bilgiler17['urunfiyat'],
                                );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单