Josh Josh - 7 months ago 8
PHP Question

PHP grabbing arrays to create lines of code

I would like to create something simple. Since I'm new to PHP, I'm struggling to see what I'm doing wrong,. I have two PHP files content in code snippets below, as well as the static code I'm looking to achieve.

PHP TOPPINGS



<?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 ARRAYS



<?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"
),


);
?>


STATIC CODE I'M LOOKING TO CREATE



<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>


And yes I have made sure the toppings.php file and the arrays.php file are connected to the index file.

Thank you for your help in advance and if your still unsure what I'm looking to achieve feel free to message me.

Answer

you're using double quotes " inside double quotes "

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>";

try wrapping your HTML attributes with single quotes '

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>";

thank Fred for pointing out that if there's any possibility your values, such as $item, could contain a single quote ' themselves, then you'll have to escape the inner double quotes with a backslash \"

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>";