dongmei1828 2016-10-14 23:26
浏览 17
已采纳

在不使用PHP的情况下计算foreach循环中的会话

I am trying to count all my products that is stored in a session called $_SESSION['product'.$id];. But its not counting, its counting like this 1 1 1 1. So its counting 1 of each product id separated. My foreach loop...

foreach ($_SESSION as $name => $value) {
    if($value > 0){
        if(substr($name, 0, 8 ) == "product_"){
            $length = strlen($name) -8;
            $item_id = substr($name,8 , $length);

            $query = "SELECT * 
                 FROM gallery2 
                 WHERE gallery2.id =".escape_string($item_id). "";
            $run_item = mysqli_query($conn,$query);
            while($rows = mysqli_fetch_assoc($run_item)){ 
                $vari   = $rows['variante'];
                $num    = $rows['title'];
                $id     = $rows['id'];

                if(!isset($_SESSION['icms'.$id])) {
                    $_SESSION['icms'.$id]='0';
                }else{
                    $_SESSION['icms'.$id];
                }

                //some code here

                $subtotal=$value * $_SESSION['icms'.$id];
                $cost=$_SESSION['icms'.$id];

                $product = '
        <tr>
          <td style="width:100px; "><img src="../'.$rows['image'].'" style="width:90%;border: 1px solid black;"></td>
          <td>'.$num.''.$vari.'</td>
          //some code here
          <td class="product'.$id.'">'.$value.'</td>
          <td class="cost" data-id="'.$id.'" >R$:'.$cost.'</td>
          <td class="subtotal" data-id="'.$id.'">R$:'.number_format($subtotal, 2, '.', '') .'</td>
          <td> 
             '.$btn_add.' '.$btn_remove.' '.$btn_delete.'
          </td>
        </tr>';
                echo $product;  
                //some closing brackets 

Why this is happening? What i tried:

print_r(count($_SESSION['product_'.$item_id])) ;
print_r(count($name)) ;
print_r(count($product)) ;
print_r(count($value)) ;

enter image description here

  • 写回答

1条回答 默认 最新

  • duan0530 2016-10-14 23:47
    关注

    You can use this:

    $product_count = count(array_filter(array_keys($_SESSION), function($x) {
        return substr($x, 0, 8) == 'product_';
    }));
    

    But as I mentioned in a comment, it would probably be better if you redesigned your data. Instead of storing each product in a separate session variable, store them in an array. So instead of $_SESSION['product_'.$i] you would have $_SESSION['products'][$i]. Then you wouldn't need the code that checks whether the session variable beings with product_, you could just use

    foreach ($_SESSION['products'] as $id => $value)
    

    And to get the number of products, it would just be count($_SESSION['products']).

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line