簡體   English   中英

如何在另一個PHP頁面中顯示信息

[英]How to display information in another PHP Page

我在使用迷你購物車時遇到了麻煩。 我創建了迷你購物車,當添加產品時,它會進入購物籃頁面。 但是,我想創建一個結帳頁面。 我如何抓住購物籃中的產品並將其放入結帳頁面。

因此,例如,結帳頁面將如下所示

項目名稱,項目模型,項目價格

                 TOTAL OF ITEMS

CHECKOUT BUTTON(鏈接到支付系統)

這是我的Basket.php代碼:

<?php
    $bikecode = $_GET['id'];     //the product id from the URL 
    $action = $_GET['action']; //the action from the URL

if($bikecode && !productExists($bikecode)) {
    die("Product Doesn't Exist");
} 

    switch($action) {   //decide what to do 

        case "add":
            $_SESSION['cart'][$bikecode]++; //add one to the quantity of the product with id $bikecode 
        break;

        case "remove":
            $_SESSION['cart'][$bikecode]--; //remove one from the quantity of the product with id $bikecode 
            if($_SESSION['cart'][$bikecode] == 0) unset($_SESSION['cart'][$bikecode]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
        break;

        case "empty":
            unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
        break;
    }

    if($_SESSION['cart']){

        echo "<table width=\"100%\">";   

          foreach($_SESSION['cart'] as $bikecode => $quantity) { 
             $sql = sprintf("SELECT BikeCode, Model, Price FROM Bike WHERE BikeCode = '%s';", $bikecode);

                $result = mysqli_query($con, $sql);

                if(mysqli_num_rows($result) > 0) {

                    list($bikecode, $model, $price) = mysqli_fetch_row($result);    

                    $cost = $quantity * $price;
                    $total = $total + $cost;

                        echo "<tr><th>BikeCode:</th><th>Model:</th><th>Quantity:</th><th>Price:</th></tr>";
                        echo "<tr>";
                        echo "<td align=\"center\">$bikecode</td>";
                        echo "<td align=\"center\">$model</td>";
                        echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$bikecode\">X</a></td>";
                        echo "<td align=\"center\">£$cost</td>";
                    echo "</tr>";
                }
            }

            echo "<tr>";
                echo "<td colspan=\"3\" align=\"right\">Total</td>";
                echo "<td align=\"right\">£$total</td>";
            echo "</tr>";

            echo "<tr>";
                echo "<td colspan=\"4\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
            echo "</tr>";       
        echo "</table>";

    }else{
        echo "You have no items in your shopping cart.";
    }

function productExists($bikecode) {
    $sql = sprintf("SELECT * FROM Bike WHERE BikeCode = '%s';", $bikecode); 
    return mysqli_num_rows(mysqli_query($con, $sql)) > 0;
}
?>

只要你有session_start(); 在每個頁面的頂部,您應該能夠使用與此編碼中完全相同的foreach循環。 與信息的會話將被轉移到下一頁。

假設您有一個名為test.php的頁面,其中包含以下代碼:

<?php
session_start();
$_SESSION['test'] = 1;
echo $_SESSION['test']; 
?>

然后,要訪問othertest.php頁面中的測試變量,只需執行以下操作:

<?php
session_start();
echo $_SESSION['test']; ?>

你忘了將session_start()添加到php文件的頂部。 我建議將session_start()添加到單獨的文件中,然后將該文件包含在使用會話變量的每個php頁面中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM