iT邦幫忙

0

PHP 及HTML if for 迴圈 菜鳥問問題

jj77 2019-01-07 15:31:564329 瀏覽
  • 分享至 

  • xImage

出現 Warning: count(): Parameter must be an array or an object that implements Countable in K:\backup\drink - BACKUP\receive.php on line 65
在接收頁的62~70行

<?php
					if  ($_POST['ITEM'] !=null)
						{
							for ($i = 0 ; $i < count($_POST['ITEM']);$i++)
						{
							echo $_POST['ITEM'][$i]."&nbsp;&nbsp;";
						}
						}
			?><br>

請問該如何解決?
傳送頁

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by TEMPLATED
http://templated.co
Released for free under the Creative Commons Attribution License

Name       : Stratagem 
Description: A two-column, fixed-width design with dark color scheme.
Version    : 1.0
Released   : 20130707

-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery-2.1.3.js" type="text/javascript"></script>
<script src="highcharts.js" type="text/javascript"></script>
<script src="jquery.highchartTable.js" type="text/javascript"></script>
<title>飲料自助吧</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="all" />
<link href="fonts.css" rel="stylesheet" type="text/css" media="all" />
<style type="text/css">
.map {
	width: 600px;
	height: 800px;
	margin-left: auto;
	margin-right: auto;
}
</style>
<!--[if IE 6]>
<link href="default_ie6.css" rel="stylesheet" type="text/css" />
<![endif]-->
</head>
<body>

<form action="receive.php" name="from" method="POST">	
<div id="wrapper">
	<div id="menu-wrapper">
		<div id="menu" class="container">
			<ul>
				<li><a href="index.html">首頁</a></li>
				<li><a href="news.html">最新消息</a></li>
				<li><a href="menu.html">菜單</a></li>
				<li class="current_page_ITEM"><a href="onlinebuy.php">線上訂購</a></li>
				<li><a href="contactus.html">聯絡我們</a></li>
			</ul>
		</div>
	</div>
	<div id="header-wrapper">
		<div id="header" class="container">
			<div id="logo">
			  <h1>飲料自助吧</h1>
				<marquee scrollamount="5"><font face="標楷體"><font color="white">最便利的線上飲品訂購外送服務  最新優惠  一觸即發</font></font></marquee>
			</div>
		</div>
	</div>

		<div class=" left">
訂貨人:<input type="text" name="name"><br>
電話:<input type="text" name="phone"><br>
地址:<input type="text" name="address"><br>
---------------------------------------------------------<br>
鮮榨果汁

  <tr>
    <td><P></td>
  </tr>
  <tr>
  </tr>
  
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="大紅西瓜,40">大紅西瓜</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="小玉西瓜,40">小玉西瓜</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="綜合水果,40">綜合水果</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="奇異果汁,40">奇異果汁</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="火龍果汁,40">火龍果汁</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="金桔檸檬,40">金桔檸檬</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="香蕉芭樂,40">香蕉芭樂</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="鳳梨蘋果,50">鳳梨蘋果</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="葡萄水梨,50">葡萄水梨</td>
  </tr>
</table>
</div>

<div class="float">
 特級茗茶
  <tr>
    <td><P></td>
  </tr>
  <tr>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="凍頂烏龍,40">凍頂烏龍</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="烏龍綠茶,40">烏龍綠茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="玉露綠茶,40">玉露綠茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="四季青茶,40">四季青茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="錫蘭紅茶,40">錫蘭紅茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="正山紅茶,40">正山紅茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="特級普洱,40">特級普洱</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="鐵觀音茶,50">鐵觀音茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="情人果茶,50">情人果茶</td>
  </tr>
</table>
</div>
<div class="float">
 混搭牛奶
  <tr>
    <td><P></td>
  </tr>
  <tr>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="烏龍牛奶,40">烏龍牛奶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="綠茶牛奶,40">綠茶牛奶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="木瓜牛奶,40">木瓜牛奶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="香蕉牛奶,40">香蕉牛奶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="可可牛奶,40">可可牛奶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="紅茶牛奶,40">紅茶牛奶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="黑糖牛奶,40">黑糖牛奶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="豆沙牛奶,50">豆沙牛奶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="翡翠牛奶,50">翡翠牛奶</td>
  </tr>
</table>
</div>
<div class="float">
好料飲品
  <tr>
    <td><P></td>
  </tr>
  <tr>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="珍珠紅茶,40">珍珠紅茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="珍珠綠茶,40">珍珠綠茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="珍珠烏龍,40">珍珠烏龍</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="布丁紅茶,40">布丁紅茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="布丁綠茶,40">布丁綠茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="布丁烏龍,40">布丁烏龍</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="蒟蒻紅茶,40">蒟蒻紅茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="蒟蒻綠茶,50">蒟蒻綠茶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="蒟蒻烏龍,50">蒟蒻烏龍</td>
  </tr>
