0

次の質問があります。以下のような配列セッションにショッピングカートを保存します

session_start();
$id= $_GET['id'];
if(isset($_SESSION['cart']))
{
array_push($_SESSION['cart'], $id);
}
else
    $_SESSION['cart']= array($id);

header("location:cart.php");

そしてカートを取り出そうとすると。カートに入れた数だけ同じ製品IDを取得します。

<?php
if(!isset($_SESSION['cart'])) {
    echo "Your cart is empty.<br /><br /><a href='products.php'>Show products</a>";
} else {
    echo '<table border="0.2">';

    $total_price = 0;

    foreach($_SESSION['cart'] as $id) {
        $the_query = "select * from products where id='$id' GROUP BY id";

        $result = mysql_query($the_query) or die('Query failed: ' . mysql_error());

        $the_product = mysql_fetch_array($result, MYSQL_ASSOC);

        $total_price = $total_price + $the_product['price'];

        $href = "show_products.php?id=".$the_product['id'];
        //echo "<tr>";
        echo "<tr><td><a href='$href'>";
        echo "<img src='".$the_product['image_url_small']."' /></a></td>";
        echo "<td><strong>".$the_product['name']."</strong></td><td><em>$".$the_product['price']."</em>";
        echo "</td>";
        echo "<td> <a href='do_deletecart.php?id=". $the_product['id'] ."'>Delete item </a></td></tr>";
    }
    echo "<tr><td colspan='2'></td></tr>";
    echo "<tr><td style='text-align:center;font-size:40px;'>$</td><td><strong>Total</strong><br /><em>$".$total_price."</em></td></tr>";
    echo "</table>";
    echo "<br /><a href='empty_cart.php'>Empty Cart</a> <a href='showallproducts.php'>Show phones</a><br /><br />";
}

製品 ID または製品名を 1 つだけ表示するにはどうすればよいですか。少し早いですがお礼を

4

2 に答える 2

1

あなたの質問を正しく理解できれば、同じ製品 ID に対して多くの結果が得られます。$_SESSIONこれは、変数に同じ id 値を何度も格納しているためです。

変数で同じ ID を繰り返さないようにするには、次のようにし$_SESSIONます。

編集

完全を期すために、コードを更新しました。それが役立つことを願っています。

index.php

<?php

session_start();

$id= isset($_GET['id']) ? $_GET['id'] : null;

if(!is_null($id)){
    if(isset($_SESSION['cart']) && count($_SESSION['cart']) > 0){

        // increment product quantity if already exists
        // or create a new one
        add_or_increment_product_to_cart($id, $_SESSION['cart']);

    } else {
        // initialize cart
        // add the first product
        $_SESSION['cart'] = array();
        array_push($_SESSION['cart'], (object) array('id' => $id, 'quantity' => 1));
    }
}

function add_or_increment_product_to_cart($id, $cart){

    foreach ($cart as $key => $product) {
        if($id == $product->id){
            $product->quantity++;
            return;
        }
    }

    array_push($_SESSION['cart'], (object) array('id' => $id, 'quantity' => 1));
}

header("location:cart.php");

Cart.php

<?php

session_start();

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : null;

if($cart) {
    foreach ($cart as $key => $product) {
        $the_query = "SELECT * FROM products WHERE id=" . $product->id . " LIMIT 1";

        // your code to fetch the products from the database
        // what you have done is fine but vulnerable
        // PDO recommended 
    }
} else {
    echo "Your cart is empty.<br /><br /><a href='products.php'>Show products</a>";
}

mysql_connectまた、は推奨されておらず、データベースに接続するための推奨される安全な方法はPDOクラスであることに注意してください。@Touki がコメントで述べたように、コードは SQL インジェクションに対して脆弱です。

于 2013-04-29T10:54:51.547 に答える
0

クエリを 1 つだけ実行してすべての製品を取得し、クエリの結果を繰り返して HTML に入力することをお勧めします。例えば;

$the_query = "select * from products where id in (". implode(',', $_SESSION['cart']) .")";
$result = mysql_query($the_query);
while (($the_product = mysql_fetch_array($result, MYSQL_ASSOC))) {
    ...
}

これには、クエリを 1 つだけ実行し、製品ごとに 1 つの行のみを選択するという追加のボーナスがあります。

ただし、mysql_* メソッドは非推奨であり、mysqli や PDO などの別のライブラリの使用を開始することをお勧めします。

関連する注意事項として、このコードは現在 SQL インジェクションの影響を非常に受けやすく、理想的には、クエリ文字列に入れる前に入力をサニタイズする必要があります。

于 2013-04-29T10:40:09.887 に答える