首页 > 解决方案 > 组合 2 个 For Each 循环 PHP

问题描述

我正在用 PHP 编写一个应用程序,它基本上会根据用户的输入生成动态页面。

我有变量的

$_POST['config'];
$_POST['configsize']; 

通过 POST 数据以 CSV 格式提交。

我关心的是在表格中显示提交的数据。

<table class="table table-hover">
  <tr class="text-capitalize">
    <th>Unit Types</th>
    <th>Unit Size</th>
    <th>Get Price</th>
  </tr>
';  // end content

// add <li>
$amenities1 = $_POST['config'];
$amenities2 = $_POST['configsize'];
$amenitisArr1 = explode(",", $amenities1);
$amenitisArr2 = explode(",", $amenities2);

// add values to html
foreach ($amenitisArr1 as $str1) {
    $content_to_write .= '<tr><th>'.$str1.'</th>';
}
// add values to html
foreach ($amenitisArr2 as $str2) {
    $content_to_write .= '<th>'.$str2.'</th>';
    $content_to_write .= '<th>Book Now</th></tr>';
}
// continue in content
$content_to_write .= '</table>

我得到了输出,但是表格的对齐方式被折腾了!!

请帮助更正数据。

输出应该是这样的:

<table>
<tr>
<th>Unit Types</th>
<th>Units Size</th>
<th>Book Now</th>
</tr>

<tr> --- I want my For Each Loop to kick in here for as many times there is a CSV
<td>xxx</td>
<td>xxx</td>
<td>xxx</td>
</tr>

</table> 

继续我的输出:

$amenities1 = $_POST['config'];
$amenities2 = $_POST['configsize'];

这是 CSV。这将从 $_POST['config'] 和 $_POST['configsize'] 发布

$amenities1 = 2BHK,3BHK
$amenities2 = 1092 Sq.ft,2098 Sq.ft

这是带有新代码的当前输出

这是预期的输出。

图 1 是电流输出。图 2 是预期的输出。

标签: phpforeach

解决方案


您应该upload_tmp_dir在您的php.ini- 它不能在运行时设置,因为在您的脚本开始运行时,上传已经完成并且内容已经存储在临时文件中。

move_uploaded_file()如果您需要动态创建此文件夹的名称 - 除了首先确保所需路径存在(例如,通过将第三个参数设置mkdir()为 TRUE)时使用之外,您别无选择


推荐阅读