PHP抓取数组来创建代码行 [英] PHP grabbing arrays to create lines of code

查看:53
本文介绍了PHP抓取数组来创建代码行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造一些简单的东西。由于我是PHP新手,所以我很努力地看到我做错了什么。我在下面的代码片段中有两个PHP文件内容,以及我期望实现的静态代码。


$ b

PHP TOPPINGS



 <?php 
foreach($ toppingItems as $ item){
echo< label class =hover toppingfor = c $ item [number]>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00id =c $ item [number]> ; $项[标题] LT; /标签>;
}
?>



PHP ARRAYS



 < code $<?php 

// Pizza Toppings

$ toppingItems = array(

array(
number =4,
title=>EXTRA CHEESE
),
数组(
number=>5,
title=>TOMATOES
),
数组(
number=>6,
title=>OLIVES
),
数组(
number=>7,
title=>MUSHROOMS
),
array (
number=>8,
title=>CHICKEN
),
array(
number=>
title=>MOZZARELLA
),
数组(
number=>10,
title =SALAMI
),
数组(
number=>11,
title=>ONIONS
) ,
数组(
number=>1 2,
title=> PEPPERONI
),
数组(
number=>13,
title=>乱糟糟的外壳
),
array(
number=>14,
title=>MEATBALLS
),
array(
number =15,
title=>BACON
),
数组(
number=>16,
title=>HAM
),
数组(
number=>17,
title=>SHRIMPS
),


);
?>



静态代码我正在创建



 < label class =hover toppingfor =c4>< input class =itemsonclick =findTotal()type =checkboxname = toppingvalue =1.00id =c4> EXTRA CHEESE< / label> 
< label class =hover toppingfor =c5>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = C5 >蕃茄< /标签>
< label class =hover toppingfor =c6>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = C6 >橄榄< /标签>
< label class =hover toppingfor =c7>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = C7 >蘑菇及LT; /标签>
< label class =hover toppingfor =c8>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = C8 >鸡< /标签>
< label class =hover toppingfor =c9>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = C9 >莫扎莱< /标签>
< label class =hover toppingfor =c10>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = 10 > SALAMI< /标签>
< label class =hover toppingfor =c11>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = C11 >葱< /标签>
< label class =hover toppingfor =c12>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = 12 > PEPPERONI< /标签>
< label class =hover toppingfor =c13>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 id =c13>乱糟糟的外壳< / label>
< label class =hover toppingfor =c14>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = 14 >肉丸< /标签>
< label class =hover toppingfor =c15>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = C15 >培根LT; /标签>
< label class =hover toppingfor =c16>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = C16 > HAM< /标签>
< label class =hover toppingfor =c17>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00 ID = C17 > SHRIMPS< /标签>

是的,我确定toppings.php文件和arrays.php文件连接到索引文件。



感谢您提前给予您的帮助,如果您仍然不确定我想要达到的目标,请随时给我发消息。

$ b $在你的双引号中使用双引号 code $ <$ p
$ b

  echo< label class =hover toppingfor =c $ item [number] >< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00id =c $ item [number]> $ item [title]< ; /标签>; 

尝试用单引号包装您的HTML属性'

  echo< label class ='hover topping'for ='c $ item [number]'>< input item ='items'onclick ='findTotal()'type ='checkbox'name ='topping'value ='1.00'id ='c $ item [number]'> $ item [title]< / label> ; 

谢谢 Fred 指出 a>,如果有可能,你的值(例如 $ item )可能包含单引号',那么您必须使用反斜杠 \

 <$ c来转义内部双引号$ c> 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 可能包含单个'和double 的组合引号,那么你必须采取拆分方法在 @ Kevinvhengst's答案,然后用 htmlentities

  echo'< label class =hover toppingfor =c。 ('item''number'])。'>< input class =itemsonclick =findTotal()type =checkboxname =toppingvalue =1.00id =c .htmlentities($项[ '编号']) '>'。ヶ辆($项[ '标题'])。 '< /标签>'; 


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.

解决方案

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

这篇关于PHP抓取数组来创建代码行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