-1

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.

7
  • Use $item['number'] instead of $item[number]. Commented Apr 25, 2016 at 22:09
  • @A.L afraid still getting error <b>Parse error</b>: syntax error, unexpected T_STRING, expecting ',' or ';' in <b>C:\Program Files (x86)\Ampps\www\pizza website\includes\toppings.php</b> on line <b>3</b><br /> on the 'echo' line of code Commented Apr 25, 2016 at 22:11
  • 2
    believe me, it's a quotes issue. This is better class=\"hover topping\" etc. Commented Apr 25, 2016 at 22:15
  • "STATIC CODE I'M LOOKING TO CREATE class="hover topping"" - escaped double quotes if you truly want them to be rendered as double quotes just as I shown you above. Commented Apr 25, 2016 at 22:23
  • 1
    A good development environment can help you prevent this kind of issue. An IDE or text editor with appropriate extensions installed will highlight syntax errors like this as you write your code. Commented Apr 25, 2016 at 22:27

4 Answers 4

1

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

and just to take it one step further, if there's any chance that $item could contain a mix of both single ' and double " quotes, then you'll have to take the split approach in @Kevinvhengst's answer and then wrap your variables with 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>';
Sign up to request clarification or add additional context in comments.

6 Comments

or they can escape them for=\"c$item[number]\" which would be better if ever there should be quotes in values. I always like to render HTML in double quotes ;-) for example class='Bill's topping' would fail if ever that was the case and HTML source would reveal a notice about. Just a quick FYI.
I've waffled on this one for years, and lately I like my declarative HTML/SQL in ' and my procdural JS/PHP in ", and then escape that third tier when I have to. hopefully I don't change my mind tomorrow :)
I discovered this last week actually in pulling in data from a database which contained single quotes and wondered why my code was (sort of) failing; it was because I had used single quotes. Live and learn eh? lol oh well, once can't buy experience ;-) I only discovered it when viewing HTML source; a tool in it own right.
@A.L when it's inside a double quote yes, php.net/manual/en/…
@Fred-ii- you're right - I've been bit by that one more than once, and the escaped quote is most correct, although not so extremely pretty. I will update my answer shortly
|
1

Echo the following:

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

Comments

0

Mixing string and PHP code is confusing, especially when you have double quotes, it's easier to put HTML strings (which usually contain double quotes) in single quotes and PHP expressions outside of these strings:

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>';
}

Comments

0

your code

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

contains an error:

You cannot use unescaped double quotes within a double quoted PHP string.

Try:

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

As you can see I've used to set my control structure, yet I've place my HTML outside of the php open and closing tag. Also I've used

<?= $item[number] => 

as short for

<?php echo $item[number] ?>

This is a matter of preference, I find it makes the code more readable.

3 Comments

You need to enable PHP short tags in your php.ini for <?= to work.
"You cannot use double quotes within a double quoted PHP string." - You can if you escape them, which is a bit more work but that's a preference thing for sure which I occasionally like to use for special cases.
Barmar: <?= ?> always works from php 5.4 see: php.net/manual/en/language.basic-syntax.phptags.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.