HOME > PHP > PHP Forum > ช่วยดูโค๊ด shopping cart หน่อยนะคะ อ่านหนังสือแล้วลองทำตามดู แต่พอคลิกปุ่ม save ทำไมค่าที่อยู่ใน text ถึงไม่คำนวณตามตัวเลขที่เราใส่เข้าไป
ช่วยดูโค๊ด shopping cart หน่อยนะคะ อ่านหนังสือแล้วลองทำตามดู แต่พอคลิกปุ่ม save ทำไมค่าที่อยู่ใน text ถึงไม่คำนวณตามตัวเลขที่เราใส่เข้าไป
โค๊ด function
<?
function calculate_price($cart)
{
$price = 0.0;
if(is_array($cart))
{
$conn = connect_db("onlineshop");
foreach($cart as $pid => $qty)
{
$query = "select price from products where pid = '$pid' ";
$result = mysql_query($query);
if ($result)
{
$item_price = mysql_result($result,0,"price");
$price+=$item_price*$qty;
}
}
}
return $price;
}
function calculate_items($cart)
{
$items = 0;
if(is_array($cart))
{
foreach($cart as $pid => $qty)
{
$items+=$qty;
}
}
return $items;
}
function display_cart($cart,$change)
{
global $items;
global $total_price;
echo"<table border = 0 width = 100% cellspacing=0 align=center>
<form action = show_cart.php method=post>
<tr bgcolor=FFBBBB>
<th><font face = MS Sans Serif size=3>no</font></th>
<th><font face = MS Sans Serif size=3>name</font></th>
<th><font face = MS Sans Serif size=3>price</font></th>
<th><font face = MS Sans Serif size=3>num</font></th>
<th><font face = MS Sans Serif size=3>total</font></th>
</tr>";
foreach ($cart as $pid => $qty)
{
$mobile=get_book_details($pid);
echo"<tr>";
echo"<td>";
echo "<a href =\"thdata.php?pid=".$pid."\"><font face = \"MS Sans Serif\" size=2>".$mobile["pid"]."</font></a></td>";
echo "<td><font face = \"MS Sans serif\" size=2>".$mobile["pname"]."</font></td>";
echo"<td align = center><font face =\"MS Sans serif\" size=2>".number_format($mobile["price"],2)."</font></td>";
echo"<td align = center>";
if ($change == true)
echo"<input type = text name =\"$pid\" value = $qty size = 2>";
else
echo "<font face =\"MS Sans serif\" size=2>".$qty."</font>";
echo"</td><td align = right><font face=\"MS Sans Serif\" size=2>".number_format($mobile["price"]*$qty,2)." コメキ</font></td></tr>\n";
}
echo "<tr bgcolor=#FFAAAA>
<th align = left colspan=3>total product</th>
<th align = center>$items</th>
<th align = right>".number_format($total_price,2)."コメキ</th></tr>";
if($change== true)
{
echo"<tr>
<td colspan=4> </td>
<td align = center>
<input type = hidden name = new value=0>
<input type = hidden name = save value = true>
<input type = submit value = \"save\"></td>
<td> </td>
</tr>";
}
echo"</form></table>";
}
function get_book_details($pid)
{
if(!$pid||$pid=="")
return false;
$con=connect_db("onlineshop");
$query = "select*from products where pid = '$pid'";
$result = mysql_query($query);
if (!$result)
return false;
$result = mysql_fetch_array($result);
return $result;
}
?>