我有一个html表单,其中包含多个textarea,其值来自数据库。
for (int i = 0; i < queueArray.length && queueArray[i] != null; i++)
{
result+= queueArray[i].toString() + ",";
}
result = result.Substring(0,result.length() - 1);
现在,当我提交此表单时,我得到了这些具有多个值的表单数组,但它们是列格式,我需要以行格式:
while($row=mysql_fetch_array($record)){
echo "<tr class='item-row'>";
echo "<td>" .$startSrno. "</td>";
echo "<td class='item-name'> <div class='delete-wpr'><textarea name='productName[]'>".$row['name']."</textarea></div></td>";
echo "<td>".$row['pkgsize']."</td>";
echo "<td><textarea class='savings' name='saving[]'>".$row['savings']."</textarea></td>";
echo "<td><textarea class='mrp' name='mrp[]'>".$row['MRP']."</textarea></td>";
echo "<td><textarea class='qty' name='qty[]'>1</textarea></td>";
echo "<td><textarea class='cost' name='saleprice[]'>".$row['saleprice']."</textarea></td>";
echo "<td><textarea class='price' name='total[]'>".$row['saleprice'] * '1'."</textarea></td>";
echo '<input type="hidden" name="productId[]" value="'.$row['id']. '">';
echo '<input type="hidden" name="pkgsize[]" value="'.$row['pkgsize']. '">';
echo "</tr>";
$startSrno++;
}
$id = implode(',',$_POST['productId']);<br/>
$name = implode(',',$_POST['productName']);
$saving = implode(',',$_POST['saving']);
$qty = implode(',',$_POST['qty']);
$pkgsize = implode(',',$_POST['pkgsize']);
$mrp = implode(',',$_POST['mrp']);
$saleprice = implode(',',$_POST['saleprice']);
$total = implode(',',$_POST['total']);
Boost,Olay,Bournvita (Name)
45,35,20 (Savings)
2,3,5 (qty)
500gm,20ml,1kg(pkgsize)
120,70,320(mrp)
75,35,300(saleprice)
150,105,1500(total)
提前致谢
答案 0 :(得分:0)
听起来你需要一个for循环。如果您已经验证了该数据并确保没有传入空的“单元格”。
$products = '(' . $name[0] . ',' . $saving[0] . ',' . $qty[0] . ',' . $pkgsize[0] . ',' . $mrp[0] . ',' . $saleprice[0] . ',' . $total[0] . ')';
for($i = 1; $i < count($id); $i++)
$products = ',(' . $name[i] . ',' . $saving[i] . ',' . $qty[i] . ',' . $pkgsize[i] . ',' . $mrp[i] . ',' . $saleprice[i] . ',' . $total[i] . ')';
我的PHP技能可能不是最新的,它可能会做得更好,但我会说这应该有效。
答案 1 :(得分:0)
$b = '';
for ($i=0; $i<count($_POST['productId']); $i++) {
$a = array(
//$_POST['productId'][$i],
$_POST['productName'][$i],
$_POST['saving'][$i],
$_POST['qty'][$i],
$_POST['pkgsize'][$i],
$_POST['mrp'][$i],
$_POST['saleprice'][$i],
$_POST['total'][$i]
);
if ($b != '') $b .= ',';
$b = '('.implode(',',$a).')';
}
echo $b;
答案 2 :(得分:0)
$oddArray = array();
$evenArray = array();
$insertValues = '';
for ($i=0; $i<count($_POST['productId']); $i++) {
if ($i % 2 == 0) {
//echo $i."<br/>";
$insertValues .= '(';
$evenArray = array(
//$_POST['productId'][$i],
'\''.$_POST['productName'][$i].'\'',
$_POST['saving'][$i],
$_POST['qty'][$i],
'\''.$_POST['pkgsize'][$i].'\'',
$_POST['mrp'][$i],
$_POST['saleprice'][$i],
$_POST['total'][$i]
);
$insertValues .= implode(',',$evenArray);
$insertValues .= '),';
}else{
//echo $i."<br/>";
$insertValues .= '(';
$oddArray = array(
//$_POST['productId'][$i],
'\''.$_POST['productName'][$i].'\'',
$_POST['saving'][$i],
$_POST['qty'][$i],
'\''.$_POST['pkgsize'][$i].'\'',
$_POST['mrp'][$i],
$_POST['saleprice'][$i],
$_POST['total'][$i]
);
$insertValues .= implode(',',$oddArray);
$insertValues .= '),';
}
}
$insertValuesTrim = rtrim($insertValues,',');