前提・実現したいこと
プログラミング初心者で現在練習のためにMySQLを用いてPHPでECサイトを作成している。
SESSIONを用いてお客様がカートに入れた商品の情報を保持したい。
こちらの質問の追加質問 SESSIONを用いたショッピングカートのデータ保持について
##追記(2019/02/07)
####追加した情報
- POSTで渡す値を$row['name']から$row['id']へ変更
- cart.phpでforeach文の追加
####問題点
//cart.phpのブラウザ表示 array(3) { [2]=> array(2) { ["cnt"]=> string(1) "1" ["size"]=> string(1) "S" } [""]=> array(2) { ["cnt"]=> NULL ["size"]=> NULL } [1]=> array(2) { ["cnt"]=> string(1) "1" ["size"]=> string(1) "L" } }
- 1番目に追加した商品の情報が2番目の商品の情報に上書きされ、同じ商品の情報が複数表示される。
- 配列が3つまでしか反映されない(4つ以上商品を追加してもブラウザ状には3つの同じ商品情報が表示されるだけ)。
####更新したコード
php
1//index.php 2 3<?php 4 5host = '*****'; 6$dbname = '*****'; 7$dbuser = '*****'; 8$dbpassword = '*****'; 9 10 11try{ 12 $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8","$dbuser","$dbpassword"); 13 echo ""; 14}catch(PDOException $e){ 15 var_dump($e -> getMessage()); 16 die(); 17 18} 19 20try{ 21 $sql = "SELECT * FROM EC"; 22 $stmh = $pdo->prepare($sql); 23 $stmh->execute(); 24}catch(PDOException $Exception){ 25 die('接続エラー:' .$Exception->getMessage()); 26} 27 28 29 30 31?> 32<!DOCTYPE html> 33 34<html lang="ja"> 35 <head> 36 <meta charset="utf-8"> 37 <title>*****</title> 38 <link rel="stylesheet" type="text/css" href="style.css"> 39 <link href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css" rel="stylesheet"> 40 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script> 41 <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/js/lightbox.min.js" type="text/javascript"></script> 42 </head> 43 <body> 44 <div> 45 <a href="cart.php"> 46 <img src="img/cart.png" class="cart-icon"> 47 </a> 48 </div> 49 <div> 50 <a href="index.php"> 51 <img src="img/*****.JPG" class="logo-icon"> 52 </a> 53 </div> 54 <?php 55 while($row = $stmh->fetch(PDO::FETCH_ASSOC)){ 56 ?> 57 <div class="item-list"> 58 <a href="<?=htmlspecialchars($row['image'])?>" data-lightbox="group"> 59 <img src="<?=htmlspecialchars($row['image'])?>" class="item-image"> 60 </a> 61 <p><?=htmlspecialchars($row['id'])?></p> 62 <h4> 63 <a href="detail.php?name=<?= htmlspecialchars($row['id'])?>"> 64 <?=htmlspecialchars($row['name'])?> 65 </a> 66 </h4> 67 <p><?=htmlspecialchars($row['description'])?></p> 68 <p>¥<?=htmlspecialchars($row['price'])?>-</p> 69 </div> 70 71 72 73 <?php 74 } 75 $pdo = null; 76 ?> 77 78 79 80 81 </body> 82 83</html> 84
php
1//detail.php 2 3<?php 4host = '*****'; 5$dbname = '*****'; 6$dbuser = '*****'; 7$dbpassword = '*****'; 8 9 10try{ 11 $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8","$dbuser","$dbpassword"); 12 echo ""; 13}catch(PDOException $e){ 14 var_dump($e -> getMessage()); 15 die(); 16 17} 18$itemId = $_GET['name']; 19 20 21 22 23 24 25 26 27try{ 28 $sql = "SELECT * FROM EC where id = '{$itemId}'"; 29 $stmh = $pdo->prepare($sql); 30 $stmh->execute(); 31}catch(PDOException $Exception){ 32 die('接続エラー:' .$Exception->getMessage()); 33} 34 35session_start(); 36 37?> 38 39<!DOCTYPE html> 40<html lang="ja"> 41 <head> 42 <meta charset="utf-8"> 43 <title>*****</title> 44 <link rel="stylesheet" type="text/css" href="style.css"> 45 <link href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css" rel="stylesheet"> 46 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script> 47 <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/js/lightbox.min.js" type="text/javascript"></script> 48 </head> 49 50 51 <body> 52 <?php 53 $row = $stmh->fetch(PDO::FETCH_ASSOC); 54 55 ?> 56 <div> 57 <a href="cart.php"> 58 <img src="img/cart.png" class="cart-icon"> 59 </a> 60 </div> 61 <div> 62 <a href="index.php"> 63 <img src="img/*****.JPG" class="logo-icon"> 64 </a> 65 </div> 66 <div class="detail-item-list"> 67 <a href="<?=htmlspecialchars($row['image'])?>" data-lightbox="group"> 68 <img src="<?=htmlspecialchars($row['image'])?>" class="detail-image"> 69 </a> 70 71 <h4> 72 <?=htmlspecialchars($row['name'])?> 73 </h4> 74 <p><?=htmlspecialchars($row['description'])?></p> 75 <p>¥<?=htmlspecialchars($row['price'])?>-</p> 76 <form action="cart.php" method="post"> 77 <select name="item-count"> 78 <?php for ($i=1; $i<=10; $i++) : ?> 79 <option class="item-count" value="<?php echo $i ?>"><?php echo $i ?></option> 80 <?php endfor; ?> 81 </select> 82 <select name="item-size"> 83 <option>S</option> 84 <option>M</option> 85 <option>L</option> 86 <option>XL</option> 87 88 </select> 89 <input type="hidden" name="item-id" value="<?= $row['id']?>"> 90 <input type="submit" name="submit" value="Add to Cart"> 91 </form> 92 <form action="index.php" method="post"> 93 <input type="submit" name="back" value="back"> 94 </form> 95 </div> 96 </body> 97</html> 98
php
1//cart.php 2 3<?php 4host = '*****'; 5$dbname = '*****'; 6$dbuser = '*****'; 7$dbpassword = '*****'; 8 9try{ 10 $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8","$dbuser","$dbpassword"); 11 echo ""; 12}catch(PDOException $e){ 13 var_dump($e -> getMessage()); 14 die(); 15 16} 17 18session_start(); 19$itemId = filter_input(INPUT_POST, 'item-id'); 20$_SESSION['cart'][$itemId] = array('cnt' => filter_input(INPUT_POST, 'item-count'), 'size' => filter_input(INPUT_POST, 'item-size')); 21var_dump($_SESSION['cart']); 22 23 24 25try{ 26 $sql = "SELECT * FROM EC where id = '{$itemId}'"; 27 $stmh = $pdo->prepare($sql); 28 $stmh->execute(); 29}catch(PDOException $Exception){ 30 die('接続エラー:' .$Exception->getMessage()); 31} 32 33 34 35$row = $stmh->fetch(PDO::FETCH_ASSOC); 36$itemCount = $_POST['item-count']; 37$itemSize = $_POST['item-size']; 38$itemSubtotal = $itemCount * htmlspecialchars($row['price']); 39$itemTotal += $itemSubtotal; 40 41 42 43?> 44 45<!DOCTYPE html> 46<html lang="ja"> 47 <head> 48 <meta charset="utf-8"> 49 <title>*****</title> 50 <link rel="stylesheet" type="text/css" href="style.css"> 51 </head> 52 <body> 53 54 <div> 55 <a href="cart.php"> 56 <img src="img/cart.png" class="cart-icon"> 57 </a> 58 </div> 59 <div> 60 <a href="index.php"> 61 <img src="img/*****.JPG" class="logo-icon"> 62 </a> 63 </div> 64 65 <div class="cart-item-list"> 66 <table class="cart-subtotal"><tbody> 67 <tr><th>Item</th><th>Price</th><th>Size</th><th>Quantity</th><th>Subtotal</th></tr> 68 <?php 69 foreach($_SESSION['cart'] as $key => $value){ 70 ?> 71 <tr> 72 <td> 73 <div class="table-image"> 74 75 <img src="<?=htmlspecialchars($row['image'])?>" class="cart-image"> 76 </div> 77 <div class="table-name" name="cart-item-image"> 78 <a href="detail.php?name=<?= htmlspecialchars($row['name'])?>"> 79 <?php 80 echo htmlspecialchars($row['name']); 81 82 ?> 83 </a> 84 </div> 85 <div class="table-description"> 86 <?=htmlspecialchars($row['description'])?> 87 </div> 88 </td> 89 <td>¥<?=htmlspecialchars($row['price'])?>-</td> 90 <td><?= $itemSize ?></td> 91 <td> 92 <div class="table-count"> 93 <?= $itemCount ?> 94 </div> 95 </td> 96 <td> 97 <div class="table-subtotal"> 98 ¥<?= $itemSubtotal ?>- 99 </div> 100 </td> 101 102 </tr> 103 <?php 104 } 105 ?> 106 </tbody></table> 107 108 <div class="cart-total"> 109 <h3>Total</h3> 110 <p> 111 ¥<?= $itemTotal ?>- 112 </p> 113 114 </div> 115 <div class="checkout"> 116 <form action="checkout.php?name=<?=htmlspecialchars($row['id'])?>" method="post"> 117 <input type="submit" name="checkout" value="Checkout"> 118 <input type="hidden" name="item-size" value="<?= $itemSize ?>"> 119 <input type="hidden" name="item-count" value="<?= $itemCount ?>"> 120 121 </form> 122 <form action="detail.php" method="post"> 123 <input type="submit" name="back" value="back"> 124 </form> 125 </div> 126 127 </div> 128 </body> 129</html> 130 131
####考えられる原因
当方が複雑なarrayやforeachの使い方を理解できていない。
「ここを直せばきれいにカート情報を保持できるよ」というアドバイスがあればご指摘よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー