小さな ECサイトのカート部分で、ユーザーが商品を選んだ際にその商品情報をDBに取りに行って、ユーザーがドロップダウンから選択した商品個数とともに、表示をしたいと思っています。
商品が選ばれた時(product_get.php)に、DBに見にいく(cart.inc)ようにしています。
結果は以下のように表示されます。
php
1array(1) { [0]=> array(1) { ["item"]=> NULL } } 1
なぜ、中身が入ってこないのでしょうか。
cart.incのSELECT文実行後がよくないんだろうなぁと思ってるのですが、どう直していいのかわかりません。よろしくお願いします。
以下に、使っているプログラムを載せます。
php
1<?php 2//product_get.php// 3session_start(); 4ini_set("display_errors","on"); 5error_reporting(E_ALL | E_STRICT); 6ini_set("include_path","./includes"); 7include("dbinfo.inc"); 8$quantity = 0; 9?> 10<!DOCTYPE> 11<HTML> 12<HEAD> 13 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 14 <title>Ex6 Sofa</title> 15 <link rel="stylesheet" href="custom.css"> 16 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"> 17</HEAD> 18<BODY> 19 <div class="col-xs-6 col-xs-offset-3"> 20 <h1>Online shop</h1><p>Hi, <?php echo $_SESSION['username']; ?>!!</p><p>Customer ID:<?php echo $_SESSION['cust_id']; ?></p> 21 <?php 22 if(!isset($_SESSION['catalog_id'])) { 23 ?> 24 <dl class="cart"> 25 <dd>Quantity of items:<?php print $quantity; ?></dd> 26 <form action="cartcontent.php" method="post"> 27 <input type="hidden" item="cart" value="<?php print$quantity; ?>" name="cart" /> 28 <input value="See Cart" type="submit"> 29 </form> 30 </dl> 31<?php 32} 33{ 34 // データベースに接続 35 $dbLink = mysqli_connect($host, $user, $passwd, $dbname); 36 37 // Get product info 38 $sql = "SELECT pd.catalog_id, pd.name, pd.description, pd.price, pd.category_id, 39 pi.path, pi.path_small FROM product_discription pd, product_img pi 40 WHERE pi.catalog_id = pd.catalog_id 41 AND pi.category_id = " . $_POST['category']; 42 43 $result = mysqli_query($dbLink, $sql) or trigger_error(mysqli_error($dbLink)); 44 45 $itemfc = ""; 46 while ($item = mysqli_fetch_array($result)) { 47?> 48 <dl class="products"> 49 <dd><a href="item_detail.php?product_id=<?echo $item['catalog_id'] ?>"><?php print($item['name']); ?></a></dd> 50 <dd>Category:<?php print($item['category_id'] ); ?></dd> 51 <dd>Catalog ID:<?php print($item['catalog_id'] ); ?></dd> 52 <dd>Price:$<?php print($item['price'] ); ?>CAD</dd> 53 <dd>Description:<?php print($item['description'] ); ?></dd> 54 <dt><img src="furniturestore/<?php print($item['path_small'] ); ?>" alt="" /></dt> 55 </dl> 56 <form action="cart.php" method="post"> 57 <select name="num"> 58 <?php 59 for ($i = 1; $i <= 9; $i++) { 60 echo "<option value='" . $i . "'>".$i."</option>"; 61 $quantity = $i; 62 } 63 ?> 64 </select> 65 <input type="hidden" name="act" value="add"> 66 <input type="hidden" name="itemToAdd" value="<?php echo $item['catalog_id'] ?>"> 67 <input type="submit" name="submit" value="Go to Cart"> 68 </form> 69<?php 70} 71mysqli_free_result($result); 72 } 73?> 74 </div> 75 </body> 76 </html> 77
php
1<?php 2//add.php// 3ini_set("display_errors","on"); 4error_reporting(E_ALL | E_STRICT); 5ini_set("include_path","./includes"); 6include("cart.inc"); 7session_start(); 8$cart = new Cart(); 9 10 11?> 12<!DOCTYPE> 13<HTML> 14<HEAD> 15 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 16 <title>Ex6 Add</title> 17 <link rel="stylesheet" href="custom.css"> 18 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"> 19</HEAD> 20<BODY> 21 <div class="col-xs-6 col-xs-offset-3"> 22 23<form action="cartcontent.php" method="post"> 24 <?php 25 $items =$cart->getAll(); 26 var_dump($items); 27 print_r(count($items)); 28 29 foreach ($cart->getAll() as $item) { 30 echo "added" . $item['quantity'] . " " . $item['item']['name']; 31 } 32 ?> 33 <input type="hidden" item="cart" value="" name="cart" /> 34 <input value="See Cart" type="submit"> 35</form> 36
php
1<?php 2//cart.php// 3 4ini_set("display_errors","on"); 5error_reporting(E_ALL | E_STRICT); 6ini_set("include_path","./includes"); 7include("dbinfo.inc"); 8include("cart.inc"); 9session_start(); 10// 指定されたリクエストパラメータを取得する 11function getRequestValue($key, $default=null) 12{ 13 if (isset($_REQUEST[$key])) { 14 // 存在するときはリクエストパラメータを返す 15 return $_REQUEST[$key]; 16 } else { 17 // 存在しないときはデフォルト値を返す 18 return $default; 19 } 20} 21$do =""; 22$cart = new Cart(); 23 24if (isset($_POST['itemToAdd'])) { 25$qty = ($_POST['quantity']); //rename to the form value name in product_get 26$act = getRequestValue('act','cart'); // アクションを取得 27switch ($act) { 28case 'add': // 追加 29 $item = $_POST['itemToAdd']; 30 if (!is_null($item)) { 31 $cart->addItem($item,$qty); 32 $do = "add.php"; 33 } else { 34 $do = "loggedin.php"; 35 } 36 break; 37case 'cart': // 中身を表示 38 $item = $cart->getAll(); 39 $do = "cartcontent.php"; 40 break; 41case 'remove': // 削除 42 $catalog_id = $_REQUEST($_POST['catalog_id']); 43 if (!is_null($_POST['cart'])) { 44 include(cart_empty.php); 45 } 46 $item = $_POST['cart']->getAll(); 47 $do = "cartcontent.php"; 48 break; 49case 'order': // 注文 50 $cart->removeAll(); 51 $do = "order.php"; 52 break; 53default: // (存在しない動作) 54 echo "What's up?"; 55 exit; 56 break; 57} 58} 59header("Location: " . $do) 60//include($do); 61?> 62
php
1<?php 2//cart.inc// 3include("dbinfo.inc"); 4class Cart { 5 function __construct() { 6 if (!array_key_exists('cart', $_SESSION)) { 7 $_SESSION['cart'] = array(); 8 } 9 } 10 // カートの中身を取得する 11 function getAll() { 12 return $_SESSION['cart']; 13 } 14 // カートに商品を追加する 15 function addItem($item, $quantity) { 16 $id = $item; 17 if(!isset($_GET['catalog_id'])) { 18 $dbLink = mysqli_connect($host, $user, $passwd, $dbname); 19 $sql = "SELECT pd.catalog_id, pd.name, pd.description, pd.price, pd.category_id, 20 pi.path FROM product_discription pd, product_img pi 21 WHERE pi.catalog_id = pd.catalog_id 22 AND pd.catalog_id = " . $id; 23 } 24 $result = mysqli_query($dbLink, $sql) or trigger_error(mysqli_error($dbLink)); 25 if (isset($_SESSION['cart'][$this->numberOfItems()]['id'])) { 26 // 注文数を増やす 27 $_SESSION['cart'][$this->numberOfItems()]['item'] = $items; //TODO: Item returned from db; 28 $_SESSION['cart'][$this->numberOfItems()]['quantity'] += $_POST['num']; //TODO: Item returned from db; 29 } 30 else { //新規注文 31 $_SESSION['cart'][$this->numberOfItems()]['item'] = $items; 32 } 33 } 34 // カートから商品を取り除く 35 function removeItem($catalog_id) { 36 unset($_SESSION['cart'][$catalog_id]); 37 } 38 // カートを空にする 39 function removeAll() { 40 $_SESSION['cart'] = array (); 41 } 42 43 function getItem($index) { 44 return $_SESSION['cart'][$index]; 45 } 46 47 function numberOfItems() { 48 return count($_SESSION['cart']); 49 } 50} 51?> 52
テーブルは以下の通りになります。(table名はdiscriptionとスペルを間違えてますが、直せなかったのでこれでやってます)
Table Name: product_discription
Column Name | Type | Description |
---|---|---|
catalog_id | INT(5) | Customer ID number.AUTO_INCREMENT. Primary key.Unique. |
name | VARCHAR(50) | Product name. |
description | VARCHAR(500) | The description of product. |
price | DECIMAL(7,2) | The price of product. |
category_id | INT(5) | The category ID of product. |
Table Name: product_category
Column Name | Type | Description |
---|---|---|
category_id | INT(5) | Category ID number.AUTO_INCREMENT. Primary key.Unique. |
category | VARCHAR(100) | The name of the category. |
Table Name: product_img
Column Name | Type | Description |
---|---|---|
catalog_id | INT(5) | Catalog ID number.AUTO_INCREMENT. Primary key.Unique. |
path | VARCHAR(300) | the small picture path. |
path_small | VARCHAR(300) | the picture path. |
category_id | INT(5) | Category ID number. |
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/28 19:29
2017/06/28 21:07
退会済みユーザー
2017/06/29 00:37
2017/06/30 04:05