</table>
</div>
<div class="float">
 期間限定
  <tr>
    <td><P></td>
  </tr>
  <tr>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="花生牛奶,40">花生牛奶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="芋頭牛奶,40">芋頭牛奶</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="紅豆桂花,40">紅豆桂花</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="薑汁桂圓,40">薑汁桂圓</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="薑汁檸檬,40">薑汁檸檬</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="抹茶紅豆,40">抹茶紅豆</td>
  </tr>
  <tr>
    <td><input TYPE=CHECKBOX NAME=ITEM VALUE="香蕉芭樂,40">香蕉芭樂</td>
  </tr>
</table>
<tr>
 </tr>
</div>
<input TYPE="submit" value="送出" onClick="receive.php">
<input TYPE="reset" value="重寫">
<div id="banner" class="container"> <a href="#" class="image"></a></div>
</div>
</form>
<div id="copyright" class="container">
  <p>&copy; TAIWAN. All rights reserved. | Photos by <a href="login.html">紅色布丁狗</a> | Design by <a href="login.html" rel="nofollow">藍色布丁狗</a>.</p>
</div>
</body>
</html>

接收頁

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset = utf-8"  />
<script src="jquery-2.1.3.js" type="text/javascript"></script>
<script src="highcharts.js" type="text/javascript"></script>
<script src="jquery.highchartTable.js" type="text/javascript"></script>
<title>飲料自助吧</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="all" />
<link href="fonts.css" rel="stylesheet" type="text/css" media="all" />
<style type="text/css">
.map {
	width: 600px;
	height: 800px;
	margin-left: auto;
	margin-right: auto;
}
</style>
<!--[if IE 6]>
<link href="default_ie6.css" rel="stylesheet" type="text/css" />
<![endif]-->
</head>
<body>
<div id="wrapper">
	<div id="menu-wrapper">
		<div id="menu" class="container">
			<ul>
				<li><a href="index.html">首頁</a></li>
				<li><a href="news.html">最新消息</a></li>
				<li><a href="menu.html">菜單</a></li>
				<li class="current_page_item"><a href="onlinebuy.php">線上訂購</a></li>
				<li><a href="contact us.html">聯絡我們</a></li>
			</ul>
		</div>
	</div>
	<div id="header-wrapper">
		<div id="header" class="container">
			<div id="logo">
			  <h1>確認訂單</h1>
				<marquee scrollamount="5"><font face="標楷體"><font color="white">最便利的線上飲品訂購外送服務  最新優惠  一觸即發</font></font></marquee>
			</div>
		</div>
	</div>
	<form>
	<?php
	$con = mysqli_connect("127.0.0.1","root1","123456","test");
	if (mysqli_connect_errno())
		{
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
		}
		mysqli_query($con,"SELECT * FROM `data`");
		mysqli_query($con,"INSERT INTO `data`(name,phone,address,ITEM) VALUES ('".$_POST["name"]."','".$_POST["phone"]."','".$_POST["address"]."','".$_POST["ITEM"]."')");
		mysqli_close($con);
	?>
	
	訂貨人姓名:<?php echo $_POST['name'];?><br>
	電話:<?php echo $_POST['phone'];?><br>
	地址:<?php echo $_POST['address'];?><br>
	訂單:<?php
					if  ($_POST['ITEM'] !=null)
						{
							for ($i = 0 ; $i < count($_POST['ITEM']);$i++)
						{
							echo $_POST['ITEM'][$i]."&nbsp;&nbsp;";
						}
						}
			?><br>
	
	
	</form>
	
<div id="banner" class="container"> <a href="#" class="image"></a> </div>
</div>
<div id="copyright" class="container">
  <p>&copy; TAIWAN. All rights reserved. | Photos by <a href="#">紅色布丁狗</a> | Design by <a href="#" rel="nofollow">藍色布丁狗</a>.</p>
</div>
</body>
</html>

柯柯 iT邦新手 2 級 ‧ 2019-01-07 15:38:40 檢舉
if ($_POST['ITEM'] !=null){
foreach($_POST['ITEM'] as $v){
echo $v."&nbsp;&nbsp;";
}
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

3
耿直小伙
iT邦新手 1 級 ‧ 2019-01-07 16:14:34

不要傳遞無效的參數給count(),先用isset()驗證參數是否存在。另外使用for不要反覆呼叫count(),應該先用count()取出陣列最大值在進行。若要明確設定條件為null,最好使用$_POST['ITEM'] === null或者is_null()判斷,否則可能會產生其他結果。

<?php
    if (isset($_POST['ITEM']) && $_POST['ITEM'] != null) {
        $count = count($_POST['ITEM']);
        for ($i = 0 ; $i < $count;$i++) {
            echo $_POST['ITEM'][$i];
        }
    }
?>
0
if  ($_POST['ITEM'] !=null)
						{
							for ($i = 0 ; $i < count($_POST['ITEM']);$i++)
						{
							echo $_POST['ITEM'][$i]."&nbsp;&nbsp;";
						}
						}

改成

if  (is_array($_POST['ITEM']))
{
        foreach($_POST['ITEM'] AS $_item){
            echo $_item;
        }
}

我要發表回答

立即登入回答