|
|
|
ขอคำแนะนำหน่อย ครับ ส่งค่าตัวแปรกลับไปข้างบนทำไงครับ |
|
|
|
|
|
|
|
อยากนำตัวแปร $total ส่งไปข้างทำยังไงครับ
Code (PHP)
<?php
session_start();
if(isset($_POST["add_to_cart"]))
{
if(isset($_SESSION["shopping_cart"]))
{
$item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
if(!in_array($_GET["id"], $item_array_id))
{
$count = count($_SESSION["shopping_cart"]);
$item_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_taste' => $_POST["hidden_taste"],
'item_quantity' => $_POST["quantity"]
);
$_SESSION["shopping_cart"][$count] = $item_array;
}
else
{
echo '<script>alert("Item Already Added")</script>';
echo '<script>window.location="n1.php"</script>';
}
}
else
{
$item_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_taste' => $_POST["hidden_taste"],
'item_quantity' => $_POST["quantity"]
);
$_SESSION["shopping_cart"][0] = $item_array;
}
}
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($values["item_id"] == $_GET["id"])
{
unset($_SESSION["shopping_cart"][$keys]);
echo '<script>alert("Item Removed")</script>';
echo '<script>window.location="n1.php"</script>';
}
}
}
}
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($values["item_id"] == $_GET["id"])
{
unset($_SESSION["shopping_cart"][$keys]);
echo '<script>alert("Item Removed")</script>';
echo '<script>window.location="n1.php"</script>';
}
}
}
if($_GET["action"] == "empty")
{
unset($_SESSION["shopping_cart"]);
echo '<script>alert("Item Removed")</script>';
echo '<script>window.location="n1.php"</script>';
}
}
?>
<!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">
<meta name="description" content="">
<meta name="author" content="">
<title>SB Admin 2 - Bootstrap Admin Theme</title>
<!-- Bootstrap Core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- MetisMenu CSS -->
<!-- <link href="vendor/metisMenu/metisMenu.min.css" rel="stylesheet"> -->
<!-- Custom CSS -->
<link href="dist/css/sb-admin-2.css" rel="stylesheet">
<!-- Morris Charts CSS -->
<!-- <link href="vendor/morrisjs/morris.css" rel="stylesheet"> -->
<!-- Custom Fonts -->
<link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="../vendor/jquery/jquery.min.js"></script>
</head>
<body>
<div class="section">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">ปรุงยา</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-3 col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fa fa-plus-square fa-5x"></i>
</div>
<div class="col-xs-9 text-right">
<div class="huge">$total</div> //ส่งกลับขึ้นมาตัวนี้ครับ
<div>จำนวนรวมทั้งหมด : กรัม</div>
</div>
</div>
</div>
<div class="panel-footer" >
<span class="pull-right"><i class="fa "></i></span>
<div class="clearfix"></div>
</div>
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-bell fa-fw"></i> สมุนไพรที่มีความสัมพันธ์
</div>
<!-- /.panel-heading -->
<div class="panel-body ">
<div class="list-group" style="overflow-y: scroll; max-height: 410px; min-height: 410px">
<!-- <div class="pre-scrollable" rows="50"> -->
<?php
$sql = "SELECT * FROM herbs LIMIT 50,60";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<form method="post" action="n1.php?action=add&id=<?php echo $row["id"]; ?>">
<a href="#" class="list-group-item">
<input type="hidden" name="hidden_name" value="<?php echo $row["herb_name"]; ?>" />
<input type="hidden" name="hidden_price" value="<?php echo $row["herb_taste"]; ?>" />
<input type="hidden" name="hidden_taste" value="<?php echo $row["taste_id"]; ?>" />
<b> <i class="fa fa-leaf fa-fw"></i><?php echo $row["herb_name"]; ?>
<!-- <input type="text" name="quantity" class="form-control" style="width:100px; height:30px;" value="1" /> -->
<span class="pull-cnerter text-muted small"><em> <?php echo $row["herb_taste"]; ?></em></span></b>
<span class="pull-right text-muted small">
<input type="submit" name="add_to_cart" style="margin-top:-3px; width:100px; height:27px;" class="btn btn-success"
value="Add to Mix" /></span>
<span class="pull-right text-muted small"><em><input type="text" name="quantity" class="form-control" style="margin-top:-3px; width:100px; height:27px;" value="1" /></em></span>
</a>
</form>
<?php
}
}
?>
<!-- /.list-group -->
</div>
</div>
<!-- /.panel-body -->
</div>
</div>
<div class="col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-bell fa-fw"></i> สมุนไพรที่ใช้ในการปรุง
<span class="pull-right text-muted small">
<a href="n1.php?action=empty" style="margin-top:-3px; width:100px; height:30px;" class="btn btn-info">Empty</a>
</span>
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="list-group" style="overflow-y: scroll; max-height: 410px; min-height: 410px">
<?php
if(!empty($_SESSION["shopping_cart"]))
{
$total = 0;
$total1 = 0; $total2 = 0; $total3 = 0; $total4 = 0; $total5 = 0;
$total6 = 0; $total7 = 0; $total8 = 0; $total9 = 0; $total10 = 0;
$totalA = 0;
$totalB = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
?>
<input type="hidden" name="text[]" value="<?php echo $values["item_name"]; ?>"/>
<input type="hidden" name="text1[]" value="<?php echo $values["item_quantity"]; ?>"/>
<div class="list-group-item" style="-webkit-border-radius:5px; -moz-border-radius:5px;">
<i class="fa fa-flask fa-fw"></i> <?php echo $values["item_name"]; ?>
<span class="pull-cnerter text-muted small"><em> <?php echo $values["item_price"]; ?></em></span></b>
<span class="pull-right text-muted small">
<a href="n1.php?action=delete&id=<?php echo $values["item_id"]; ?>" style="margin-top:-3px; width:100px; height:27px;" class="btn btn-danger">Remove</a></span>
<?php number_format($values["item_quantity"] * '1'); ?>
<span class="pull-right text-muted small"><em><?php echo $values["item_quantity"]; ?></em></span>
</div>
<?php
$total = $total + ($values["item_quantity"] * '1'); // ตัวแปรตัวนี้ครับ
}
}
?>
</div>
<!-- /.list-group -->
</div>
<!-- /.panel-body -->
</div>
</div>
<!-- /.col-lg-4 -->
</div>
<!-- /.row -->
</div>
</div>
</body>
</html>
Tag : PHP, HTML/CSS, JavaScript
|
ประวัติการแก้ไข 2016-11-11 21:34:49
|
|
|
|
|
Date :
2016-11-11 21:34:12 |
By :
saharat2010 |
View :
839 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คุณควรออกแบบการเขียนโปรแกรมใหม่
จะทำการคำณวนหรือประมวลผลอะไรก็ตาม ทำให้มันเสร็จไปเลย แล้วเก็บค่าไว้ในตัวแปร แล้วค่อยเอาตัวแปรเหล่านั้นมาแสดงผลปนกับใน html
|
|
|
|
|
Date :
2016-11-11 23:01:54 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|