【问题标题】:PHP Only display 2 items per page on my shopping cartPHP 在我的购物车上每页只显示 2 件商品
【发布时间】:2014-01-07 09:03:01
【问题描述】:

我有一个工作脚本,它使用多维数组并将我的数据库中的项目引入用户购物车,但是列表不断增长。我只想在购物车中显示 2 件商品,如果购物车中的商品大于 2,则加载接下来的两页。

这是引入购物车项目的工作代码。

$cartoutput = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
    $cartoutput = "<font>Your Cart is currently empty.</font>";
}
else{
    $i=0;
    foreach($_SESSION["cart_array"] as $each_item){
        $i++;
        $cartoutput .="<h2>Cart Item $i</h2>";
        while(list($key, $value) = each($each_item)){
            $cartoutput .="$key: $value <br /><br />";
        }

    }
}

这是我尝试过的方法:无法正常工作:

if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) > 2){
    $start = (isset($_GET['start']) ? (int)$_GET['start'] : 0); //setting the get function for pagnation

echo "<table width=\"1024\" align=\"center\" >";
    echo "<tr height=\"50\"></tr>";
    echo "<tr>";


$count = $_SESSION["cart_array"]; 
    $prev = $start - 2;
    if ($prev >= 0) {
        echo '<td><a href="?start=' . $prev . '">Previous Items</a></td>';
    }                                                                     

    $next = $start + 2;
    if ($next < $count) {
        echo '<td><a href="?start=' . $next . '">Next Items</a></td>';
    }                                                                 

echo "</tr>";    
echo "</table>";
}

谁能告诉我如何将用户项目限制为每页 2 个,如果超过 2 个,添加下一个和上一个按钮?

谢谢

【问题讨论】:

  • &lt;font&gt; 标签已弃用。
  • 我知道,这很复杂,但我使用的是 Fresco 浏览器,它可以使用。不过,这不是我的问题。
  • 我会将您的脚本保留为原始脚本,但使用客户端脚本方法轻松简单地进行分页:Datatables 您只需对其进行一些调整即可使其正常工作。

标签: php database session get


【解决方案1】:

我认为你有一个小问题。而不是
$count = $_SESSION["cart_array"];

我认为你的意思是:
$count = count($_SESSION["cart_array"]);

【讨论】:

  • 谢谢乔!它按应有的方式显示下一个和上一个按钮,但它不会一次将项目数限制为 2 个。
【解决方案2】:
        <?php
        session_start();

        $_SESSION['cart_array_start'] = $_GET['start'] ;

        $_SESSION['cart_array'] = array("a", "b", "c", "d", "e", "f", "g", "h",         "i");


        $cartoutput = "";
        if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
            $cartoutput = "<font>Your Cart is currently empty.</font>";
        }
        else{
            $i=0;
            //set our start session to 1 if it doesn't exist.
            if (!isset($_SESSION['cart_array_start'])){
                $_SESSION['cart_array_start'] = 1 ;
            }
            $first_product = $_SESSION['cart_array_start'] ;
            //echo $first_product ;
            $last_product = $_SESSION['cart_array_start'] + 1 ;
            foreach($_SESSION["cart_array"] as $each_item){

                $i++;
                //echo $i ;
                if ($i == $first_product || $i == $last_product){
                    $cartoutput .="<h2>Cart Item $i</h2>";
                    $key = $i - 1 ;
                    $cartoutput .="$key: $each_item <br /><br />";

                }

            }
        }

        echo $cartoutput ;

        //code to start at the previous page.
        if (empty($_SESSION['cart_array_start']) || $_SESSION['cart_array_start'] <= 2)        {
            //they dont get a back button because they are on the first page.
        }else{
            $back = $_SESSION['cart_array_start'] - 2 ;
            echo "<a href=\"test.php?start=" . $back . "\">Back one page</a><br />";
        }

        //next page code.
        if (count($_SESSION['cart_array']) > 2){
            //start to display it - but lets check to see if they are on the last page         first.
            $next = $_SESSION['cart_array_start'] + 2 ;
            //give our product in cart count plus one because we want to display the         last page even if it only has 1 product.
            $products = count($_SESSION['cart_array']) + 1 ;
            if ($next > $products){
                //do nothing because nothing will be on that page.
            }else{
                //give them a next button.
                echo "<a href=\"test.php?start=" . $next . "\">next page</a>";
            }
        }
        ?>

【讨论】:

  • 它只是为您创建一个新会话并使用该会话来跟踪他们在阵列中的哪些产品。它每次都遍历整个数组,并且仅在数组键与其所在的迭代匹配时才运行 $cartoutput .= 部分。每次有效显示两个。
  • 谢谢。我试过你的代码,它应该显示下一个和上一个按钮,但它实际上并没有加载第三个项目。它只是填充按钮。我错过了什么吗?
  • 我的锚标签与您的不同。您是否更改了代码中的那些?从我为按钮发布的锚标签中删除 page.php。
  • 我更新了代码。它在我的服务器上运行良好。我修正了 $_SESSION['cart_arrat_start'] 的错字。
  • 不要忘记再次更改锚标签。我将它们作为 test.php,因为它在我的服务器上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 2013-07-10
  • 1970-01-01
  • 2021-11-27
  • 2015-07-26
  • 1970-01-01
  • 2020-11-03
相关资源
最近更新 更多