循环使用会话

时间:2017-08-20 12:44:29

标签: php

计数值是每次刷新时的变化。

如何将计数值存储在变量中并在“for循环”中使用$x <= 10;

<?php
  echo "<div class='p_list_heading'> Product Name </div>";
  echo "<div class='p_list_heading'> Product Price </div>";
  echo "<div class='p_list_heading'> Product Quantity </div>";
  echo count($_SESSION['product_name']);

   for ($x = 0; $x <= 10; $x++) {

     if($_SESSION['product_name'][$x]!==""){

        echo "<div class=\"c_hold\">";
        echo "<div>".@$_SESSION['product_name'][$x]."</div>";
        echo "<div>".@$_SESSION['product_price'][$x]."Rs". "</div>";
        echo "</div>";
                                            }
    }
    ?>

2 个答案:

答案 0 :(得分:2)

存储在某个变量$y

$y = count($_SESSION['product_name']);
for ($x = 0; $x <= $y; $x++) {
    if($_SESSION['product_name'][$x]!==""){
        echo "<div class=\"c_hold\">";
        echo "<div>".@$_SESSION['product_name'][$x]."</div>";
        echo "<div>".@$_SESSION['product_price'][$x]."Rs". "</div>";
        echo "</div>";
    }
}

您也可以这样做,而不是存储在变量中:

for ($x = 0; $x <= count($_SESSION['product_name']); $x++) {
    if($_SESSION['product_name'][$x]!==""){
        echo "<div class=\"c_hold\">";
        echo "<div>".@$_SESSION['product_name'][$x]."</div>";
        echo "<div>".@$_SESSION['product_price'][$x]."Rs". "</div>";
        echo "</div>";
    }
}

答案 1 :(得分:0)

请尝试以下:

$count = count($_SESSION['product_name']);
echo $count;

for ($x = 0; $x <= $count; $x++)
{
}