|  | 
	                
  
    | 
	 
        ทำระบบ shopping cart แล้ว มันเกิด error คือได้ทำระบบ shopping cart แล้ว มันเกิด error     |  
    |  |  
 
              
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลองเปลี่ยนอย่างนี้ดูนะครับ 
 1.เว้นวรรคให้มัน
 
 Code (PHP)
 
 if($cart && array_count_values($cart)) 
 
 
 2.เปลี่ยนซะเลย เป็น and แทนดีก่า
  Code (PHP)
 
 if($cart and array_count_values($cart)) 
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-25 23:18:11 | By :
                            onizike |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลองเช็คค่านี้ดูนะครับ 
 array_count_values($cart)
 
 error มันฟ้องสามารถนับค่าที่เป็น integer กับ string เท่านั้นนะครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-25 23:22:24 | By :
                            onizike |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลองทั้ง 2 วิธีแล้วก็ยังไม่ได้คะ มีวิธีไหนอีกรึป่าวคะ 
 แล้ว error แบบนี้มันหมายความว่าอย่างไรคะ
 
 ขอบคุณค่ะ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-25 23:33:17 | By :
                            nut |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | if($cart&&array_count_values($cart)) ----------------------------- มันฟ้อง error ทีบรรทัดนี้คะ { display_cart($cart,true);
 }
 
 เดาเอาว่าจะเป็น
 if (!empty($cart) && count($cart) > 0){
 display_cart($cart,true);
 }
 
 น่ะครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-25 23:36:13 | By :
                            num |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | พี่หนุ่มสุดยอด  
 จาก Code เดิม
 Code เดิม(PHP)
 
 if($cart&&array_count_values($cart)){ 
   display_cart($cart,true);
} 
 
 
 
 คุณไม่ได้กำหนดเงื่อนไขให้มันอ่ะครับ ตัวอย่างเช่น
 ผมให้ $cart มีค่าเท่ากับ 1 และ array_count_values($cart) มีค่าเท่ากับ 1
 เวลาเขียนออกมามันจะอยู่ในรูปแบบนี้ครับ
 
 Code ตัวอย่าง(PHP)
 
 if (1 && 1 ){
   display_cart($cart,true);
}
 ซึ่ง 1 และ 1 มันไม่มีความหมายเช็คค่าไม่ได้ครับ ไม่เลยไม่เข้าเงื่อนไข IF ครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-25 23:49:29 | By :
                            onizike |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ทำตามที่คุณ  num  บอกปรากฎว่ามันไม่ error แล้วเข้า if นี้หละ แต่มัน ตีค่าเป็น 0 เลยคะ 
 
  
 
 
  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-25 23:50:50 | By :
                            ืnut |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอดูโค้ดข้างใน display_cart หน่อยครับ ลอง var_dump($cart); ออกมาดูด้วยครับ เพื่อดูว่า $cart มีข้อมูลอะไรบ้าง
 
 แต่ดูคร่าวๆ แล้วน่าจะเป็นที่ textbox ยังไม่ได้กำหนดค่า value น่ะครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-26 00:03:15 | By :
                            num |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | คุณหนุ่มคะ  (กรณีที่เลือกสินค้ามาแสดงไม่เป็นไรคำนวณปกติ แต่พอเปลี่ยนจำนวนใน textbox แล้วมันกลับมา clear ค่าใน textbox เป็นค่าว่าง แล้ว error ดังกล่าวข้างต้นคะ ) 
 function display_cart($cart,$change)
 {
 global $items;
 global $total_price;
 echo "<form action='show_cart.php' method='post'>
 <table width='850' cellspacing='1' bordercolor='#000000' cellspacing='0' align='center'>
 <tr align='center' valign='middle' bgcolor='#333333' class='white12'>
 <td width='62' class='white12'> Product ID</td>
 <td width='138' height='35'>PRODUCT PICTURE</td>
 <td width='285'>PRODUCT NAME </td>
 <td> PRICE</td>
 <td> QUATITY</td>
 <td> TOTAL</td>
 <td width='81'> </td>
 </tr>";
 // display product rows
 foreach ($cart as $product_id =>$qty)
 {	 $product=get_product_detail($product_id);
 echo " <tr valign='middle' bgcolor='#C0C0C0'>
 <td align='center' class='black12'><strong class='black12'>".$product[p_id]."</strong></td>
 <td align='center'><a href='p_detail.php?p_id=".$product[p_id]."'><img src='admin/product_images/".$product[p_picname1]."' width='50' height='35' border='0'></a></td>
 <td align='center' class='producttopic'><a href='p_detail.php?p_id=".$product[p_id]."'>". $product[p_name]."</a> </td>
 <td width='80' align='center' class='red12'>". $product[p_price]."</td>
 <td width='80' align='center' class='white11'>";
 if($change==true)
 {   		echo  "<input name=\"$product_id\" type='text' size=1 value=$qty>";}
 else { echo "<font>".$qty."</font>";			}
 echo "</td>";
 echo "<td width='80' align='center'><span class='red12'>".number_format($product['p_price']*$qty,2). "</span></td>
 <td width='81' align='center'><a href=delete_cart.php?p_id=".$product[p_id]."><img src='admin/images/scissors.png' alt='Delete' border='0'/></a>	</td>
 </tr>";
 }		// end 	foreach
 echo "<tr valign='middle' bgcolor='#C0C0C0'>
 <td colspan='4' align='center' class='black12'><b>Product in Cart</b></td>
 <td width='80' align='center' class='white11'>".$items."</td>
 <td width='80' align='center'>".number_format($total_price,2)."</td>
 <td width='81' align='center'></td>
 </tr>
 <tr valign='middle' bgcolor='#000000' class='white11'>
 <td colspan='4' align='right'><strong>Total : </strong></td>
 <td colspan='3' align='center' bgcolor='#191919'><span class='producttopic'><strong>".number_format($total_price,2)." Bath</strong></span></td>
 </tr>
 <tr valign='middle' bgcolor='#000000' class='white11'>
 <td colspan='4' align='right'><strong>Discount Code : </strong></td>
 <td colspan='3' align='center' bgcolor='#191919'><input name='code' type='text' ></td>
 </tr>
 <tr valign='middle' bgcolor='#000000' class='white11'>
 <td colspan='4' align='right'><strong>Merchandise Total :  </strong></td>
 <td colspan='3' align='center' bgcolor='#191919'>
 <input name='code' type='text' value='".number_format(($total_price-($total_price*5/100)),2)."' readonly></td></tr>";
 if($change==true)
 { echo "<tr><td colspan='7'><div  style='text-align:right; padding-right:100px;'>
 <input type='hidden' name='new' value='0'>
 <input type='hidden' name='save' value='true'>
 <input name='' type='submit' value='ºÑ¹·Ö¡¡ÒÃà»ÅÕè¹á»Å§'></div></td></tr>";
 }
 echo "</table></form>";
 } // end function display_cart
 
 
 
 function get_product_detail($product_id)
 {
 if(!$product_id  || $product_id==''){
 return false;	}
 include ("_conn/conn_db.php");
 $sql="select * from product where p_id='$product_id' ";
 mysql_query("SET NAMES 'tis620' ");
 $query=mysql_query($sql);
 if($query=='') {
 return false;  }
 $result=mysql_fetch_array($query);
 return $result;
 } // // end function get_product_detail;
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-26 10:43:12 | By :
                            nut |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอโค้ดส่วน update ด้วยครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-26 11:23:59 | By :
                            num |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | // ส่วนที่เรียกมาโชว์คะ <?
 include ("_include/function.php");
 include ("_conn/conn_db.php");
 $new=$_GET['new'];
 if(empty($cart))  $cart=NULL;
 if(empty($index))  $index=NULL;
 if(empty($new))  $new=NULL;
 if($new)
 {
 if(!session_is_registered('cart'))		//session_is_registered('cart')
 {
 $cart = array();
 session_register('cart');
 $items = 0;
 $total_price = "0.00";
 session_register('items');
 session_register('total_price');
 }
 if($cart[$new])	{
 $cart[$new]++;
 }
 else    {
 $cart[$new]=1;		  /// ให้  qty เริ่มต้น=1 ใน textbox
 $total_price=calculate_price($cart) ;
 $items=calculate_items($cart);
 }
 }// end if($new)
 else
 {
 if($save)
 {   echo "มีการเปลี่ยนแปลงจำนวน<br>";
 print "<pre>";
 print_r($_POST);
 print "<pre>";
 foreach($cart as $product_id => $qty)
 {    echo "$cart as $product_id=>$qty <br>";
 if($$product_id=="0")
 { 	unset($cart[$product_id]);
 }
 else
 { 	$cart[$product_id]=$$product_id ;
 }
 }
 $total_price=calculate_price($cart);
 $items=calculate_items($cart);
 }  // end if($save)
 }// end else
 
 include ("headshopcart.php");
 if($cart&&array_count_values($cart))
 {  	display_cart($cart,true);
 
 }
 else
 {	echo "<br></br><center><b> ไม่มีรายการสินค้าในตระกร้า</b></center><br></br><br></br>";
 }
 
 ?>
 
 
 ////////////////////////
 ส่วนของฟังกชันที่ใช้งานคะ
 <? function calculate_price($cart)
 {
 $price=0.0;
 if(is_array($cart))
 {
 include ("_conn/conn_db.php"); //$conn=connect_db("golf");
 foreach($cart as $product_id =>$qty)
 { 	$query="select * from product where p_id='$product_id'";
 $result=mysql_query($query);
 if($result)
 {
 $item_price=mysql_result($result,0,"p_price");
 $price+=$item_price*$qty;
 }  // end if
 }// end foreach
 } //if
 return $price;
 }  // end function calculate_price
 
 
 
 function calculate_items($cart)
 {  // order Total
 $items=0.0;
 if(is_array($cart))
 {
 foreach($cart as $product_id =>$qty)
 {
 $items+=$qty;
 }
 }
 return $items;
 }  //end function calculate_items
 
 
 
 
 function display_cart($cart,$change)
 {
 global $items;
 global $total_price;
 echo "<form action='show_cart.php' method='post'>
 <table width='850' cellspacing='1' bordercolor='#000000' cellspacing='0' align='center'>
 <tr align='center' valign='middle' bgcolor='#333333' class='white12'>
 <td width='62' class='white12'> Product ID</td>
 <td width='138' height='35'>PRODUCT PICTURE</td>
 <td width='285'>PRODUCT NAME </td>
 <td> PRICE</td>
 <td> QUATITY</td>
 <td> TOTAL</td>
 <td width='81'> </td>
 </tr>";
 // display product rows
 foreach ($cart as $product_id =>$qty)
 {	$product=get_product_detail($product_id);
 echo " <tr valign='middle' bgcolor='#C0C0C0'>
 <td align='center' class='black12'><strong class='black12'>".$product[p_id]."</strong></td>
 <td align='center'><a href='p_detail.php?p_id=".$product[p_id]."'><img src='admin/product_images/".$product[p_picname1]."' width='50' height='35' border='0'></a></td>
 <td align='center' class='producttopic'><a href='p_detail.php?p_id=".$product[p_id]."'>". $product[p_name]."</a> </td>
 <td width='80' align='center' class='red12'>". $product[p_price]."</td>
 <td width='80' align='center' class='white11'>";
 if($change==true)
 {   		echo  "<input name=\"$product_id\" type='text' size=1 value=$qty>";}
 else { echo "<font>".$qty."</font>";			}
 echo "</td>";
 echo "<td width='80' align='center'><span class='red12'>".number_format($product['p_price']*$qty,2). "</span></td>
 <td width='81' align='center'><a href=delete_cart.php?p_id=".$product[p_id]."><img src='admin/images/scissors.png' alt='Delete' border='0'/></a>	</td>
 </tr>";
 }		// end 	foreach
 echo "<tr valign='middle' bgcolor='#C0C0C0'>
 <td colspan='4' align='center' class='black12'><b>Product in Cart</b></td>
 <td width='80' align='center' class='white11'>".$items."</td>
 <td width='80' align='center'>".number_format($total_price,2)."</td>
 <td width='81' align='center'></td>
 </tr>
 <tr valign='middle' bgcolor='#000000' class='white11'>
 <td colspan='4' align='right'><strong>Total : </strong></td>
 <td colspan='3' align='center' bgcolor='#191919'><span class='producttopic'><strong>".number_format($total_price,2)." Bath</strong></span></td>
 </tr>
 <tr valign='middle' bgcolor='#000000' class='white11'>
 <td colspan='4' align='right'><strong>Discount Code : </strong></td>
 <td colspan='3' align='center' bgcolor='#191919'><input name='code' type='text' ></td>
 </tr>
 <tr valign='middle' bgcolor='#000000' class='white11'>
 <td colspan='4' align='right'><strong>Merchandise Total :  </strong></td>
 <td colspan='3' align='center' bgcolor='#191919'>
 <input name='discout_code' type='text' value='".number_format(($total_price-($total_price*5/100)),2)."' readonly></td></tr>";
 if($change==true)
 { echo "<tr><td colspan='7'><div  style='text-align:right; padding-right:100px;'>
 <input type='hidden' name='new' value='0'>
 <input type='hidden' name='save' value='true'>
 <input name='' type='submit' value='บันทึกการเปลี่ยนแปลง'></div></td></tr>";
 }
 echo "</table></form>";
 } // end function display_cart
 
 
 
 function get_product_detail($product_id)
 {
 if(!$product_id  || $product_id==''){
 return false;	}
 include ("_conn/conn_db.php");
 $sql="select * from product where p_id='$product_id' ";
 mysql_query("SET NAMES 'tis620' ");
 $query=mysql_query($sql);
 if($query=='') {
 return false;  }
 $result=mysql_fetch_array($query);
 return $result;
 } // // end function get_product_detail;
 ?>
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-26 11:47:24 | By :
                            nut |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ตัวแปร $$product_id มีค่า null ครับ เปลี่ยนไปใช้ตัวแปรข้างใน $_POST แทนครับ ลองแล้วใช้ได้แล้วครับ
 
 
 <?
