Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > ต้องการเปลี่ยน code เขียน mysqli ในตะกร้าสินค้าค่ะ



 

ต้องการเปลี่ยน code เขียน mysqli ในตะกร้าสินค้าค่ะ

 



Topic : 136691



โพสกระทู้ ( 1 )
บทความ ( 0 )



สถานะออฟไลน์




สวัสดีค่ะ มือใหม่เริ่มเรียนรู้การเขียนโค้ด ค่ะ ได้โหลด โค้ด ตระกร้าสินค้า ใน thaicreate ไปลองปรับแก้ เป็น mysqli ค่ะ แต่หน้านี้แก้ไม่เป็นค่ะ อ่านการรับค่า แล้วไม่เข้าใจค่ะ รบกวนช่วยดูและแนะนำหน่อยได้ไหมค่ะ หน้าคิดเงินรายการสินค้าไม่มาค่ะ ขอบคุณค่ะ



หน้าสินค้า
Code (PHP)
<?
ob_start();
//============ Start Session และทำการเรียก Function ติดต่อฐานข้อมูล 
session_start();
require("connect/connect.php");
require("connect/function.php");

$ProductID = $_GET["ProductID"];
echo $ProductID;
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>::: CROCHET-PATTERN.COM :::</title>
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body topmargin="0">
<table width="778" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr> 
          <td>
            <?php require_once('head.php'); ?>
          </td>
        </tr>
        <tr> 
          <td><table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#f2f2f6" bgcolor="#FFFFFF">
              <tr> 
                <td width="23%" valign="top"> <?php require_once('menuleft.php'); ?> <br> </td>
                <td width="54%" valign="top"> 
                  <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <!--DWLayoutTable-->
                    <tr> 
                      <td width="417" height="310" valign="top"> 
                        <table width="418" height="22" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">
                          <tr> 
                            <td width="8"> <img src="image/menu01.jpg" width="8" height="22" alt=""></td>
                            <td width="389" background="image/menu02.jpg"><div align="center"><strong>
                                <?
							  	//============   อ่านว่าเป็นหมวดหมุ่ใด
$resultHeadPro=select("category","where 1=1 and CatID='".$_GET["CatID"]."'");
if(!$resultHeadPro)
{
header("location:index.php");
}
echo "(".$resultHeadPro["CategoryName"].")";

							  	//============   อ่านว่าเป็นรายการสินค้าชนิดใด
$resultDetail=select("product","where 1=1 and ProductID='".$_GET["ProductID"]."'");
if(!$resultDetail)
{
header("location:index.php");
}
echo " / (".$resultDetail["ProductCode"]." - ".$resultDetail["ProductName"].")";

?>
                                </strong> </div></td>
                            <td width="9"> <img src="image/menu03.jpg" width="9" height="22" alt=""></td>
                          </tr>
                        </table> 
                        <table width="100%" border="0">
                          <tr> 
                            <td>&nbsp;</td>
                            <td colspan="2">&nbsp;</td>
                          </tr>
                          <tr> 
                            <td width="24">&nbsp;</td>
                            <td colspan="2"><img src="picture/<?=$resultDetail["Picture"];?>" width="350" /></td>
                          </tr>
                          <tr> 
                            <td colspan="3"><hr width="96%" color="#f2f2f6"></td>
                          </tr>
                          <tr> 
                            <td>&nbsp;</td>
                            <td width="200" valign="top"><strong>รหัสสินค้า</strong> 
                              : 
                              <?=$resultDetail["ProductCode"];?>
                              <br>
                              <br>
                              <strong><img src="image/ed_image.gif" width="18" height="18" align="absmiddle"></strong><a href="send_contact.php?S=สอบถามสินค้า [<?=$resultDetail["ProductCode"];?> - <?=$resultDetail["ProductName"];?>]">สอบถามรายละเอียด</a></td>
                            <td width="181">ปกติ 
                              <?="(<s>".number_format($resultDetail["PriceNormal"], 2,'.',',')."</s>";?>
                              บาท)<strong> <br>
                              <font color="#FF0000">พิเศษ</font></strong> : 
                              <?="(".number_format($resultDetail["Price"], 2,'.',',');?>
                              บาท) <br>
                              ค่าจัดส่ง 
                              <?="(".number_format($resultDetail["PriceSend"], 2,'.',',');?>
                              บาท) </td>
                          </tr>
                          <tr> 
                            <td>&nbsp;</td>
                            <td><strong>ชื่อสินค้า</strong>&nbsp;&nbsp; : 
                              <?=$resultDetail["ProductName"];?>
                            </td>
                            <td><a href="cart.php?ProductID=<?=$resultDetail["ProductID"];?>"><img src="image/shopping.gif" width="36" height="36" border="0" align="absmiddle"></a> 
                              <font color="#FF0000"><strong>สั่งซื้อ</strong></font></td>
                          </tr>
                          <tr> 
                            <td>&nbsp;</td>
                            <td colspan="2"><strong>รายละเอียด</strong>&nbsp;&nbsp; 
                              :</td>
                          </tr>
                          <tr> 
                            <td>&nbsp;</td>
                            <td colspan="2"> 
                              <?=$resultDetail["Description"];?>
                            </td>
                          </tr>
                          <tr> 
                            <td>&nbsp;</td>
                            <td colspan="2">&nbsp;</td>
                          </tr>
                        </table>
                        <hr width="96%" color="#f2f2f6">
                        <p class="style59">&nbsp;</p>
                        <p class="style59">&nbsp;</p>
                        <p class="style59">&nbsp;</p></td>
                    </tr>
                  </table></td>
                <td width="23%" valign="top"> <?php require_once('menuright.php'); ?> </td>
              </tr>
              <tr> 
                <td colspan="3" valign="top">
                  <?php require_once('down.php'); ?>
                </td>
              </tr>
            </table></td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>





หน้าจ่ายเงิน
Code (PHP)
<?
ob_start();
//============ Start Session และทำการเรียก Function ติดต่อฐานข้อมูล 
session_start();
require("connect/connect.php");
require("connect/function.php");

?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>::: CROCHET-PATTERN.COM :::</title>
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body topmargin="0">
<table width="778" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr> 
          <td>
            <?php require_once('head.php'); ?>

<?

$_GET["ProductID"] = isset ($_GET["ProductID"]) ? $_GET["ProductID"] : '';
$_GET["action"]= isset ($_GET["action"]) ? $_GET["action"] : '';
$_POST["action"] = isset($_POST["action"]) ? $_POST["action"] : '';
$_SESSION["strP"] = isset ($_SESSION["strP"]) ? $_SESSION["strP"] :'';
$_SESSION["strProductID"] = isset($_SESSION["strProductID"]) ? $_SESSION["strProductID"] : '';
$ProductID = isset($ProductID) ? $ProductID:'';

echo '$_GET[\'ProductID\']='.$_GET['ProductID'].'';


//============ Check ว่ามีซ้ำแล้วหรือยัง
if($_GET["ProductID"] != "")
{
	for($i=0;$i<=count($_SESSION["strProductID"]);$i++)
	{
			if($_SESSION["strProductID"][$i] == $_GET["ProductID"])
			{
			header("location:$_SERVER[PHP_SELF]");
			exit();
			}
	}
}


//============ ถ้ามีการเลือกรายการสินค้าให้เก็บลง Session
if(trim($_GET["ProductID"]) != "")
{
	$_SESSION["strP"] = $_SESSION["strP"] . +1;
	$_SESSION["strProductID"][$_SESSION["strP"]]=$_GET["ProductID"];
	$_SESSION["strQuanlity"][$_SESSION["strP"]]=1;
	session_write_close();
	header("location:$_SERVER[PHP_SELF]");
}	

//============ กรณีลบรายการสั่งซื้อ
if(trim($_GET["action"]) == "Del")
{
	$_SESSION["strProductID"][$_GET[P]]="";
	$_SESSION["strQuanlity"][$_GET[P]]="";
	session_write_close();
	header("location:$_SERVER[PHP_SELF]");
}	

//============ กรณีการแก้ไขรายการสั่งซื้อ
if(trim($_POST["action"]) == "Update")
{
for($i=0;$i<count($_POST["txtQua"]);$i++)
{	
	$strP=$_POST[txtP][$i];
	$strQ=$_POST["txtQua"][$i];
	if($strQ <= 0)
	{
	$strQ=1;
	}

	$_SESSION["strQuanlity"][$strP]=$strQ;
/*
	$result=select("product","where 1=1 and ProductID='".$_SESSION["strProductID"][$strP]."'");
	if($result["Stock"] < $strQ)
	{
	$_SESSION["strQuanlity"][$strP]=$result["Stock"];
									echo"<script language='JavaScript'>";
									echo"alert('รหัสสินค้า [ $result[ProductCode] ] มีจำนวน Stock ไม่เพียงพอ');";
									echo"</script>";	
	}
*/	
	
}	
	session_write_close();
	//============ กระโดด Refresh ใหม่อีกรอบ
	header("location:$_SERVER[PHP_SELF]");
}	
?>			
          </td>
        </tr>
        <tr> 
          <td><table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#f2f2f6" bgcolor="#FFFFFF">
              <tr> 
                <td width="23%" valign="top"> <?php require_once('menuleft.php'); ?> <br> </td>
                <td width="54%" valign="top"> 
                  <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <!--DWLayoutTable-->
                    <tr> 
                      <td width="417" height="310" valign="top">
                        <form action="" method="post" name="frmCart" id="frmCart">
                          <table width="415" border="0">
                            <tr> 
                              <td><table width="418" height="22" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">
                                  <tr> 
                                    <td width="8"> <img src="image/menu01.jpg" width="8" height="22" alt=""></td>
                                    <td width="389" background="image/menu02.jpg"><div align="center"><strong>รายการสั่งซื้อ 
                                        <input name="action" type="hidden" id="action" value="Update">
                                        </strong></div></td>
                                    <td width="9"> <img src="image/menu03.jpg" width="9" height="22" alt=""></td>
                                  </tr>
                                </table>
                                <strong> </strong></td>
                            </tr>
                            <tr> 
                              <td>&nbsp;</td>
                            </tr>
                            <tr> 
                              <td><table width="98%" height="21" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#666666">
                                  <tr bgcolor="#FFFFFF"> 
                                    <td><div align="center">ลำดับ</div></td>
                                    <td>ชื่อสินค้า</td>
                                    <td><div align="center">ราคา</div></td>
                                    <td>ค่าจัดส่ง</td>
                                    <td><div align="center">จำนวน</div></td>
                                    <td><div align="center">รวม</div></td>
                                  </tr>
                                  <?

