php
2016-02-15 36 views 0 likes 
0

我想用爆显示输入,如:破灭输入字段

enter image description here

我的代码:

$qty = array('2', '2', '1'); 
$price = array('200', '400', '1000'); 
$subtotal = array('400', '800', '1000'); 
echo "<li> 
Quantity: <input type='text' class='qty' name='qty' value='".implode("'> 
Quantity: <input type='text' class='qty' name='qty' value='",$qty)."'> 
Price: <input type='text' class='price' name='price' value='".implode("'> 
Price: <input type='text' class='price' name='price' value='",$price)."'> 
Subtotal: <input type='text' class='subtot' name='subtot' value='".implode("'> 
Subtotal: <input type='text' class='subtot' name='subtot' value='",$subtotal)."'> 
</li>"; 

我的代码已经给出的结果:

enter image description here

编辑:

我想我的代码应该是这样的格式:

<li> 
    Quantity: <input ..> 
    Price: <input ..> 
    Subtotal: <input ..> 
<li> 
<li> 
    Quantity: <input ..> 
    Price: <input ..> 
    Subtotal: <input ..> 
<li> 
<li> 
    Quantity: <input ..> 
    Price: <input ..> 
    Subtotal: <input ..> 
<li> 

... 
+0

请检查http://php.net/manual/en/function.implode.php。你的数组在哪里是imploade函数? –

+0

@DineshPatra代码已更新。 –

+0

如果你想显示然后爆炸数组,然后将它们的值给你的回声。 –

回答

1

您需要添加一个循环来按顺序添加li元素。尝试下面的内容。以下是您使用所有相同长度数组的想法实现的。

$qty = array('2', '2', '1'); 
$price = array('200', '400', '1000'); 
$subtotal = array('400', '800', '1000'); 
for($i = 0; $i < count($qty); $i++) { 
    echo "<li> 
     Quantity: <input type='text' class='qty' name='qty' value='".$qty[$i]."'> 
     Price: <input type='text' class='price' name='price' value='".$price[$i]."'> 
     Subtotal: <input type='text' class='subtot' name='subtot' value='".$subtotal[$i]."'> 
    </li>"; 
} 

UPDATE用foreach例如:

$qty = array('2', '2', '1'); 
$price = array('200', '400', '1000'); 
$subtotal = array('400', '800', '1000'); 
foreach($qty as $key=>$value) { 
    echo "<li> 
    Quantity: <input type='text' class='qty' name='qty' value='".$qty[$key]."'> 
    Price: <input type='text' class='price' name='price' value='".$price[$key]."'> 
    Subtotal: <input type='text' class='subtot' name='subtot' value='".$subtotal[$key]."'> 
    </li>"; 
} 
+0

任何想法在foreach请? –

+0

绝对。在这种情况下,您只需要使用foreach索引。我会更新我的代码。 – Shuvro

+0

非常感谢。 –

0

如果您在PHP提交这个($ _ POST)它已经是一个Array(),你不必内爆这一个了。

+0

$ SESSION不提交。 –

+0

你指的是'explode()',因为你可以尝试发布你拥有的数组值吗?纠正我,如果我错了 –

1

在这里,我们走了,这是一个非常简单的工作,你只是去通过任何数组,之后只是打印/呼应了整个立与数量,价格,小计在一个单一的迭代。

新的脚本

<?php 
$qty = array('2', '2', '1'); 
$price = array('200', '400', '1000'); 
$subtotal = array('400', '800', '1000'); 
for($i = 0; $i < count($qty); $i++){?> 
    <li> 
     Quantity: <input type='text' class='qty' name='qty' value='<?php echo $qty[$i];?>'/> 
     Price: <input type='text' class='price' name='price' value='<?php echo $price[$i];?>'/> 
     Subtotal: <input type='text' class='subtot' name='subtot' value='<?php echo $subtotal[$i];?>'/> 
</li> 
<?php }?> 

用foreach脚本

<?php 
$qty = array('2', '2', '1'); 
$price = array('200', '400', '1000'); 
$subtotal = array('400', '800', '1000'); 
foreach($qty as $key => $value){?> 
    <li> 
     Quantity: <input type='text' class='qty' name='qty' value='<?php echo $qty[$key];?>'/> 
     Price: <input type='text' class='price' name='price' value='<?php echo $price[$key];?>'/> 
     Subtotal: <input type='text' class='subtot' name='subtot' value='<?php echo $subtotal[$key];?>'/> 
</li> 
<?php }?> 
相关问题