我想创造一些简单的东西。由于我是PHP的新手,我很难看到我做错了什么。我在下面的代码片段中有两个PHP文件内容,以及我想要实现的静态代码。
<?php
foreach ($toppingItems as $item) {
echo "<label class="hover topping" for="c$item[number]"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c$item[number]">$item[title]</label>";
}
?>
<?php
// Pizza Toppings
$toppingItems = array(
array(
"number" => "4",
"title" => "EXTRA CHEESE"
),
array(
"number" => "5",
"title" => "TOMATOES"
),
array(
"number" => "6",
"title" => "OLIVES"
),
array(
"number" => "7",
"title" => "MUSHROOMS"
),
array(
"number" => "8",
"title" => "CHICKEN"
),
array(
"number" => "9",
"title" => "MOZZARELLA"
),
array(
"number" => "10",
"title" => "SALAMI"
),
array(
"number" => "11",
"title" => "ONIONS"
),
array(
"number" => "12",
"title" => "PEPPERONI"
),
array(
"number" => "13",
"title" => "STUFFED CRUST"
),
array(
"number" => "14",
"title" => "MEATBALLS"
),
array(
"number" => "15",
"title" => "BACON"
),
array(
"number" => "16",
"title" => "HAM"
),
array(
"number" => "17",
"title" => "SHRIMPS"
),
);
?>
<label class="hover topping" for="c4"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c4">EXTRA CHEESE</label>
<label class="hover topping" for="c5"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c5">TOMATOES</label>
<label class="hover topping" for="c6"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c6">OLIVES</label>
<label class="hover topping" for="c7"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c7">MUSHROOMS</label>
<label class="hover topping" for="c8"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c8">CHICKEN</label>
<label class="hover topping" for="c9"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c9">MOZZARELLA</label>
<label class="hover topping" for="c10"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c10">SALAMI</label>
<label class="hover topping" for="c11"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c11">ONIONS</label>
<label class="hover topping" for="c12"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c12">PEPPERONI</label>
<label class="hover topping" for="c13"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c13">STUFFED CRUST</label>
<label class="hover topping" for="c14"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c14">MEATBALLS</label>
<label class="hover topping" for="c15"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c15">BACON</label>
<label class="hover topping" for="c16"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c16">HAM</label>
<label class="hover topping" for="c17"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c17">SHRIMPS</label>
是的,我确保toppings.php文件和arrays.php文件已连接到索引文件。
提前感谢您的帮助,如果您仍然不确定我想要实现的目标,请随时给我留言。
答案 0 :(得分:1)
您在双引号"
"
echo "<label class="hover topping" for="c$item[number]"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c$item[number]">$item[title]</label>";
尝试使用单引号'
echo "<label class='hover topping' for='c$item[number]'><input class='items' onclick='findTotal()' type='checkbox' name='topping' value='1.00' id='c$item[number]'>$item[title]</label>";
感谢Fred感谢pointing out如果您的值(例如$item
)可能包含单引号'
,那么您将不得不逃脱带有反斜杠\"
echo "<label class=\"hover topping\" for=\"c$item[number]\"><input class=\"items\" onclick=\"findTotal()\" type=\"checkbox\" name=\"topping\" value=\"1.00\" id=\"c$item[number]\">$item[title]</label>";
并且更进一步,如果$item
可能包含单'
和双"
引号的混合,那么你必须采取@Kevinvhengst's answer中的拆分方法,然后使用htmlentities
echo '<label class="hover topping" for="c'.htmlentities($item['number']).'"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c'.htmlentities($item['number']).'">'.htmlentities($item['title']).'</label>';
答案 1 :(得分:1)
回应以下内容:
echo '<label class="hover topping" for="c'.$item['number'].'"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c'.$item['number'].'">'.$item['title'].'</label>';
答案 2 :(得分:0)
混合字符串和PHP代码令人困惑,特别是当你有双引号时,将HTML字符串(通常包含双引号)放在单引号和这些字符串之外的PHP表达式中更容易:
foreach ($toppingItems as $item) {
echo '<label class="hover topping" for="c'.$item['number'].
'"><input class="items" onclick="findTotal()" type="checkbox" '.
'name="topping" value="1.00" id="c'.$item[number].'">'.
$item[title].'</label>';
}
答案 3 :(得分:0)
你的代码
foreach ($toppingItems as $item) {
echo "<label class="hover topping" for="c$item[number]"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c$item[number]">$item[title]</label>";
}
包含错误:
您不能在双引号PHP字符串中使用未转义的双引号。
尝试:
<?php foreach ($toppingItems as $item) { ?>
<label class="hover topping" for="c<?= $item[number] ?>">
<input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c<?= $item[number]?>"><?= $item[title] ?>
</label>
<?php } ?>
正如你所看到的,我已经习惯了设置我的控制结构,但是我把我的HTML放在php打开和关闭标签之外。我也用过
<?= $item[number] =>
的缩写
<?php echo $item[number] ?>
这是一个偏好问题,我发现它使代码更具可读性。