//============ ทำการแสงรายการที่ได้เลือกว่ามีกี่รายการ
unset($_SESSION["strProductID"]);
$_SESSION["strProductID"] = isset ($_SESSION["strProductID"]) ? $_SESSION["strProductID"] : array();

if( $_SESSION["strProductID"] == ''){
	$_SESSION["strProductID"] = array();
}

$strNum=0;
$strTotal=0;
for($i=0;$i<=count($_SESSION["strProductID"]);$i++)
{
	
	if(isset($_SESSION["strProductID"]["$i"])){
		//============ เลือกว่ารายการสินค้าใดบ้าง
		$result=select("product","where 1=1 and ProductID='".$_SESSION["strProductID"]["$i"]."' ");
		if($result)
		{
		$strNum++;
?>
                                  <tr bgcolor="#FFFFFF"> 
                                    <td width="9%"> <div align="center"> 
                                        <?=$strNum;?>
                                        <a href="javascript:if(confirm('ลบรายการสินค้า')==true){window.location='<?=$_SERVER['PHP_SELF'];?>?action=Del&P=<?=$i;?>';}"><img src="image/deleteC.gif" width="12" height="11" border="0"></a></div></td>
                                    <td width="48%"> 
                                       <?=$result["ProductName"];?>
                                    </td>
                                    <td width="14%"> <div align="right"> 
                                        <?="".number_format($result["Price"], 2,'.',',');?>
                                      </div></td>
                                    <td width="13%"> 
                                      <div align="right">
                                        <?="".number_format($result["PriceSend"], 2,'.',',');?>
                                      </div></td>
                                    <td width="13%"><div align="center"> 
                                        <input name="txtP[]" type="hidden" id="txtP[]" value="<?=$i;?>">
                                        <input name="txtQua[]" type="text" id="txtQua<?=$strNum;?>" style="width:30px;text-align:right" value="<?=$_SESSION["strQuanlity"]["$i"];?>" maxlength="3" onChange="checknum(this.value,'txtQua<?=$strNum;?>');">
                                      </div></td>
                                    <td width="16%"> <div align="right"> 
                                        <?="".number_format(($result["Price"]+$result["PriceSend"])*$_SESSION["strQuanlity"]["$i"], 2,'.',',');?>
                                      </div></td>
                                  </tr>
                                  <?
								  $strTotal=$strTotal+(($result["Price"]+$result["PriceSend"])*$_SESSION["strQuanlity"]["$i"]);
		}
	}	
}
?>
                                </table>
                                <br> <table width="98%" height="21" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#666666">
                                  <tr bgcolor="#FFFFFF"> 
                                    <td><div align="right">จำนวน (รายการ)</div></td>
                                    <td> <div align="right"> 
                                        <?=$strNum;?>
                                      </div></td>
                                  </tr>
                                  <tr bgcolor="#FFFFFF"> 
                                    <td> <div align="right">ราคารวม (บาท)</div></td>
                                    <td width="16%"> <div align="right"> 
                                        <?="".number_format($strTotal, 2,'.',',');?>
                                      </div></td>
                                  </tr>
                                </table></td>
                            </tr>
                            <tr> 
                              <td width="378"><div align="center"><br>
                                  <?
							if($strNum==0)
							{
							echo "<br><center><strong><font color=red>ไม่มีรายการสั่งซื้อ</font></strong></center><br><br>";
							}
							?>
                                  <input name="BtnContinute" type="button" class="button" id="BtnContinute2" value="ซื้อสินค้าต่อ" onClick="window.location='index.php';">
                                  &nbsp; 
                                  <?
								  if($strNum>0)
								  {?>
                                  <input name="BtnCalu" type="submit" class="button" id="BtnCalu" value="คำนวณเงินใหม่">
                                  &nbsp; 
                                  <input name="BtnPayment" type="button" class="button" id="BtnPayment" value="ชำระเงิน" onClick="window.location='pay.php';">
                                  <?}?>
                                  <br>
                                </div></td>
                            </tr>
                          </table>
                          <p class="style59">&nbsp;</p>
                        </form>
                        <p class="style59">&nbsp;
						<script language="JavaScript">
						function checknum(value,fields)
						{
						if(!check_number(value))
						{
						eval("document.all."+fields+".value='1';");
						alert('กรอกจำนวนไม่ถูกต้อง');
						}
						}
						
						function check_number(ch){
							var len, digit;
							if(ch == " "){ 
								return false;
									 len=0;
								}else{
									 len = ch.length;
							}
							for(var i=0 ; i<len ; i++)
							{
								digit = ch.charat(i)
								if(digit >="0" && digit <="9"){
									;	
								}else{
									return false;					
								}		
							}		
							return true;
						}						
						</script></p>
                        <p class="style59">&nbsp;</p>
                        <p class="style59">&nbsp;</p></td>
                    </tr>
                  </table></td>
                <td width="23%" valign="top"> <?php require_once('menuright.php'); ?> </td>
              </tr>
              <tr> 
                <td colspan="3" valign="top">
                  <?php require_once('down.php'); ?>
                </td>
              </tr>
            </table></td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>





Tag : PHP, MySQL, HTML







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2022-05-14 12:49:05 By : หอมน้ำ View : 786 Reply : 31
 

 

No. 1



