drvvvuyia15070493 2016-01-29 07:33
浏览 97

将html表单列数组转换为行php

I have an html form which which has multiple textarea and its values are coming from database.

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

}

now when i submit this form i get these arrays of form which has multiple values but they are in column format where as i need it in row form:

By doing this (using implode function ()) i get below output:

$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']);

Current Output by using echo e.g. echo $name == (Boost,Olay):

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)

But i want output in below stated (row) format to use directly in insert query:

(Boost,45,2,500gm,120,75,150),(Olay,35,3,20ml,120,35,105),(Bournvita,20,5,1kg,300,75,1500)

Thanks in advance

  • 写回答

3条回答 默认 最新

  • drls2738 2016-01-29 07:51
    关注

    Sounds like you need a for-loop for that. Provided you already validated that data and made sure no empty 'cell' was passed in.

    $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] . ')';
    

    My PHP skills may not be up to date and it probably could be done better, but I'd say this should work.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了