我如何将会话数据传递给另一个脚本,这是一个数据数组



我有两个脚本,一个使用Session数组存储用户购物车的详细信息,另一个是结帐。我想知道是否有可能将购物车的会话数据传递给结帐脚本,而不重复结帐中的代码。

     $page_title = 'View Your Shopping Cart';
      include ('includes/header.html');
    // Check if the form has been submitted (to update the cart):
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Change any quantities:
    foreach ($_POST['qty'] as $k => $v) {
        // Must be integers!
        $pid = (int) $k;
        $qty = (int) $v;
        if ( $qty == 0 ) { // Delete.
            unset ($_SESSION['cart'][$pid]);
        } elseif ( $qty > 0 ) { // Change quantity.
            $_SESSION['cart'][$pid]['quantity'] = $qty;
        }
    } // End of FOREACH.
        } // End of SUBMITTED IF.
        // Display the cart if it's not empty...
        if (!empty($_SESSION['cart'])) {
    // Retrieve all of the information for the prints in the cart:
    require ('../mysqli_connect.php'); // Connect to the database.
    $q = "SELECT print_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS      artist, print_name FROM artists, prints WHERE artists.artist_id = prints.artist_id AND prints.print_id IN (";
    foreach ($_SESSION['cart'] as $pid => $value) {
        $q .= $pid . ',';
    }
    $q = substr($q, 0, -1) . ') ORDER BY artists.last_name ASC';
    $r = mysqli_query ($dbc, $q);
    // Create a form and a table:
    echo '<form action="view_cart.php" method="post">
    <table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
    <tr>
        <td align="left" width="30%"><b>Artist</b></td>
        <td align="left" width="30%"><b>Print Name</b></td>
        <td align="right" width="10%"><b>Price</b></td>
        <td align="center" width="10%"><b>Qty</b></td>
        <td align="right" width="10%"><b>Total Price</b></td>
    </tr>
    ';
    // Print each item...
    $total = 0; // Total cost of the order.
    while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {
        // Calculate the total and sub-totals.
        $subtotal = $_SESSION['cart'][$row['print_id']]['quantity'] * $_SESSION['cart'][$row['print_id']]['price'];
        $total += $subtotal;
        // Print the row:
        echo "t<tr>
        <td align="left">{$row['artist']}</td>
        <td align="left">{$row['print_name']}</td>
        <td align="right">${$_SESSION['cart'][$row['print_id']]['price']}</td>
        <td align="center"><input type="text" size="3" name="qty[{$row['print_id']}]" value="{$_SESSION['cart'][$row['print_id']]['quantity']}" /></td>
        <td align="right">$" . number_format ($subtotal, 2) . "</td>
        </tr>n";
    } // End of the WHILE loop.
    mysqli_close($dbc); // Close the database connection.
    // Print the total, close the table, and the form:
    echo '<tr>
        <td colspan="4" align="right"><b>Total:</b></td>
        <td align="right">$' . number_format ($total, 2) . '</td>
    </tr>
    </table>
    <div align="center"><input type="submit" name="submit" value="Update My Cart" /></div>
    </form><p align="center">Enter a quantity of 0 to remove an item.
    <br /><br /><a href="checkout.php">Checkout</a></p>';
} else {
    echo '<p>Your cart is currently empty.</p>';
}
include ('includes/footer.html');
?>

 $page_title = 'Order Confirmation';
 include ('includes/header.html');

 $cid = 1; // Temporary.

这是我需要从view_cart脚本

获得总数的地方
    $total = 178.93; // Temporary.
     require ('../mysqli_connect.php'); // Connect to the database.
    // Turn autocommit off:
    mysqli_autocommit($dbc, FALSE);
    // Add the order to the orders table...
    $q = "INSERT INTO orders (customer_id, total) VALUES ($cid, $total)";
    $r = mysqli_query($dbc, $q);
    if (mysqli_affected_rows($dbc) == 1) {
        // Need the order ID:
    $oid = mysqli_insert_id($dbc);
    // Insert the specific order contents into the database...
    // Prepare the query:
    $q = "INSERT INTO order_contents (order_id, print_id, quantity, price) VALUES (?, ?, ?, ?)";
    $stmt = mysqli_prepare($dbc, $q);
    mysqli_stmt_bind_param($stmt, 'iiid', $oid, $pid, $qty, $price);
    // Execute each query; count the total affected:
    $affected = 0;
    foreach ($_SESSION['cart'] as $pid => $item) {
        $qty = $item['quantity'];
        $price = $item['price'];
        mysqli_stmt_execute($stmt);
        $affected += mysqli_stmt_affected_rows($stmt);
    }
    // Close this prepared statement:
    mysqli_stmt_close($stmt);
    // Report on the success....
    if ($affected == count($_SESSION['cart'])) { // Whohoo!
        // Commit the transaction:
        mysqli_commit($dbc);
        // Clear the cart:
        unset($_SESSION['cart']);
        // Message to the customer:
        echo '<p>Thank you for your order. You will be notified when the items ship.</p>';
        // Send emails and do whatever else.
    } else { // Rollback and report the problem.
        mysqli_rollback($dbc);
        echo '<p>Your order could not be processed due to a system error</p>';
    }
} else { // Rollback and report the problem.
    mysqli_rollback($dbc);
    echo '<p>Your order could not be processed due to a system error.</p>'; 
}
mysqli_close($dbc);
include ('includes/footer.html');
?>

如果这两个脚本在同一域并且没有任何前置内容,那么在每个文件中您必须在开头添加:

session_start();

然后在一个文件中你可以这样做:

$_SESSION['whatever] = 'test data';

在第二个脚本中你可以运行

echo $_SESSION['whatever'];
显示

test data

在您的情况下,在第一个脚本中计算total后,您应该简单地执行:

$_SESSION['total'] = $total

在第二行中你可以使用

显示它
echo $_SESSION['total'];

但是你要记住你必须把session_start放在两个脚本的开头

相关内容

  • 没有找到相关文章