โพสกระทู้ ( 3,843 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์
Twitter Facebook

ถ้าแก้จุดก่อนนี้ได้ หน้านี้ลองแก้มาก่อนมั้ยครับ จะได้ช่วยดู

ขอแนะนำอีกนิดนะครับ ยังไง ลองไปศึกษา php framework นะครับ จะช่วยให้เราได้วิธีการ coding ใหม่ และดูง่ายครับ
เริ่มจาก Codeigniter ก็ได้ครับ เพราะ code ใน thaicreate ส่วนใหญ่เกิน 10 ปี function, command หลายตัวมัน deprecated ไปแล้วครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-17 10:32:06 By : mongkon.k
 


 
ตอบความคิดเห็นที่ : 1 เขียนโดย : mongkon.k เมื่อวันที่ 2022-05-17 10:32:06
รายละเอียดของการตอบ ::
สวัสดีค่ะ ลองแก้แล้วค่ะ ตาม error ใส่ isset ตอนนี้

มันมี error นี้มาค่ะ
_GET['ProductID']=20
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given in C:\xampp\htdocs\example\tukataScript\cart.php:39 Stack trace: #0 {main} thrown in C:\xampp\htdocs\example\tukataScript\cart.php on line 39



for คือ บันทัดที่39 ค่ะ ลองหาใน google แต่ยังแก้ไม่ได้ค่ะ รบกวนช่วยดูหน่อยได้ไหมค่ะ ต้องแก้อย่างไรค่ะ

Code (PHP)
if($_GET["ProductID"] != "")
{
	for($i=0; $i <= count(!is_array($_SESSION["strProductID"])); $i++)
	{
		
			if($_SESSION["strProductID"][$i] == $_GET["ProductID"])
			{
				
			header("location:$_SERVER[PHP_SELF]");
			exit();
			}
			
	}
	
}




แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-17 17:28:17 By : หอมน้ำ
 

 
Code (PHP)
$productID = isset($_GET['ProductID']) ? trim($_GET['ProductID']) : '';
...
...
if ($productID != "") {
 //Statement
}


เหมือนคุณจะยังไม่เข้าใจCodeนะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-18 10:09:20 By : Guest
 


 

No. 4



โพสกระทู้ ( 4,169 )
บทความ ( 7 )

Hall of Fame 2012

สถานะออฟไลน์


จากประสบการณ์ด้าน eCommerce สำหรับระบบ Cart หรือตระกร้าสินค้าแนะนำให้เขียนเป็น Demo ก่อนครับจะได้เข้าใจ Logic ซึ่งคุณไม่จำเป็นต้อง Referance กับ Database แต่สามารถทำเป็น array raw data ก็ได้ครับ function method ที่สำคัญมันจะมีแค่
- add cart ซึ่งจำเป็นต้องใช้ session เป็นตัว main โดย condition จะตรวจสอบว่า ถ้าไม่มี array key นี้ใน session cart ให้ทำการเพิ่มใน session แต่ถ้ามี key อยู่แล้วให้ทำการ update array session ซึ่งอาจจะมีเรื่อง quantity เข้ามาผสม
- update cart ใช้สำหรับการ update จำนวนในตระกร้า โดยอ้างอิงจาก key ที่ทำการส่งพร้อมกับจำนวน ลงใน array key ของ quantity
- delete cart ใช้สำหรับการ ลบสินค้าในตระกร้า โดย focus เดียวคือนำ key ที่ส่งมาเช็คกับ session array key ที่มีอยู่ ถ้าตรงกันทำให้เป็นค่าว่าง
การเพิ่ม order แนะนำให้เป็นการ dump ข้อมูล session นั้นเป็น serialize ในการนำเข้า และ unserialize ในการแสดงผล ข้อดีของการทำในลักษณะนี้คือ order แต่ละตัวจะมีแค่ row เดียว และไม่ต้องวุ่นวายกับการ FK ครับ

ปล. ทำความเข้าใจ code ตัวอย่าง, อ่าน code ให้เข้าใจทุกบรรทัด, นำมาเขียนเป็น style ของตัวเอง (นั่นคือคุณสมบัติของ Developer ที่ควรจะมีครับ)


ประวัติการแก้ไข
2022-05-18 17:47:57
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-18 17:43:04 By : Genesis™
 


 
ตอบความคิดเห็นที่ : 3 เขียนโดย : Guest เมื่อวันที่ 2022-05-18 10:09:20
รายละเอียดของการตอบ ::
ใช่ค่ะ เพิ่งลองทำค่ะ

ลองแก้ตามแล้วค่ะ
Code (PHP)
$ProductID = isset($_GET["ProductID"]) ? trim($_GET["ProductID"]) :'';
if($ProductID != "") 
{
	for($i=0; $i <= count(!is_array($_SESSION["strProductID"])); $i++)
	{
		
			if($_SESSION["strProductID"][$i] == $_GET["ProductID"])
			{
			header("location:$_SERVER[PHP_SELF]");
			exit();
	
			}
			
	}
	
}


ยังมี error นี้ ยังแก้ไม่ได้เลยค่ะ ต้องแก้อย่างไรค่ะ
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given in C:\xampp\htdocs\example\tukataScript\cart.php:40 Stack trace: #0 {main} thrown in C:\xampp\htdocs\example\tukataScript\cart.php on line 40


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-18 19:07:32 By : หอมน้ำ
 


 
ตอบความคิดเห็นที่ : 4 เขียนโดย : Genesis™ เมื่อวันที่ 2022-05-18 17:43:04
รายละเอียดของการตอบ ::
ตอบความคิดเห็นที่ : 4 เขียนโดย : Genesis™ เมื่อวันที่ 2022-05-18 17:43:04
รายละเอียดของการตอบ ::
ขอบคุณสำหรับคำแนะนำค่ะ จะลองพยามทำความเข้าใจให้มากขึ้นค่ะ


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-18 19:23:15 By : หอมน้ำ
 


 

No. 7



โพสกระทู้ ( 4,706 )
บทความ ( 8 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 5 เขียนโดย : หอมน้ำ เมื่อวันที่ 2022-05-18 19:07:32
รายละเอียดของการตอบ ::
จะใช้อะไรก็หาเอกสารของมันมาดู
https://www.php.net/manual/en/function.count.php
https://www.php.net/manual/en/function.in-array.php

ลองดูว่าแต่ละอันจะใส่อะไรและ return อะไร แล้วเราใช้ถูกมั้ย?

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-18 19:46:38 By : mr.v
 


 
Code (PHP)
count(!is_array($_SESSION["strProductID"])) //แปลกๆนะ

echo count(FALSE); //result = 1

echo count(TRUE); //result = 1


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-19 08:01:20 By : Guest
 


 

No. 9



โพสกระทู้ ( 9,542 )
บทความ ( 2 )



สถานะออฟไลน์


น่าจะมือใหม่ซิงๆ
แนะนำครับ โค๊ดไหนไม่รู้จัก ใช้ google ให้เป็นประโยชน์ครับ

คำค้น
php --function/command ที่ต้องการเรียนรู้--

ไม่ต้องใส่ [[--]] ทั้งหน้าและหลังนะครับ (เคยเจอพวกเถรตรง 55555)
จะมีคำอธิบายให้เราเข้าใจ ถ้าอ่านภาษาอังกฤษไม่ออก ก็ใช้ google translate ช่วย ไม่100% ก็เยอะอยู่น่าจะทำความเข้าใจได้

และ getting start มีความสำคัญมาก จะช่วยทำให้เราเข้าใจ ไวยกรณ์พื้นฐาน ได้มาก
ไวยกรณ์พื้นฐานสำคัญมากๆ ทำให้เราสามารถเขียน function class ต่างๆ ได้เอง โดยไม่ต้องพึง โค๊ดสำเร็จ อะไรเลย
(แต่ถ้ารู้จักใช้โค๊ดสำเร็จยิ่งช่วยให้เราทำงานได้เร็วขึ้น)
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-19 08:18:01 By : Chaidhanan
 


 
ตอบความคิดเห็นที่ : 7 เขียนโดย : mr.v เมื่อวันที่ 2022-05-18 19:46:38
รายละเอียดของการตอบ ::
หนูลองใส่แล้วค่ะ หนูน่าจะใส่ผิด มันยัง error อันเดิมเลยค่ะ ต้องแก้อย่างไรค่ะ

Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given in C:\xampp\htdocs\example\tukataScript\cart.php:41 Stack trace: #0 {main} thrown in C:\xampp\htdocs\example\tukataScript\cart.php on line 41

Code (PHP)
$ProductID = isset($_GET["ProductID"]) ? trim($_GET["ProductID"]) :'';
if($ProductID != "") 
{
	
	$arr_length = count($_SESSION["strProductID"]);
	for($i=0; $i <= $arr_length; $i++)
	{
			if($_SESSION["strProductID"][$i] == $_GET["ProductID"])
			{				
			header("location:$_SERVER[PHP_SELF]");
			exit();	
			}
			return $i;
	}
	
}


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-19 18:57:06 By : หอมน้ำ
 


 
ตอบความคิดเห็นที่ : 8 เขียนโดย : Guest เมื่อวันที่ 2022-05-19 08:01:20
รายละเอียดของการตอบ ::

หนูต้องเอา ไปใส่ตรงไหนค่ะ ขอโทษค่ะหนูไม่เข้าใจ
echo count(FALSE); //result = 1
echo count(TRUE); //result = 1


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-19 18:58:26 By : หอมน้ำ
 


 
ตอบความคิดเห็นที่ : 9 เขียนโดย : Chaidhanan เมื่อวันที่ 2022-05-19 08:18:01
รายละเอียดของการตอบ ::
ใช่ค่ะใหม่มากค่ะ หนูมีข้อเสียคืออ่านไม่เข้าใจ เลยลงมือทำเลย เลยเอาโค้ทมาลองแก้ดูค่ะ หนูจะพยามศึกษาให้มากขึ้นค่ะ ขอบคุณนะคะ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-19 19:04:44 By : หอมน้ำ
 


 
วิธีแก้ปัญหาระยะยาว คือ ศึกษาพื้นฐานให้เข้าใจก่อน ที่ จขกท นำมาแสดงไม่ใช่พื้นฐานสำหรับคนที่เริ่มต้นจากศูนย์

การแก้ไขปัญหาเฉพาะหน้า นำลิงค์ที่มาของโค้ดมาแปะและบอกความต้องการเป็นภาษาคน หรือจะให้ดีถามเป็นข้อๆในส่วนที่ไม่เข้าใจ อย่ามักง่าย เพราะคุณจะไม่ได้อะไรนอกไปจากทักษะการคัดลอกวาง ศาสตร์นี้ไม่เน้นจำ แต่ต้องเข้าใจขั้นตอนการทำงาน และความต่างของแวดล้อมจึงจะแก้ปัญหาได้

เมื่อสะดวกจะมาขยายความ คห 8
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-19 19:57:04 By : 009
 


 
ตอบความคิดเห็นที่ : 11 เขียนโดย : หอมน้ำ เมื่อวันที่ 2022-05-19 18:58:26
รายละเอียดของการตอบ ::
Code (PHP)
for($i=0; $i <= count(!is_array($_SESSION["strProductID"])); $i++) //บรรทัดนี้

//จุดนี้
count(!is_array($_SESSION["strProductID"])) // ยังไงค่ามันก็ได้เท่ากับ 1

!is_array //return เป็น TRUE หรือ FALSE

//เอามาพิสูจน์ให้ดูว่า COUNT->TRUE หรือ FALSE ยังไงก็เท่ากับ 1
echo count(FALSE); //result = 1 
echo count(TRUE); //result = 1

for($i=0; $i <= 1; $i++) //ดังนั้นจึงได้ค่านี้เสมอ


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-20 10:41:04 By : Guest
 


 

No. 15



โพสกระทู้ ( 9,542 )
บทความ ( 2 )



สถานะออฟไลน์


จาก คห 14 เพิ่มเติม

ทุก function ที่ขึ้นต้นด้วย is_xxxx() สิ่งที่ได้ จะเป็นค่า BOOLEAN ( true|false) เท่านั้น
เช่น is_null($var) ตัวแปร $var เป็น null ถ้า ใช่ จะได้ค่า true ถ้าไม่ใช่ จะได้ค่า false

is_array($var) ตัวแปร $var มี type เป็น array ถ้า ใช่ จะได้ค่า true ถ้าไม่ใช่ จะได้ค่า false

ส่วนคำสั่ง count($var) หาค่าจำนวน element ของ array กรณี ตัวแปร $var ไม่มี type เป็น array จะได้ ค่า 1 เสมอ
แต่จะมีการ กำหน่ดค่า error ว่า ตัวแปร $var ไม่ใช่ array

ที่นี้ php มีค่าค่าเริ่มต้น ไม่ให้แสดง error จึงได้แค่ค่า 1
ดังนั้นขณะหัดเขียนโปรแกรม ควรเปิดการแสดง error ด้วย
นำคำสั่งนี้ไว้ต้นไฟล์ เพื่อเปิด error เป็นครั้งคราว ถ้าจะปิด ก็ มาร์คเอาไว้
Code (PHP)
<?php 
error_reporting(-1); // กำหนด การแสดงเออเร่อร์ ทุกชนิด
ini_set('display_errors', 1); // กำหนด ให้แสดง เออเร่อร์
// error_reporting(-1); ini_set('display_errors', 1); // ไม่ใช้




ประวัติการแก้ไข
2022-05-20 13:43:38
2022-05-20 13:47:14
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-20 13:41:36 By : Chaidhanan
 


 

No. 16



โพสกระทู้ ( 4,706 )
บทความ ( 8 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 14 เขียนโดย : Guest เมื่อวันที่ 2022-05-20 10:41:04
รายละเอียดของการตอบ ::
วิธีนั้นมันผิด ผมก็ให้ลิ้งค์ไปแล้วใน คห. 7 ก็จะยังไปแนะนำอะไรผิดๆตอกย้ำเข้าไปอีก!

คห. 15 คุณ Chai อธิบายไว้แล้ว จะขออ้างอิงจากลิ้งค์ของ PHP (อีกที)
https://www.php.net/manual/en/function.count.php count()
รับ argument 2 ค่า
ค่าแรกเป็น $value จะต้อง implements Countable interface หรือ array อย่างใดอย่างหนึ่ง!

แล้ว null, false, true มันใช่อย่างใดอย่างหนึ่งในสองอย่างนี้มั้ย?

ค่าที่สองของ count() เป็น $mode ซึ่งค่าเดิมจะเป็น COUNT_NORMAL

เวลาเขียนโค้ด จำเป็นอย่างยิ่งที่จะต้องเปิดแสดง error ด้วย ไม่ใช่อุดปากไว้
แล้วพอมันดูเหมือนจะทำงานได้ก็ตะพึดตะพือใช้มันอยู่อย่างนั้น ไม่คิดจะตรวจอ้างอิงเอกสารให้ถูกต้อง เพราะไม่มี error อะไรแจ้งเตือน
นี่คือข้อเสีย และจะนำไปสู่หายนะที่จะอธิบายต่อไป.

.............................

PHP เป็นภาษาที่เหมือนเด็กไฮเปอร์ คือ ไม่ชอบความนิ่งเสถียรไม่อยู่กับที่ ชอบออกของใหม่แล้วทำลายของเก่าทิ้งเป็นว่าเล่น. เป็นอย่างนี้มาตั้งแต่เท่าที่เริ่มเขียนก็ PHP 5.1+ แล้ว.
ในขณะที่ภาษาอื่นๆบางภาษา คือเขาจะพยายามรักษาของเก่าไว้ ไม่เพิ่มอะไรแต่ก็ไม่ทิ้ง ให้มันสามารถ build/compile/run ข้ามรุ่นได้นิ่มนวลที่สุด เหมาะสำหรับพวกงานที่ต้องเขียนแล้วใช้ยาวๆเพียงแค่ตามอัพเดทและอาจมีแก้ไขนิดหน่อย.

กรณี function count() นี้ ถ้าคุณเปิดแสดง error ใน PHP เก่าๆ เช่น 5.x ไปจนถึง 7.4 คุณจะพบเจอ error แบบนี้
Quote:
Warning: count(): Parameter must be an array or an object that implements Countable

หมายความว่าเป็นข้อผิดพลาดที่อยู่ในระดับแจ้งเตือน (Warning)

ในขณะที่ถ้าคุณอัพเกรดมาใช้ PHP 8.0 ขึ้นไป คุณจะพบเจอ error แบบนี้
Quote:
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given

และ
Quote:
TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given

ซึ่งหมายความว่าเป็นข้อผิดพลาดที่อยู่ในระดับ ร้ายแรง (Fatal) และมันจะหยุดทำงานทันที. ทำงานต่อไปไม่ได้! ต่างจาก PHP รุ่นก่อนหน้าที่อยู่ในระดับแจ้งเตือน จะยังทำงานต่อไปได้อีก.

ทีนี้วกกลับไปเรื่องตัวภาษา PHP
ถ้าคุณเปิดแสดง error จนเป็นปกตินิสัย แล้วรู้ว่า PHP มันไฮเปอร์อย่างที่อธิบาย โดยธรรมชาติคนที่มีประสบการณ์บ้าง (อย่างผม) จะรู้เลยว่ามี Warning แบบนี้ต้องแก้ไขแล้ว เพราะไม่งั้นรุ่นใหม่ๆออกมาเราได้ฉิบหายกันหมดแน่ๆ. เพราะเขาทำแบบนี้มานับครั้งไม่ถ้วนแล้ว.

ถ้าอ่านแต่ต้นจนจบถึงนี่ ก็หวังว่าจะพยายามเปิดแสดง error กันไว้ให้เคยชิน และอย่าพยายามแนะนำอะไรผิดๆให้มันเพิ่มความวิบัติแก่มือใหม่อีก.



ประวัติการแก้ไข
2022-05-20 23:42:36
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-20 23:38:18 By : mr.v
 


 

No. 17



โพสกระทู้ ( 4,169 )
บทความ ( 7 )

Hall of Fame 2012

สถานะออฟไลน์


ตอบความคิดเห็นที่ : 12 เขียนโดย : หอมน้ำ เมื่อวันที่ 2022-05-19 19:04:44
รายละเอียดของการตอบ ::
เยี่ยมครับ สู้ๆ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-21 11:21:53 By : Genesis™
 


 

No. 18



โพสกระทู้ ( 4,706 )
บทความ ( 8 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 10 เขียนโดย : หอมน้ำ เมื่อวันที่ 2022-05-19 18:57:06
รายละเอียดของการตอบ ::
Code (PHP)
count($_SESSION["strProductID"])

Quote:
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given

ถ้าได้ error แบบนี้แปลว่าค่าที่ใส่ลงไปใน count คือ $_SESSION["strProductID"] เป็น string (string given) ไม่ใช่ Countable หรือ array.

ดังนั้นต้องย้อนกลับไปว่า....
ทำไมถึงเอา $_SESSION["strProductID"] มาใช้ มันมาจากไหน มันควรจะเป็น Countable หรือ array มากกว่ามั้ย
หรือ... แทนที่จะใช้ $_SESSION["strProductID"] มันควรจะใช้ตัวอื่นไหมที่มันถูกต้องกว่านี้

edit:


พอค้นย้อนไปเห็นโค้ดบรรทัดนี้
Code (PHP)
$_SESSION["strProductID"] = isset($_SESSION["strProductID"]) ? $_SESSION["strProductID"] : '';

รู้เลยว่าผิด
แก้จาก empty string ('') ไปเป็น empty array ([]) ซะ
Code (PHP)
$_SESSION["strProductID"] = isset($_SESSION["strProductID"]) ? $_SESSION["strProductID"] : [];

แล้วลองใหม่


Edit2:

บรรทัดนี้ด้วย
Code (PHP)
if( $_SESSION["strProductID"] == ''){
	$_SESSION["strProductID"] = array();
}

ในเมื่อมันจะต้องเป็น array แน่ๆอยู่แล้วก็เช็ค type มันไปเลย ไม่ใช่เช็คว่าเป็น empty string
แก้เป็น...
Code (PHP)
if (!is_array($_SESSION["strProductID"])) {
    $_SESSION["strProductID"] = array();
}




ประวัติการแก้ไข
2022-05-22 09:56:50
2022-05-22 10:01:16
2022-05-22 10:02:40
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-22 09:54:36 By : mr.v
 


 
ตอบความคิดเห็นที่ : 18 เขียนโดย : mr.v เมื่อวันที่ 2022-05-22 09:54:36
รายละเอียดของการตอบ ::
Code (PHP)

ได้แก้ไขแล้วนะคะ และแก้ error ไปหมดแล้ว ตอนนี้ไม่มี error และ ไม่มีข้อมูลอะไรโชว์เลยค่ะ หนูต้องแก้อย่างไรค่ะ

<?
ob_start();
//============ Start Session และทำการเรียก Function ติดต่อฐานข้อมูล 
session_start();
require("connect/connect.php");
require("connect/function.php");

error_reporting(-1); // กำหนด การแสดงเออเร่อร์ ทุกชนิด
ini_set('display_errors', 1); // กำหนด ให้แสดง เออเร่อร์
// error_reporting(-1); ini_set('display_errors', 1); // ไม่ใช้
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>::: CROCHET-PATTERN.COM :::</title>
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body topmargin="0">
<table width="778" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr> 
          <td>
            <?php require_once('head.php'); ?>

<?

$_GET["ProductID"] = isset ($_GET["ProductID"]) ? $_GET["ProductID"] : '';
$_GET["action"]= isset ($_GET["action"]) ? $_GET["action"] : '';
$_POST["action"] = isset($_POST["action"]) ? $_POST["action"] : '';
$_SESSION["strP"] = isset ($_SESSION["strP"]) ? $_SESSION["strP"] :'';
$ProductID = isset($ProductID) ? $ProductID:'';



echo '$_GET[\'ProductID\']='.$_GET['ProductID'].'';

//============ Check ว่ามีซ้ำแล้วหรือยัง
$ProductID = isset($_GET["ProductID"]) ? trim($_GET["ProductID"]) :'';
if($ProductID != "") 
{	
	$arr_length = count($_SESSION["strProductID"]);
	for($i=0; $i <= $arr_length; $i++)
	{
			$strProductID = isset($_SESSION["strProductID"][$i]) ? $_SESSION["strProductID"][$i] : '';
			if($strProductID == $ProductID)
			{				
			header("location:$_SERVER[PHP_SELF]");
			exit();	
			}
			return $i;
	}
	
}


//============ ถ้ามีการเลือกรายการสินค้าให้เก็บลง Session
if(trim($_GET["ProductID"]) != "")
{
	$_SESSION["strP"] = $_SESSION["strP"] . +1;
	$_SESSION["strProductID"][$_SESSION["strP"]]=$_GET["ProductID"];
	$_SESSION["strQuanlity"][$_SESSION["strP"]]=1;
	session_write_close();
	header("location:$_SERVER[PHP_SELF]");
}	

//============ กรณีลบรายการสั่งซื้อ
if(trim($_GET["action"]) == "Del")
{
	$_SESSION["strProductID"][$_GET[P]]="";
	$_SESSION["strQuanlity"][$_GET[P]]="";
	session_write_close();
	header("location:$_SERVER[PHP_SELF]");
}	

//============ กรณีการแก้ไขรายการสั่งซื้อ
if(trim($_POST["action"]) == "Update")
{
for($i=0;$i<count($_POST["txtQua"]);$i++)
{	
	$strP=$_POST[txtP][$i];
	$strQ=$_POST["txtQua"][$i];
	if($strQ <= 0)
	{
	$strQ=1;
	}

	$_SESSION["strQuanlity"][$strP]=$strQ;
/*
	$result=select("product","where 1=1 and ProductID='".$_SESSION["strProductID"][$strP]."'");
	if($result["Stock"] < $strQ)
	{
	$_SESSION["strQuanlity"][$strP]=$result["Stock"];
									echo"<script language='JavaScript'>";
									echo"alert('รหัสสินค้า [ $result[ProductCode] ] มีจำนวน Stock ไม่เพียงพอ');";
									echo"</script>";	
	}
*/	
	
}	
	session_write_close();
	//============ กระโดด Refresh ใหม่อีกรอบ
	header("location:$_SERVER[PHP_SELF]");
}	
?>			
          </td>
        </tr>
        <tr> 
          <td><table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#f2f2f6" bgcolor="#FFFFFF">
              <tr> 
                <td width="23%" valign="top"> <?php require_once('menuleft.php'); ?> <br> </td>
                <td width="54%" valign="top"> 
                  <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <!--DWLayoutTable-->
                    <tr> 
                      <td width="417" height="310" valign="top">
                        <form action="" method="post" name="frmCart" id="frmCart">
                          <table width="415" border="0">
                            <tr> 
                              <td><table width="418" height="22" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">
                                  <tr> 
                                    <td width="8"> <img src="image/menu01.jpg" width="8" height="22" alt=""></td>
                                    <td width="389" background="image/menu02.jpg"><div align="center"><strong>รายการสั่งซื้อ 
                                        <input name="action" type="hidden" id="action" value="Update">
                                        </strong></div></td>
                                    <td width="9"> <img src="image/menu03.jpg" width="9" height="22" alt=""></td>
                                  </tr>
                                </table>
                                <strong> </strong></td>
                            </tr>
                            <tr> 
                              <td>&nbsp;</td>
                            </tr>
                            <tr> 
                              <td><table width="98%" height="21" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#666666">
                                  <tr bgcolor="#FFFFFF"> 
                                    <td><div align="center">ลำดับ</div></td>
                                    <td>ชื่อสินค้า</td>
                                    <td><div align="center">ราคา</div></td>
                                    <td>ค่าจัดส่ง</td>
                                    <td><div align="center">จำนวน</div></td>
                                    <td><div align="center">รวม</div></td>
                                  </tr>
                                  <?
unset($_SESSION["strProductID"]);
$_SESSION["strProductID"] = isset ($_SESSION["strProductID"]) ? $_SESSION["strProductID"] : [];

if (!is_array($_SESSION["strProductID"])) {
    $_SESSION["strProductID"] = array();
}
//============ ทำการแสงรายการที่ได้เลือกว่ามีกี่รายการ
$strNum=0;
$strTotal=0;
for($i=0;$i<=count($_SESSION["strProductID"]);$i++)
{
	
	if(isset($_SESSION["strProductID"]["$i"])){
		//============ เลือกว่ารายการสินค้าใดบ้าง
		$result=select("product","where 1=1 and ProductID='".$_SESSION["strProductID"]["$i"]."' ");
		if($result)
		{
		$strNum++;
?>
                                  <tr bgcolor="#FFFFFF"> 
                                    <td width="9%"> <div align="center"> 
                                        <?=$strNum;?>
                                        <a href="javascript:if(confirm('ลบรายการสินค้า')==true){window.location='<?=$_SERVER['PHP_SELF'];?>?action=Del&P=<?=$i;?>';}"><img src="image/deleteC.gif" width="12" height="11" border="0"></a></div></td>
                                    <td width="48%"> 
                                       <?=$result["ProductName"];?>
                                    </td>
                                    <td width="14%"> <div align="right"> 
                                        <?="".number_format($result["Price"], 2,'.',',');?>
                                      </div></td>
                                    <td width="13%"> 
                                      <div align="right">
                                        <?="".number_format($result["PriceSend"], 2,'.',',');?>
                                      </div></td>
                                    <td width="13%"><div align="center"> 
                                        <input name="txtP[]" type="hidden" id="txtP[]" value="<?=$i;?>">
                                        <input name="txtQua[]" type="text" id="txtQua<?=$strNum;?>" style="width:30px;text-align:right" value="<?=$_SESSION["strQuanlity"]["$i"];?>" maxlength="3" onChange="checknum(this.value,'txtQua<?=$strNum;?>');">
                                      </div></td>
                                    <td width="16%"> <div align="right"> 
                                        <?="".number_format(($result["Price"]+$result["PriceSend"])*$_SESSION["strQuanlity"]["$i"], 2,'.',',');?>
                                      </div></td>
                                  </tr>
                                  <?
								  $strTotal=$strTotal+(($result["Price"]+$result["PriceSend"])*$_SESSION["strQuanlity"]["$i"]);
		}
	}	
}
?>
                                </table>
                                <br> <table width="98%" height="21" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#666666">
                                  <tr bgcolor="#FFFFFF"> 
                                    <td><div align="right">จำนวน (รายการ)</div></td>
                                    <td> <div align="right"> 
                                        <?=$strNum;?>
                                      </div></td>
                                  </tr>
                                  <tr bgcolor="#FFFFFF"> 
                                    <td> <div align="right">ราคารวม (บาท)</div></td>
                                    <td width="16%"> <div align="right"> 
                                        <?="".number_format($strTotal, 2,'.',',');?>
                                      </div></td>
                                  </tr>
                                </table></td>
                            </tr>
                            <tr> 
                              <td width="378"><div align="center"><br>
                                  <?
							if($strNum==0)
							{
							echo "<br><center><strong><font color=red>ไม่มีรายการสั่งซื้อ</font></strong></center><br><br>";
							}
							?>
                                  <input name="BtnContinute" type="button" class="button" id="BtnContinute2" value="ซื้อสินค้าต่อ" onClick="window.location='index.php';">
                                  &nbsp; 
                                  <?
								  if($strNum>0)
								  {?>
                                  <input name="BtnCalu" type="submit" class="button" id="BtnCalu" value="คำนวณเงินใหม่">
                                  &nbsp; 
                                  <input name="BtnPayment" type="button" class="button" id="BtnPayment" value="ชำระเงิน" onClick="window.location='pay.php';">
                                  <?}?>
                                  <br>
                                </div></td>
                            </tr>
                          </table>
                          <p class="style59">&nbsp;</p>
                        </form>
                        <p class="style59">&nbsp;
						<script language="JavaScript">
						function checknum(value,fields)
						{
						if(!check_number(value))
						{
						eval("document.all."+fields+".value='1';");
						alert('กรอกจำนวนไม่ถูกต้อง');
						}
						}
						
						function check_number(ch){
							var len, digit;
							if(ch == " "){ 
								return false;
									 len=0;
								}else{
									 len = ch.length;
							}
							for(var i=0 ; i<len ; i++)
							{
								digit = ch.charat(i)
								if(digit >="0" && digit <="9"){
									;	
								}else{
									return false;					
								}		
							}		
							return true;
						}						
						</script></p>
                        <p class="style59">&nbsp;</p>
                        <p class="style59">&nbsp;</p></td>
                    </tr>
                  </table></td>
                <td width="23%" valign="top"> <?php require_once('menuright.php'); ?> </td>
              </tr>
              <tr> 
                <td colspan="3" valign="top">
                  <?php require_once('down.php'); ?>
                </td>
              </tr>
            </table></td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>




แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-22 15:50:34 By : หอมน้ำ
 


 

No. 20



โพสกระทู้ ( 9,542 )
บทความ ( 2 )



สถานะออฟไลน์


เปลี่ยน <?
เป็น <?php ทุกอันก่อน
ทำให้เคยชิน เพราะ php server ใหม่ๆ จะปิด property short_tag เอาไว้

ob_start(); มาร์คไว้ก่อน ยังไม่ต้องใช้ เป็นคำสั่งรวบรวม output ไว้ใน buffer จะไม่แสดงผลจนกว่าจะทำงานเสร็จ
จะทำงานร่วมกับ ob_end_flush;
อาจจะมีปัญหา มีเออเร่อร์ บางอย่าง ทำให้ไม่แสดงผลอะไรเลย

ปล. อยากรู้ว่าใช้ php version อะไร

จัดลำดับคำสั่งให้ถูกต้อง
Code (PHP)
<?php
error_reporting(-1); // กำหนด การแสดงเออเร่อร์ ทุกชนิด
ini_set('display_errors', 1); // กำหนด ให้แสดง เออเร่อร์
// error_reporting(-1); ini_set('display_errors', 1); // ไม่ใช้

//ob_start(); // mark ไว้ก่อน ยังไม่ใช้ เขียนชำนาญแล้ว ค่อยใช้ ยังไม่สาย อย่าปิดกั้น การมองเห็นทุกชนิด
//============ Start Session และทำการเรียก Function ติดต่อฐานข้อมูล 
session_start();
require("connect/connect.php");
require("connect/function.php");
?>



ประวัติการแก้ไข
2022-05-22 17:02:57
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-22 16:58:57 By : Chaidhanan
 


 

No. 21



โพสกระทู้ ( 4,706 )
บทความ ( 8 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 19 เขียนโดย : หอมน้ำ เมื่อวันที่ 2022-05-22 15:50:34
รายละเอียดของการตอบ ::
> ในหน้าสินค้า ผมให้แก้ตัวกำหนดค่า $_SESSION["strProductID"] ลองอ่านใหม่ ช้าๆ อ่านดีๆ ให้แก้ แต่คุณไปลบออก คือไม่มีประกาศตัวแปรนี้ไว้เลย แล้วจู่ๆไปเรียกใช้ (บรรทัด 41) เดี๋ยวก็จะมี error อีกถ้ามันเข้าเงื่อนไข.


> แล้วคำสั่ง header('Location') ทั้งหลายแหล่ มันจะทำการ redirect ไปหน้าใหม่ แต่ถ้าไม่สั่ง exit(); โค้ดทุกอย่างที่อยู่บรรทัดล่างๆมันจะยังทำงานต่อไปนะครับ มันไม่หยุดให้ ตรงนี้แน่ใจหรือเปล่าว่า redirect แล้วก็ยังปล่อยมันทำงานต่อ???


> การเรียกใช้ตัวแปร array key ก็ผิด เช่น $_GET[p] มันจะเป็นการเรียก $_GET ที่ค่า key p ซึ่ง p นี้คือ constant ไม่ใช่ชื่อ array.
ตัวอย่าง
Code (PHP)
<?php
$array = ['p' => 'Im P'];

define('p', 'we are PPP');

echo $array[p];

จะได้
Quote:
Warning: Undefined array key "we are PPP"

เพราะมันมองหา $array['we are PPP'] ซึ่งมันไม่มี ไม่ใช่มองหา $array['p'] ซึ่งมันมี.


> คำสั่ง select แสดงสินค้าก็ดูเหมือนต้องใช้ข้อมูลจาก $_SESSION["strProductID"] แล้วทีนี้ตัวแปรนี้ถ้าไม่มีอะไรเลย มันก็คือไม่มีอะไรแสดง. ผมก็ไม่รู้มันทำงานมายังไงถึงได้ตัวนี้ คุณต้องไปลองไล่ดูว่ากำหนดตัว $_SESSION["strProductID"] นี้จากที่ไหนกันแน่ แล้วกำหนดอย่างไร.


ฯลฯ
ดูเหมือนจะมีข้อบกพร่องอีกเยอะๆๆ อย่างที่คห.1 บอก พวกโค้ดที่อยู่ในเว็บนี้ ถ้าคุณเอามาใช้โดยไม่ดูวันที่ แล้วไปเอาของเก่ามา หรือดูวันที่เขาโพสต์ใหม่ๆจริง แต่คนโพสต์ดันเขียนใช้ PHP รุ่นพระเจ้าเหา มันก็จะมีปัญหาลักษณะนี้ คือสไตล์การเขียนผิดๆบ้าง, บกพร่องบ้าง, เก่า deprecated ให้เลิกใช้แล้วบ้าง, คำสั่งเลิกใช้เอาออกไปแล้วก็มี
มันต้องแก้อีกเยอะมาก ผมดูแล้วเหนื่อยแทน

ถ้าจะทำสำหรับเอาไปใช้หรือแม้แต่สำหรับเรียนรู้ อาจจะไม่คุ้มนะ เพราะมันต้องวกกลับมาแก้เยอะ จากที่เห็นเขียนบกพร่องหลายจุด.
ถ้าจะเรียนรู้ ถ้าพื้นฐานพอได้ เริ่มเขียนใหม่เองจะสนุกกว่าครับ ถึงจะไม่ต้องทำฟังก์ชั่นเลิศหรูอะไรก็เถอะ มันจะค่อยๆพัฒนาไปเอง.

ลองดูแล้วกัน



ประวัติการแก้ไข
2022-05-22 18:57:58
2022-05-22 19:03:21
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-05-22 18:54:54 By : mr.v
 


 
สวัสดีค่ะพี่ๆ หนูแก้อยู่หลายวันแก้จุดนั้นได้แล้วค่ะ แต่หน้านี้ยังคงทำงานไม่ได้ หนูต้องถามต่อได้ไหมค่ะ หรือต้องไปตั้งกระทู้ไหม่ค่ะ
รายการสินค้าที่กดเลือกชื้อใส่ตระกร้าไม่มาค่ะ พี่ๆช่วยหนูหน่อยได้ไหมค่ะ หนูพยามแก้มาหลายวัน แต่สินค้าที่เลือกยังไม่มาค่ะ ไม่มี error อะไรเลยค่ะ
Code (PHP)
<? php

error_reporting(-1); // กำหนด การแสดงเออเร่อร์ ทุกชนิด
ini_set('display_errors', 1); // กำหนด ให้แสดง เออเร่อร์
// error_reporting(-1); ini_set('display_errors', 1); // ไม่ใช้

//ob_start(); // mark ไว้ก่อน ยังไม่ใช้ เขียนชำนาญแล้ว ค่อยใช้ ยังไม่สาย อย่าปิดกั้น การมองเห็นทุกชนิด
//============ Start Session และทำการเรียก Function ติดต่อฐานข้อมูล 


session_start();
require("connect/connect.php");
require("connect/function.php");



?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>::: CROCHET-PATTERN.COM :::</title>
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body topmargin="0">
<table width="778" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr> 
          <td>
            <?php require_once('head.php'); ?>

<?php




$_GET["ProductID"] = isset ($_GET["ProductID"]) ? $_GET["ProductID"] : '';
$_GET["action"]= isset ($_GET["action"]) ? $_GET["action"] : '';
$_POST["action"] = isset($_POST["action"]) ? $_POST["action"] : '';
$_SESSION["strP"] = isset ($_SESSION["strP"]) ? $_SESSION["strP"] :'';
$ProductID = isset($ProductID) ? $ProductID:'';
$_GET["CatID"] = isset($_GET["CatID"]) ? $_GET["CatID"]:'';
//$_SESSION["strProductID"] = isset($_SESSION["strProductID"]) ? $_SESSION["strProductID"]:[];

echo '$_GET[\'ProductID\']='.$_GET['ProductID'].'';



//============ Check ว่ามีซ้ำแล้วหรือยัง
$ProductID = isset($_GET["ProductID"]) ? trim($_GET["ProductID"]) :'';
if($ProductID != "") 
{	

unset($_SESSION["strProductID"]);
$_SESSION["strProductID"] = isset ($_SESSION["strProductID"]) ? $_SESSION["strProductID"] : [];

if (!is_array($_SESSION["strProductID"])) {
    $_SESSION["strProductID"] = array();
	
	$arr_length = count($_SESSION["strProductID"]);
	for($i=0; $i <= $arr_length; $i++)
	{
			$strProductID = isset($_SESSION["strProductID"][$i]) ? $_SESSION["strProductID"][$i] : '';
			if($strProductID == $ProductID)
			{							
			Header('Location: '.$_SERVER['PHP_SELF']);
			exit();	
			}
			return $i;
			
	}	
}


//============ ถ้ามีการเลือกรายการสินค้าให้เก็บลง Session
if(trim($_GET["ProductID"]) != "")
{   
	$_SESSION["strP"] = $_SESSION["strP"] . +1;
	$_SESSION["strProductID"][$_SESSION["strP"]]=$_GET["ProductID"];
	$_SESSION["strQuanlity"][$_SESSION["strP"]]=1;
	session_write_close();
	//Header('Location: '.$_SERVER['PHP_SELF']); 
	
}	

//============ กรณีลบรายการสั่งซื้อ
if(trim($_GET["action"]) == "Del")
{
	$_SESSION["strProductID"][$_GET[P]]="";
	$_SESSION["strQuanlity"][$_GET[P]]="";
	session_write_close();
	Header('Location: '.$_SERVER['PHP_SELF']); 	
}

//============ กรณีการแก้ไขรายการสั่งซื้อ
if(trim($_POST["action"]) == "Update")
{
for($i=0;$i<count($_POST["txtQua"]);$i++)
{	
	$strP=$_POST["txtP"][$i];
	$strQ=$_POST["txtQua"][$i];
	if($strQ <= 0)
	{
	$strQ=1;
	}

	$_SESSION["strQuanlity"][$strP]=$strQ;
/*
	$result=select("product","where 1=1 and ProductID='".$_SESSION["strProductID"][$strP]."'");
	if($result["Stock"] < $strQ)
	{
	$_SESSION["strQuanlity"][$strP]=$result["Stock"];
									echo"<script language='JavaScript'>";
									echo"alert('รหัสสินค้า [ $result[ProductCode] ] มีจำนวน Stock ไม่เพียงพอ');";
									echo"</script>";	
	}
*/	
	
}	
	session_write_close();
	//============ กระโดด Refresh ใหม่อีกรอบ
	Header('Location: '.$_SERVER['PHP_SELF']); 	
}



?>			
          </td>
        </tr>
        <tr> 
          <td><table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#f2f2f6" bgcolor="#FFFFFF">
              <tr> 
                <td width="23%" valign="top"> <?php require_once('menuleft.php'); ?> <br> </td>
                <td width="54%" valign="top"> 
                  <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <!--DWLayoutTable-->
                    <tr> 
                      <td width="417" height="310" valign="top">
                        <form action="" method="post" name="frmCart" id="frmCart">
                          <table width="415" border="0">
                            <tr> 
                              <td><table width="418" height="22" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">
                                  <tr> 
                                    <td width="8"> <img src="image/menu01.jpg" width="8" height="22" alt=""></td>
                                    <td width="389" background="image/menu02.jpg"><div align="center"><strong>รายการสั่งซื้อ 
                                        <input name="action" type="hidden" id="action" value="Update">
                                        </strong></div></td>
                                    <td width="9"> <img src="image/menu03.jpg" width="9" height="22" alt=""></td>
                                  </tr>
                                </table>
                                <strong> </strong></td>
                            </tr>
                            <tr> 
                              <td>&nbsp;</td>
                            </tr>
                            <tr> 
                              <td><table width="98%" height="21" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#666666">
                                  <tr bgcolor="#FFFFFF"> 
                                    <td><div align="center">ลำดับ</div></td>
                                    <td>ชื่อสินค้า</td>
                                    <td><div align="center">ราคา</div></td>
                                    <td>ค่าจัดส่ง</td>
                                    <td><div align="center">จำนวน</div></td>
                                    <td><div align="center">รวม</div></td>
                                  </tr>
                                  <?php

}

print_r($_SESSION["strProductID"]);	
//============ ทำการแสงรายการที่ได้เลือกว่ามีกี่รายการ
$strNum=0;
$strTotal=0;
$arra_length = count($_SESSION["strProductID"]);
	for($i=0; $i <= $arra_length; $i++)
{
	if(isset($_SESSION["strProductID"]["$i"])){
		//============ เลือกว่ารายการสินค้าใดบ้าง
		//$result= select("product","where 1=1 and ProductID='".$_SESSION["strProductID"]["$i"]."' ");
		$result = "SELECT * FROM product WHERE 1=1 and ProductID = '".$_SESSION["strProductID"][$i]."' ";
		if($result)
		{
		$strNum++;			

	  
		
?>
                                  <tr bgcolor="#FFFFFF"> 
                                    <td width="9%"> <div align="center"> 
                                        <?php echo $strNum;?>
                                        <a href="javascript:if(confirm('ลบรายการสินค้า')==true){window.location='<?php echo $_SERVER['PHP_SELF'];?>?action=Del&P=<?php echo $i;?>';}"><img src="image/deleteC.gif" width="12" height="11" border="0"></a></div></td>
                                    <td width="48%"> 
                                      <?php echo $result["ProductName"];?>
                                    </td>
                                    <td width="14%"> <div align="right"> 
                                        <?php echo "".number_format($result["Price"], 2,'.',',');?>
                                      </div></td>
                                    <td width="13%"> 
                                      <div align="right">
                                        <?php echo "".number_format($result["PriceSend"], 2,'.',',');?>
                                      </div></td>
                                    <td width="13%"><div align="center"> 
                                        <input name="txtP[]" type="hidden" id="txtP[]" value="<?php echo $i;?>">
                                        <input name="txtQua[]" type="text" id="txtQua<?php echo $strNum;?>" style="width:30px;text-align:right" value="<?php echo $_SESSION["strQuanlity"]["$i"];?>" maxlength="3" onChange="checknum(this.value,'txtQua<?php echo $strNum;?>');">
                                      </div></td>
                                    <td width="16%"> <div align="right"> 
                                        <?php echo "".number_format(($result["Price"]+$result["PriceSend"])*$_SESSION["strQuanlity"]["$i"], 2,'.',',');?>
                                      </div></td>
                                  </tr>
                                  <?php
								  $strTotal=$strTotal+(($result["Price"]+$result["PriceSend"])*$_SESSION["strQuanlity"]["$i"]);
		}
	}	
}

?>
                                </table>
                                <br> <table width="98%" height="21" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#666666">
                                  <tr bgcolor="#FFFFFF"> 
                                    <td><div align="right">จำนวน (รายการ)</div></td>
                                    <td> <div align="right"> 
                                        <?php echo $strNum;?>
                                      </div></td>
                                  </tr>
                                  <tr bgcolor="#FFFFFF"> 
                                    <td> <div align="right">ราคารวม (บาท)</div></td>
                                    <td width="16%"> <div align="right"> 
                                        <?php echo"".number_format($strTotal, 2,'.',',');?>
                                      </div></td>
                                  </tr>
                                </table></td>
                            </tr>
                            <tr> 
                              <td width="378"><div align="center"><br>
                                  <?php
							if($strNum==0)
							{
							echo "<br><center><strong><font color=red>ไม่มีรายการสั่งซื้อ</font></strong></center><br><br>";
							}
							?>
                                  <input name="BtnContinute" type="button" class="button" id="BtnContinute2" value="ซื้อสินค้าต่อ" onClick="window.location='index.php';">
                                  &nbsp; 
                                  <?php
								  if($strNum>0)
								  {?>
                                  <input name="BtnCalu" type="submit" class="button" id="BtnCalu" value="คำนวณเงินใหม่">
                                  &nbsp; 
                                  <input name="BtnPayment" type="button" class="button" id="BtnPayment" value="ชำระเงิน" onClick="window.location='pay.php';">
                                  <?php } ?>
                                  <br>
                                </div></td>
                            </tr>
                          </table>
                          <p class="style59">&nbsp;</p>
                        </form>
                        <p class="style59">&nbsp;
						<script language="JavaScript">
						function checknum(value,fields)
						{
						if(!check_number(value))
						{
						eval("document.all."+fields+".value='1';");
						alert('กรอกจำนวนไม่ถูกต้อง');
						}
						}
						
						function check_number(ch){
							var len, digit;
							if(ch == " "){ 
								return false;
									 len=0;
								}else{
									 len = ch.length;
							}
							for(var i=0 ; i<len ; i++)
							{
								digit = ch.charat(i)
								if(digit >="0" && digit <="9"){
									;	
								}else{
									return false;					
								}		
							}		
							return true;
						}						
						</script></p>
                        <p class="style59">&nbsp;</p>
                        <p class="style59">&nbsp;</p></td>
                    </tr>
                  </table></td>
                <td width="23%" valign="top"> <?php require_once('menuright.php'); ?> </td>
              </tr>
              <tr> 
                <td colspan="3" valign="top">
                  <?php require_once('down.php'); ?>
                </td>
              </tr>
            </table></td>
        </tr>
      </table></td>
  </tr>

</table>
</body>
</html>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-02 22:08:41 By : หอมน้ำ
 


 
ตอบความคิดเห็นที่ : 22 เขียนโดย : หอมน้ำ เมื่อวันที่ 2022-06-02 22:08:41
รายละเอียดของการตอบ ::
ขอลิงค์ source code ที่คุณนำมาใช้ + บอกเวอร์ชัน php
ผมจะช่วยพิสูจน์ให้ (ขอเป็นครั้งที่สอง และจะเป็นครั้งสุดท้าย)



เนื่องจาก จขกท มีพื้นฐานไม่เพียงพอที่จะแก้ปัญหาเองได้
ไม่สามารถนำความรู้ไปประยุกต์ใช้ รอการป้อนคำสั่งจากบุคคลที่สามอย่างเดียว
กลายเป็นสาย copy-paste
์จึงขอแนะนำตามเดิม ตาม NO.13 เท่านั้น

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-03 07:47:17 By : 009
 


 

No. 24



โพสกระทู้ ( 4,706 )
บทความ ( 8 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 22 เขียนโดย : หอมน้ำ เมื่อวันที่ 2022-06-02 22:08:41
รายละเอียดของการตอบ ::
สินค้าที่เพิ่มเก็บลง session หรือเปล่า? ถ้าใช่ก็ลอง print_r($_SESSION); ออกมาดูว่ามันเก็บอะไรลงไปจริงมั้ย?

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-03 09:01:55 By : mr.v
 


 
ตอบความคิดเห็นที่ : 23 เขียนโดย : 009 เมื่อวันที่ 2022-06-03 07:47:17
รายละเอียดของการตอบ ::
ขอโทษค่ะ
โหลดมาจากที่นี้ค่ะ
https://www.thaicreate.com/community/php-shooping-cart-free.html
หนูใช้ PHP Version 8.1.5 ค่ะ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-03 18:52:02 By : หอมน้ำ
 


 
ตอบความคิดเห็นที่ : 24 เขียนโดย : mr.v เมื่อวันที่ 2022-06-03 09:01:55
รายละเอียดของการตอบ ::
Code (PHP)
echo '$_GET[\'ProductID\']='.$_GET['ProductID'].'';

จะได้ $_GET['ProductID']=17 ค่ะ

Code (PHP)
print_r($_SESSION);

จะได้ Array ( [strEmail] => [strStatus] => Yes [strP] => 1111 [strQuanlity] => Array ( [1] => 1 [11] => 1 [111] => 1 [1111] => 1 ) [strProductID] => Array ( [1111] => 17 ) ) ค่ะ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-03 18:57:02 By : หอมน้ำ
 


 

No. 27



โพสกระทู้ ( 4,706 )
บทความ ( 8 )



สถานะออฟไลน์


Code (PHP)
Array ( 
[strEmail] => 
[strStatus] => Yes 
[strP] => 1111 
[strQuanlity] => Array ( 
    [1] => 1 
    [11] => 1 
    [111] => 1 
    [1111] => 1 
) 
[strProductID] => Array ( [1111] => 17 ) 
)

โอย ดูแล้วงง 1111 คืออะไรมาจากไหน แล้ว 1, 11, 111 อะไรเยอะแยะไปหมด?
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-03 23:31:56 By : mr.v
 


 
เอาแบบลวกๆ แล้วกัน โค้ดใหม่จนตามไม่ทัน

cart


cart.php
<?php
ob_start();
//============ Start Session และทำการเรียก Function ติดต่อฐานข้อมูล 
session_start();
require("connect/connect.php");
require("connect/function.php");
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>::: CROCHET-PATTERN.COM :::</title>
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body topmargin="0">
<table width="778" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr> 
          <td>
            <?php require_once('head.php'); ?>

<?php

//============ Check ว่ามีซ้ำแล้วหรือยัง
if(!empty($_GET["ProductID"]))
{
	if (isset($_SESSION["strProductID"]) && is_array($_SESSION["strProductID"])) {
		for($i=0;$i<=count($_SESSION["strProductID"]);$i++)
		{
			if($_SESSION["strProductID"][$i] == $_GET["ProductID"])
			{
				header("location:$_SERVER[PHP_SELF]");
				exit();
			}
		}
	}	
}

//============ ถ้ามีการเลือกรายการสินค้าให้เก็บลง Session
if(isset($_GET["ProductID"]) && trim($_GET["ProductID"]) != "")
{
	$_SESSION["strP"]=$_SESSION["strP"]+1; // ถ้าแก้ index ตรงนี้ให้ถูกต้อง(ลองไปคิดเอง)  ก็ไม่ต้องเรียก array_values() เพื่อ reset index
	$_SESSION["strProductID"][$_SESSION["strP"]]=$_GET["ProductID"];
	$_SESSION["strQuanlity"][$_SESSION["strP"]]=1;
	session_write_close();
	header("location:$_SERVER[PHP_SELF]");
}	

//============ กรณีลบรายการสั่งซื้อ
if(isset($_GET["action"]) && trim($_GET["action"]) == "Del")
{
	$_SESSION["strProductID"][$_GET["P"]]="";
	$_SESSION["strQuanlity"][$_GET["P"]]="";
	session_write_close();
	header("location:$_SERVER[PHP_SELF]");
}	

//============ กรณีการแก้ไขรายการสั่งซื้อ
if(isset($_POST["action"]) && trim($_POST["action"]) == "Update")
{
for($i=0;$i<count($_POST["txtQua"]);$i++)
{	
	$strP=$_POST["txtP"][$i];
	$strQ=$_POST["txtQua"][$i];
	if($strQ <= 0)
	{
	$strQ=1;
	}

	$_SESSION["strQuanlity"][$strP]=$strQ;
/*
	$result=select("product","where 1=1 and ProductID='".$_SESSION["strProductID"][$strP]."'");
	if($result["Stock"] < $strQ)
	{
	$_SESSION["strQuanlity"][$strP]=$result["Stock"];
									echo"<script language='JavaScript'>";
									echo"alert('รหัสสินค้า [ $result[ProductCode] ] มีจำนวน Stock ไม่เพียงพอ');";
									echo"</script>";	
	}
*/	
	
}	
	session_write_close();
	//============ กระโดด Refresh ใหม่อีกรอบ
	header("location:$_SERVER[PHP_SELF]");
}	
?>			
          </td>
        </tr>
        <tr> 
          <td><table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#f2f2f6" bgcolor="#FFFFFF">
              <tr> 
                <td width="23%" valign="top"> <?php require_once('menuleft.php'); ?> <br> </td>
                <td width="54%" valign="top"> 
                  <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <!--DWLayoutTable-->
                    <tr> 
                      <td width="417" height="310" valign="top">
                        <form action="" method="post" name="frmCart" id="frmCart">
                          <table width="415" border="0">
                            <tr> 
                              <td><table width="418" height="22" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">
                                  <tr> 
                                    <td width="8"> <img src="image/menu01.jpg" width="8" height="22" alt=""></td>
                                    <td width="389" background="image/menu02.jpg"><div align="center"><strong>รายการสั่งซื้อ 
                                        <input name="action" type="hidden" id="action" value="Update">
                                        </strong></div></td>
                                    <td width="9"> <img src="image/menu03.jpg" width="9" height="22" alt=""></td>
                                  </tr>
                                </table>
                                <strong> </strong></td>
                            </tr>
                            <tr> 
                              <td>&nbsp;</td>
                            </tr>
                            <tr> 
                              <td><table width="98%" height="21" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#666666">
                                  <tr bgcolor="#FFFFFF"> 
                                    <td><div align="center">ลำดับ</div></td>
                                    <td>ชื่อสินค้า</td>
                                    <td><div align="center">ราคา</div></td>
                                    <td>ค่าจัดส่ง</td>
                                    <td><div align="center">จำนวน</div></td>
                                    <td><div align="center">รวม</div></td>
                                  </tr>
                                  <?php

//============ ทำการแสงรายการที่ได้เลือกว่ามีกี่รายการ

$strNum=0;
$strTotal=0;
// unset ไว้ลบ array แต่ไม่ reset index มีประโยชน์ในการทดสอบเพื่อไม่ให้รายการเยอะเกินไป
//   *** ปุ่ม ถังขยะ(delete)ลบเฉพาะ values แต่ keys ยังอยู่  ***
//unset($_SESSION["strProductID"]);
//unset($_SESSION["strQuanlity"]);

// array_values ใช้ reset array index โดยไม่สนว่าก่อนหน้านี้จะมี index = 1, 11, 111, ...etc.
$_SESSION["strProductID"] = array_values($_SESSION["strProductID"]);
$_SESSION["strQuanlity"] = array_values($_SESSION["strQuanlity"]);

// 7 บรรทัดนี้ เอาไว้สำหรับจับผิดการทำงาน (debug only)
echo '<span style="color: red;">Debug</span><br><b>$_SESSION["strProductID"]:</b> ';
print_r($_SESSION["strProductID"]);
echo "<br>";
echo '<b>$_SESSION["strQuanlity"]:</b> ';
print_r($_SESSION["strQuanlity"]);
$constant = 'constant';
echo "<br><hr>PHP Version: {$constant('PHP_VERSION')}<hr>";
// end debug

for($i=0;$i<=count($_SESSION["strProductID"]);$i++)
{
//============ เลือกว่ารายการสินค้าใดบ้าง
$result=select("product","where 1=1 and ProductID='".($_SESSION["strProductID"]["$i"] ?? null)."' ");
if($result)
{
$strNum++;
?>
                                  <tr bgcolor="#FFFFFF"> 
                                    <td width="9%"> <div align="center"> 
                                        <?php echo $strNum;?>
                                        <a href="javascript:if(confirm('ลบรายการสินค้า')==true){window.location='<?php echo $_SERVER['PHP_SELF'];?>?action=Del&P=<?php echo $i;?>';}"><img src="image/deleteC.gif" width="12" height="11" border="0"></a></div></td>
                                    <td width="48%"> 
                                      <?php echo $result["ProductName"];?>
                                    </td>
                                    <td width="14%"> <div align="right"> 
                                        <?php echo "".number_format($result["Price"], 2,'.',',');?>
                                      </div></td>
                                    <td width="13%"> 
                                      <div align="right">
                                        <?php echo "".number_format($result["PriceSend"], 2,'.',',');?>
                                      </div></td>
                                    <td width="13%"><div align="center"> 
                                        <input name="txtP[]" type="hidden" id="txtP[]" value="<?php echo $i;?>">
                                        <input name="txtQua[]" type="text" id="txtQua<?php echo $strNum;?>" style="width:30px;text-align:right" value="<?php echo $_SESSION["strQuanlity"]["$i"];?>" maxlength="3" onChange="checknum(this.value,'txtQua<?php echo $strNum;?>');">
                                      </div></td>
                                    <td width="16%"> <div align="right"> 
                                        <?php echo "".number_format(($result["Price"]+$result["PriceSend"])*$_SESSION["strQuanlity"]["$i"], 2,'.',',');?>
                                      </div></td>
                                  </tr>
                                  <?php
								  $strTotal=$strTotal+(($result["Price"]+$result["PriceSend"])*$_SESSION["strQuanlity"]["$i"]);
}
}
?>
                                </table>
                                <br> <table width="98%" height="21" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#666666">
                                  <tr bgcolor="#FFFFFF"> 
                                    <td><div align="right">จำนวน (รายการ)</div></td>
                                    <td> <div align="right"> 
                                        <?php echo $strNum;?>
                                      </div></td>
                                  </tr>
                                  <tr bgcolor="#FFFFFF"> 
                                    <td> <div align="right">ราคารวม (บาท)</div></td>
                                    <td width="16%"> <div align="right"> 
                                        <?php echo "".number_format($strTotal, 2,'.',',');?>
                                      </div></td>
                                  </tr>
                                </table></td>
                            </tr>
                            <tr> 
                              <td width="378"><div align="center"><br>
                                  <?php
							if($strNum==0)
							{
							echo "<br><center><strong><font color=red>ไม่มีรายการสั่งซื้อ</font></strong></center><br><br>";
							}
							?>
                                  <input name="BtnContinute" type="button" class="button" id="BtnContinute2" value="ซื้อสินค้าต่อ" onClick="window.location='index.php';">
                                  &nbsp; 
                                  <?php
								  if($strNum>0)
								  { ?>
                                  <input name="BtnCalu" type="submit" class="button" id="BtnCalu" value="คำนวณเงินใหม่">
                                  &nbsp; 
                                  <input name="BtnPayment" type="button" class="button" id="BtnPayment" value="ชำระเงิน" onClick="window.location='pay.php';">
                                  <?php } ?>
                                  <br>
                                </div></td>
                            </tr>
                          </table>
                          <p class="style59">&nbsp;</p>
                        </form>
                        <p class="style59">&nbsp;
						<script language="JavaScript">
						function checknum(value,fields)
						{
						if(!check_number(value))
						{
						eval("document.all."+fields+".value='1';");
						alert('กรอกจำนวนไม่ถูกต้อง');
						}
						}
						
						function check_number(ch){
							var len, digit;
							if(ch == " "){ 
								return false;
									 len=0;
								}else{
									 len = ch.length;
							}
							for(var i=0 ; i<len ; i++)
							{
								digit = ch.charat(i)
								if(digit >="0" && digit <="9"){
									;	
								}else{
									return false;					
								}		
							}		
							return true;
						}						
						</script></p>
                        <p class="style59">&nbsp;</p>
                        <p class="style59">&nbsp;</p></td>
                    </tr>
                  </table></td>
                <td width="23%" valign="top"> <?php require_once('menuright.php'); ?> </td>
              </tr>
              <tr> 
                <td colspan="3" valign="top">
                  <?php require_once('down.php'); ?>
                </td>
              </tr>
            </table></td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>






1. คำอธิบาย (บางส่วน ทุกส่วนพิมพ์ไม่ไหว) ดูในคอมเมนต์
2. คราวหลังอย่าพยายามแปลชื่อไฟล์เป็นภาษาไทย
3. ไฟล์ product หากยังไม่ทำ ...ลองดูครับ ได้ทบทวนพื้นฐานไม่น้อย
4. คนปล่อยน่าจะได้แรงบันดาลใจมาจาก wordpress เพราะมีโครงสร้างและหลายอย่างเหมือนเป๊ะ เช่น $PHP_SELF
5. โค้ดเก่าแล้ว ไว้ฝึกพอได้ หากินอาจลำบาก
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-04 00:56:28 By : 009
 


 
ตอบความคิดเห็นที่ : 28 เขียนโดย : 009 เมื่อวันที่ 2022-06-04 00:56:28
รายละเอียดของการตอบ ::
ขอบคุณมากๆนะคะ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-04 20:22:40 By : หอมน้ำ
 


 
ตอบความคิดเห็นที่ : 27 เขียนโดย : mr.v เมื่อวันที่ 2022-06-03 23:31:56
รายละเอียดของการตอบ ::
ขอบคุณมากนะคะ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-04 20:25:35 By : หอมน้ำ
 


 
ขอบคุณพี่ๆทุกคนมากๆนะคะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-04 20:26:22 By : หอมน้ำ
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ต้องการเปลี่ยน code เขียน mysqli ในตะกร้าสินค้าค่ะ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่