include ("_include/function.php");
include ("_conn/conn_db.php");
$new=$_GET['new'];
if(empty($cart)) $cart=NULL;
if(empty($index)) $index=NULL;
if(empty($new)) $new=NULL;
if($new)
{
    if(!session_is_registered('cart')) //session_is_registered('cart')
    {
        $cart = array();
        session_register('cart');
        $items = 0;
        $total_price = "0.00";
        session_register('items');
        session_register('total_price');
    }
    if($cart[$new]) {
        $cart[$new]++;
    } else {
        $cart[$new]=1; /// ให้ qty เริ่มต้น=1 ใน textbox
        $total_price=calculate_price($cart) ;
        $items=calculate_items($cart);
    }
}// end if($new)
else
{
    if($save)
    {
        echo "มีการเปลี่ยนแปลงจำนวน<br>";
        print "<pre>";
        print_r($_POST);
        print "<pre>";
        foreach($cart as $product_id => $qty)
        { echo "$cart as $product_id=>$qty <br>";
            $p_qty = $_POST[$product_id];
            if($p_qty == "0")
            { unset($cart[$product_id]);
            }
            else
            { $cart[$product_id] = $p_qty  ;
            }
        }
        $total_price=calculate_price($cart);
        $items=calculate_items($cart);
    } // end if($save)
}// end else
include ("headshopcart.php");
if(!empty($cart))
{
    display_cart($cart,true);
}
else
{ echo "<br></br><center><b> ไม่มีรายการสินค้าในตระกร้า</b></center><br>< /br><br></br>";
}
?>
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-26 13:17:17 | By :
                            num |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบพระคุณมากเลยคะ สรุปคือมันหาค่า $product_id ที่ส่งมาจาก display_cart ไม่เจอ เราต้องทำการรับค่า Post มาก่อนใช่ไหมคะ 
 โล่งอกไปทีคะ ขอบคุณอีกรอบคะ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2009-10-26 13:35:12 | By :
                            nut |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |