希望有人能理解…
我在php还是很新的,我被卡住了。
我有一个问题……我有一个由foreach循环生成的复选框列表,每个框应该有一个多维数组作为它的值。<input type='checkbox' name='unit[]' value='multidimensional array' />
我有一个包含数组的文件,像这样:
$disclaimer = disclaimer;
$unit = array(
//EXTRAS
"0" => array("key0_key0" => "<h1>key0_key0_VALUE</h1>",
"key0_key1" => "<li>key0_key1_VALUE</li>",
"key0_key2" => "<li>key0_key2_VALUE</li>",
"key0_key3" => "<li>key0_key3_VALUE</li>",
"key0_key4" => "<li>key0_key4_VALUE</li>",
"key0_key5" => "<li>key0_key_VALUE</li>",
"key0_key6" => array("key0_key6_key1" => "<h1>key0_key6_key1_VALUE</h1>",
"key0_key6_key2" => "<li>key0_key6_key2_VALUE</li>"
),
),
),
//UNIT 1
"1" => array("key1_key0" => "<h1>key1_key0_VALUE</h1>",
"key1_key1" => "<li>key1_key1_VALUE</li>",
"key1_key2" => "<li>key1_key2_VALUE</li>",
"key1_key3" => "<li>key1_key3_VALUE</li>",
"key1_key4" => "<li>key1_key4_VALUE</li>",
"key1_key5" => "<li>key1_key_VALUE</li>",
"key1_key6" => array("key1_key6_key1" => "<h1>key1_key6_key1_VALUE</h1>",
"key1_key6_key2" => "<li>key1_key6_key2_VALUE</li>"
),
),
),
//UNIT 2
"2" => array("key2_key0" => "<h1>key2_key0_VALUE</h1>",
"key2_key1" => "<li>key2_key1_VALUE</li>",
"key2_key2" => "<li>key2_key2_VALUE</li>",
"key2_key3" => "<li>key2_key3_VALUE</li>",
"key2_key4" => "<li>key2_key4_VALUE</li>",
"key2_key5" => "<li>key2_key_VALUE</li>",
"key2_key6" => array("key2_key6_key1" => "<h1>key2_key6_key1_VALUE</h1>",
"key2_key6_key2" => "<li>key2_key6_key2_VALUE</li>"
),
),
);
我有这些在一个单独的包括文件,我包括在哪里,我可能需要这些显示。
然后在我的页面上,我将其包含在foreach循环之后,以显示每个复选框,该复选框的值为每个单位数组增加1。
$i = 0;
foreach($unit as $unit_no){
echo"<div class='pricelist-container'>";
if (is_array($unit_no)){
foreach ($unit_no as $unit_details){
if (is_array($unit_details)) {
foreach ($unit_details as $unit_contents){
if (is_array($unit_contents)){
foreach ($unit_contents as $extra){
echo $extra;
}
}
else{
echo $unit_contents;
}
}
}
else{
echo $unit_details;
}
}
}
else{
echo $unit_no;
}
echo"
$disclaimer
//below is where my problem seems to start.
<p class='fine'> <input type='checkbox' name='unit[]' value='" . "$" . "unit" . "[" . $i++ . "];' checked='checked' /> please uncheck to remove list</p>
<br />
</div>";
}
这是我的问题,因为我有它…但由于我是新手,我可能大错特错。我需要这些复选框来生成$var[key]作为一个值,然后提交表单后,它需要能够将这些变量传递到一个新的页面,在那里我有数组文件再次包含,所以我可以通过复选框循环,并在短列表上再次显示它们。但是我不能有这样的代码,我需要添加小循环,每次一个被添加到包含文件echo出每个这些单位,因为我将把这个包含文件更改为数据库文件,并创建一个上传页面,为网站所有者创建他/她自己的新单位,而不必编辑任何东西为这些显示在网站上。
if(isset($_POST['unit'])){
if (is_array($_POST['unit'])){
foreach ($_POST['unit'] as $print_unit){
echo $print_unit;
}
}
}
//this loop will echo out the $var[key] as strings.
上面的foreach将以字符串的形式回显$var,而不是键值递增的$var。
它基本上是一个产品列表,每个产品的内容在一个数组中,带有复选框,可以从可打印列表中添加或删除它们。一切工作,除了创建复选框值…
value='" . "$" . "unit" . "[" . $i++ . "];'
这将为我在include中的每个数组创建名称$unit[1],但它生成字符串$unit[1],但不是作为一个实际的数组和它的第一个键。
我的一个朋友帮我解决了这个问题,我将尽我所能用我有限的知识来回答我自己的问题。
如果专业人士能读一下这个答案并确认它是OK的,那就太好了。
ARRAY设置保存在包含文件中例如(data.php):
$Number = 1;
$unit[$Number]['id']="id";
$unit[$Number]['img']="image";
$unit[$Number]['name']="name";
$unit[$Number]['price']=00.00;
$unit[$Number]['contents'][1]="content1";
$unit[$Number]['contents'][2]="content2";
$unit[$Number]['contents'][3]="content3";
$unit[$Number]['contents'][4]="content4.";
$Number = 2;
$unit[$Number]['id']="id";
$unit[$Number]['img']="image";
$unit[$Number]['name']="name";
$unit[$Number]['price']=00.00;
$unit[$Number]['contents'][1]="content1";
$unit[$Number]['contents'][2]="content2";
$unit[$Number]['contents'][3]="content3";
$unit[$Number]['contents'][4]="content4.";
//END OF PRODUCT/UNIT ARRAYS
第一个列表显示循环在实际网页上显示价格表:
在包含数据文件之后如:
<!--OPEN FORM-->
<form method='post' action='SCRIPT FILE NAME.php'>
<?php
include('data.php');
/*Set INCREMENT.*/
$i = 1;
/*DISPLAY LOOP*/
foreach ($unit as $UNIT)
//ECHO SOME HTML FOR A LAYOUT CONTAINER DIV
echo "<div class='####'>";
//image
echo "<img class='#' src='location_folder_details/".$UNIT['img']."'>";
//name
echo $UNIT['name'];
//price
echo "Dollar/Pound symbol".number_format ($UNIT['price'] , 2, '.' , ' ' );
//contents
//SECOND ARRAY LOOP FOR CONTENT
if (is_array($UNIT['contents'])){
foreach ($UNIT['contents'] as $unit_contents){
echo "<li>".$unit_contents."</li>";
}
}
else {
echo "NO DETAILS AVAILABLE";
}
echo "<p class='fine' ><input type='checkbox' name='unit_$i' value='".$UNIT['id']."' checked='checked'> please uncheck to remove.</p>";
//CLOSE HTML LAYOUT CONTAINER DIV.
echo "</div>";
//Increment $i
$i ++;
}
//ADD HIDDEN FIELD TO REMEMBER INCREMENT
echo "<input type='hidden' name='count' value='$i'>";
?>
<!--ADD SUBMIT AND END FORM-->
<input class='send' action='###.php' type='submit' name='NAME' value='SUBMIT' />
</form>
那么如何处理呢
//ADD INCLUDE FILE
include ('data.php');
//POST HIDDEN COUNT FIELD
$count = $_POST['count'];
//RE-SET INCREMENT
$i = 1;
<?php
while ($i < $count){
$var_name = "unit_$i";
if (isset($_POST[$var_name])) {
// The tickbox "value", is the UNIT ID... Check include.php
$unit_id = $_POST["$var_name"];
foreach ($unit as $UNIT) {
if ($UNIT['id'] == $unit_id) {
//HTML LAYOUT ECHO
echo "<div class='####'>";
//image
echo "<img class='####' src='LOCATION/PATH/".$UNIT['img']."'>";
//name
echo $UNIT['name'];
//price
echo "DOLLAR/POND SYMBOL".number_format ($UNIT['price'] , 2, '.' , ' ' );
//SECOND ARRAY LOOP FOR CONTENT
if (is_array($UNIT['contents'])){
foreach ($UNIT['contents'] as $unit_contents){
//CONTENTS
echo "<li>".$unit_contents."</li>";
}
}
else {
echo "NO DETAILS AVAILABLE";
}
//HTML LAYOUT ECHO
echo "</div>";
}
}
}
$i++;
}
?>
我希望这对其他初学者有用,因为我挣扎了好几天,我不能弄清楚。