PHP หาค่าซ้ำ ในตะกร้าสินค้า (ไม่ต้องการให้สั่งซื้อสินค้าชิ้นที่ใส่ในตะกร้าแล้วได้อีก)
ก็เอา product id ไป search ใน cart ที่มี session id เดียวกันสิ
ถ้ามี product id ก็ให้ไป update จำนวน
ถ้าไม่มีก็ให้ไป add new แทน
Date :
2016-05-12 16:30:41
By :
ห้ามตอบเกินวันละ 2 กระทู้
ขอบคณครับ ผมงงเรื่อง ARRAY นี่แหละครับ ผมจะหาค่าซ้ำใน $_SESSION['SHOPPING_CART'] ยังไง รบกวนช่วยยกตัวอย่างหน่อยได้ไหมครับ
Date :
2016-05-12 18:39:16
By :
ParotZ
Code (PHP)
<?
// Require //
require "inc/inc.common.php";
//----------------------------------//
?>
<?
$number = $_POST['number'];
?>
<?
// Product //
$strSQL = "SELECT * FROM product WHERE number LIKE '$number'";
$objQuery = mysql_query($strSQL) or die(mysql_error());
$rs = mysql_fetch_array($objQuery);
$price = $rs['price'];
$qty = 1;
//----------------------------------//
?>
<?
session_start();
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item)
$result_array = array_search($number, $item);
if($result_array!=FALSE){
echo 'ค้นพบ '.$number;
exit;
}
//Create 'cart' if it doesn't already exist
if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }
//Add an item only if we have the threee required pices of information: name, price, qty
if (isset($_POST['number']) && isset($rs['price']) && isset($qty)){
//Adding an Item
//Store it in a Array
$ITEM = array(
//Item name
'number' => $_POST['number'],
//Product id
'product_id' => $rs['id'],
//Item Price
'price' => $rs['price'],
//Qty wanted of item
'qty' => $qty
);
//Add this item to the shopping cart
$_SESSION['SHOPPING_CART'][] = $ITEM;
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
//Allowing the modification of individual items no longer keeps this a simple shopping cart.
//We only support emptying and removing
else if (isset($_GET['remove'])){
//Remove the item from the cart
unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
//Re-organize the cart
//array_unshift ($_SESSION['SHOPPING_CART'], array_shift ($_SESSION['SHOPPING_CART']));
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_GET['empty'])){
//Clear Cart by destroying all the data in the session
session_destroy();
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_POST['update'])) {
//Updates Qty for all items
foreach ($_POST['items_qty'] as $itemID => $qty) {
//If the Qty is "0" remove it from the cart
if ($qty == 0) {
//Remove it from the cart
unset($_SESSION['SHOPPING_CART'][$itemID]);
}
else if($qty >= 1) {
//Update to the new Qty
$_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
}
}
//Clear the POST variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><? echo $title.' '.$description.' [ '.$phone.' ]'; ?></title>
<meta name="author" content="<? echo $auther; ?>" />
<meta name="keywords" content="<? echo $keyword; ?>" />
<meta name="description" content="<? echo $description; ?>" />
<link rel='shortcut icon' href='http://localhost/favicon.ico'>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<!-- load stylesheets -->
<link rel="stylesheet" href="css/bootstrap.min.css"> <!-- Bootstrap style -->
<link rel="stylesheet" href="css/templatemo-style.css"> <!-- Templatemo style -->
<!--[sukhumvit Fixed Font Start]-->
<style type="text/css">
@font-face {
font-family: 'sukhumvit';
url('fonts/sukhumvit.svg#sukhumvit') format('svg');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'sukhumvit';
src: url('fonts/sukhumvit.eot');
src: url('fonts/sukhumvit.eot?#iefix') format('embedded-opentype'),
url('fonts/sukhumvit.woff') format('woff'),
url('fonts/sukhumvit.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
.sukhumvit
{
font-family: 'sukhumvit';
}
</style>
<!--[sukhumvit Fixed Font End]-->
<!--[Phone Digit Check Start ]-->
<script language="javascript">
function validate(){
var rs1 = /^0[0-7,9]\d{7}$/.test(document.getElementById('number').value);
var rs2 = /^0\d{9}$/.test(document.getElementById('number').value);
if(rs1 || rs2){
return true;
}
else{
document.getElementById('error').innerText = 'กรุณาใส่หมายเลขให้ถูกต้อง';
return false;
}
}
</script>
<!--[Phone Digit Check End]-->
</head>
<body>
<div class="container tm-container">
<div class="row navbar-row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 navbar-container">
<a href="index.php" class="navbar-brand" id="go-to-top">BERDEDDED</a>
<nav class="navbar navbar-full">
<div class="collapse navbar-toggleable-md" id="tmNavbar">
<div class="sukhumvit" style="font-size:10px">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="checknumber.php">วิเคราะห์เบอร์</a>
</li>
<li class="nav-item">
<a class="nav-link" href="shop.php">เบอร์มงคล</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cart.php">ตะกร้าสินค้า</a>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.php">ติดต่อเรา</a>
</li>
</ul>
</div>
</div>
</nav>
<button class="navbar-toggler hidden-lg-up" type="button" data-toggle="collapse" data-target="#tmNavbar">
☰
</button>
</div>
</div>
<div class="tm-page-content">
<!-- #cart -->
<section id="tm-section-9" class="row tm-section">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-5 col-xl-6 tm-contact-left">
<h2 class="tm-section-header thin-font col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12"><div class="sukhumvit" style="font-size:36px">ส่งข้อความ</div></h2>
<div class="sukhumvit" style="font-size:12px">
<form action="sendmail.php" method="post" class="contact-form">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-12 col-xl-6 tm-contact-form-left">
<div class="form-group">
<input type="text" id="contact_name" name="contact_name" class="form-control" placeholder="ชื่อของท่าน" required/>
</div>
<div class="form-group">
<input type="email" id="contact_email" name="contact_email" class="form-control" placeholder="เมล์ของท่าน" required/>
</div>
<div class="form-group">
<input type="text" id="contact_subject" name="contact_subject" class="form-control" placeholder="เรื่อง" required/>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-12 col-xl-6 tm-contact-form-right">
<div class="form-group">
<textarea id="contact_message" name="contact_message" class="form-control" rows="6" placeholder="ข้อความ" required></textarea>
</div>
<button type="submit" class="btn submit-btn">ส่งข้อความ</button>
</div>
</form>
</div>
</div>
<div class="tm-white-curve-right col-xs-12 col-sm-6 col-md-6 col-lg-7 col-xl-6">
<div class="tm-white-curve-right-circle"></div>
<div class="tm-white-curve-right-rec"></div>
<div class="tm-white-curve-text">
<h2 class="tm-section-header green-text">
<div class="sukhumvit" style="font-size:36px">
รายการสั่งซื้อ
</div>
</h2>
<div class="sukhumvit" style="font-size:16px">
<p>
<? echo $contacttext; ?>
</p>
</div>
<p></p>
<div class="contact-info-links-container">
<center>
<div id="shoppingCartDisplay">
<form action="" method="post" name="shoppingcart">
<?php
ob_start();
?>
<table width="230" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="30" height="20"> </td>
<td width="100" height="20">
<div class="sukhumvit" style="color:#0C5817;font-size:16px">
หมายเลข
</div>
</td>
<td width="100" height="20" align="right">
<div class="sukhumvit" style="color:#0C5817;font-size:16px">
ราคา
</div>
</td>
</tr>
<?php
$_SESSION['total'] = 0;
//Print all the items in the shopping cart
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item){
?>
<tr>
<td width="30" height="20">
<a href="?remove=<?php echo $itemNumber; ?>"><img src="images/del.png" width="20" height="20" alt=""/></a>
</td>
<td width="100" height="20">
<div class="sukhumvit" style="color:#000000;font-size:16px">
<?php echo $item['number']; ?>
</div>
</td>
<td align="right">
<div class="sukhumvit" style="color:#000000;font-size:16px">
<?php echo number_format($item['qty'] * $item['price'],2,'.',','); ?>
</div>
</td>
</tr>
<?php
$_SESSION['total'] += $item['qty'] * $item['price'];
}
?>
<tr>
<td width="30" height="20"> </td>
<td width="100" height="20" align="right">
<div class="sukhumvit" style="color:#000000;font-size:16px">
รวม
</div>
</td>
<td align="right">
<div class="sukhumvit" style="color:#FF0004;font-size:16px">
<? echo number_format($_SESSION['total'],2,'.',','); ?>
</div>
</td>
</tr>
</tbody>
</table>
<?php // $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
</form>
<p>
<p><font>[<a href="confirm_order.php" OnClick="return Conf(this)">ยืนยันการสั่งซื้อ</a>]</font></p>
</div>
</center>
</div>
</div>
</div>
</section>
<!-- #cart -->
<!-- footer -->
<div class="sukhumvit" style="color:#FFFFFF;font-size:16px">
<footer class="row tm-footer">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<p class="text-xs-center tm-footer-text">Copyright © 2016 <? echo $author; ?> Design: <? echo $designer; ?></p>
</div>
</footer>
</div>
<!-- footer -->
</div>
</div> <!-- .container -->
<!-- load JS files -->
<script src="js/jquery-1.11.3.min.js"></script> <!--.floating menu -->
<script src="js/bootstrap.min.js"></script> <!--.floating side menu -->
<!-- Templatemo scripts -->
<script>
var bigNavbarHeight = 90;
var smallNavbarHeight = 68;
var navbarHeight = bigNavbarHeight;
$(document).ready(function(){
var topOffset = 180;
$(window).scroll(function(){
if($(this).scrollTop() > topOffset) {
$(".navbar-container").addClass("sticky");
} else {
$(".navbar-container").removeClass("sticky");
}
});
/* Single page nav
-----------------------------------------*/
if($(window).width() < 992) {
navbarHeight = smallNavbarHeight;
}
$('#tmNavbar').singlePageNav({
'currentClass' : "active",
offset : navbarHeight
});
/* Collapse menu after click
http://stackoverflow.com/questions/14203279/bootstrap-close-responsive-menu-on-click
----------------------------------------------------------------------------------------*/
$('#tmNavbar').on("click", "a", null, function () {
$('#tmNavbar').collapse('hide');
});
// Handle nav offset upon window resize
$(window).resize(function(){
if($(window).width() < 992) {
navbarHeight = smallNavbarHeight;
} else {
navbarHeight = bigNavbarHeight;
}
$('#tmNavbar').singlePageNav({
'currentClass' : "active",
offset : navbarHeight
});
});
/* Scroll to top
http://stackoverflow.com/questions/5580350/jquery-cross-browser-scroll-to-top-with-animation
--------------------------------------------------------------------------------------------------*/
$('#go-to-top').each(function(){
$(this).click(function(){
$('html,body').animate({ scrollTop: 0 }, 'slow');
return false;
});
});
});
</script>
</body>
</html>
ผมงง ตรงลองใช้ array_search แล้ว หาค่าซ้ำได้แค่ record สุดท้าย สงสัยผมหาผิดที่ งงมากๆครับ รบกวนผ้รู้ช่วยให้แสงสว่างด้วยครับ
ประวัติการแก้ไข 2016-05-12 21:32:35 2016-05-12 21:42:37 2016-05-12 23:15:55 2016-05-12 23:17:24
Date :
2016-05-12 21:31:59
By :
ParotZ
Load balance : Server 05