foreach内の配列をうまく回せません
php
1<?php 2 3include 'shop_class.php'; 4 5error_reporting(E_ALL); 6ini_set("display_errors",1); 7session_start(); 8if(!isset($_SESSION['id'])){ 9 header('Location:login.php'); 10} 11 12$shop = new Shop('mysql:host=localhost;dbname=ec_website','root','12345'); 13 14$user_id = intval($_SESSION['id']); 15if($_POST){ 16 // var_dump($_POST); 17 // exit(); 18 19 $product_name = $_POST['name']; 20 $img = $_POST['img']; 21 $price = $_POST['price']; 22 $product_id = intval($_POST['product_id']); 23 $item_check = $shop->cart_check($user_id,$product_id); 24 25 26 if(!$item_check){ 27 // try{ 28 $shop->add_select_item($user_id,$product_id); 29 // }catch(PDOException $e){ 30 // echo $e->getMessage(); 31 // } 32 33 echo __LINE__; 34 //exit(); 35 36 }else{ 37 $shop->update($product_id); 38 echo __LINE__; 39 //exit(); 40 } 41 42} 43 44$item_list = $shop->cart_list($user_id); 45//var_dump($item_list); 46//exit(); 47//exit(); 48// foreach($item_list as $item){ 49// //var_dump($item); 50// $item_info[] = $shop->cart_item_info($item['item_id']); 51// } 52//var_dump($item_info); 53 54foreach($item_list as $item){ 55 $item_info_list[] = $shop->item_info_list($item['item_id']); 56} 57 58var_dump($item_info_list); 59 60 61 62 63 64 65?> 66 67<!DOCTYPE html> 68<html lang="ja"> 69<head> 70 <meta charset="UTF-8"> 71 <link type="text/css" rel="stylesheet" href="./css/cart.css"> 72 <title>カートページ</title> 73</head> 74<body> 75 <header class="header_box"> 76 <a href="top.php"> 77 <img class="logo" src="./images/logo.png" alt="CodeShop"> 78 </a> 79 80 <p class="user_name"> 81 ユーザー名:<?php echo $_COOKIE['name']; ?> 82 </p> 83 <a href="cart.php"> 84 <img src="./images/cart.png"> 85 </a> 86 <a href="logout.php" class="logout"> 87 ログアウト 88 </a> 89 </header> 90 <div class="cart_content"> 91 <h3>ショッピングカート</h3> 92 <div class="product_list_title"> 93 <span class="product_list_price">価格</span> 94 <span class="product_list_num">数量</span> 95 </div> 96 <div class="product_list"> 97 <ul> 98 <?php foreach($item_info_list as $item_info): 99 foreach($item_list as $item_amount): 100 var_dump($item_amount['amount']); 101 102 ?> 103 104 <li> 105 <img src="<?=$item_info['img']?>" width="150px" height="100px"> 106 <span><?=$item_info['name']?></span> 107 <form action="cart.php" method="post"> 108 <input type="submit" value="削除"> 109 <input type="hidden" name="delete" value="1"> 110 </form> 111 <span><?=$item_info['price']?></span> 112 <form action="cart.php" method="post"> 113 <input type="text" name="quantity" value="<?=$item_amount['amount']?>"> 114 <input type="submit" value="変更する"> 115 </form> 116 117 </li> 118 <?php endforeach;?> 119 <?php endforeach;?> 120 121 122 </ul> 123 </div> 124 </div> 125 </body> 126 </html>
上記コードの、$item_info_listの中身が下記になります。
上記コードを実行した結果が下記になります
まだ、下にも画像は続きますが、上記画像のように同じ画像、名前、値段の情報を格納した、データが繰り返し表示されてしまっています。
このような現象が起こっている原因が分からないため、ご助言頂きたいです。
よろしくお願いします
追記です。
$item_listの中身は以下になります。
「array(4) { [0]=> array(2) { ["item_id"]=> int(13) ["amount"]=> int(22) } [1]=> array(2) { ["item_id"]=> int(15) ["amount"]=> int(2) } [2]=> array(2) { ["item_id"]=> int(17) ["amount"]=> int(1) } [3]=> array(2) { ["item_id"]=> int(14) ["amount"]=> int(2) } }」
$item_info_listをvar_exportした結果は以下の画像になります。
array ( 0 => array ( 'name' => 'ãã¹ã', 'price' => 100, 'img' => './images/f0ab631298df0117d17f6c91955a0be7129de339.jpg', ), 1 => array ( 'name' => 'test111', 'price' => 1000, 'img' => './images/4a60837fe9c460ed404f490ca86a61d7c936613b.jpg', ), 2 => array ( 'name' => 't-shirt2', 'price' => 1000, 'img' => './images/cccacab372172c1f63c1351d95df9e792fdd4002.jpg', ), 3 => array ( 'name' => 'test', 'price' => 1000, 'img' => './images/a482e68f5a960d0a2c47b8819a178b8b4f21c87c.jpg', ), )
回答1件
あなたの回答
tips
プレビュー