php - 当表单提交中项目的类别发生变化时,在 foreach 循环中添加标题
问题描述
我使用包含字符串“object_id”和 User_ID (int) 的字段名称收集 POST 变量。我从字段名称中提取用户 ID
在 foreach 循环中我需要生成
头条新闻
每次 $object_ds['transaction_type'] 更改值。但只是在第一次出现在循环之前。我设法为第一行实现了这一目标,但我不知道如何解决 $n>1
任何提示?谢谢
$addonFields = array_intersect_key($_POST, array_flip(preg_grep('/object_id/', array_keys($_POST))));
echo "Anzahl User: ".count($addonFields)."<br>";
$mail_subj_customer = "New! Business Opportunities Pharma & Healthcare";
foreach ($addonFields AS $item=>$value){
echo "Number of Objects: ".count($value)."<br>";
$user_id = preg_replace('/[^0-9]/', '', $item); // Extract User ID from $item
$user_ds = holeDS( 'pmcts_users', 'id', $user_id ); // get DataRow from pmcts_users where id=$user_id
$n=0;
foreach ($value AS $values){
$n++;
$object_ds = holeDS( 'pmcts_unternehmen_produkte', 'object_id', $values ); // get Product DataRow
if ($n==1 && $object_ds['transaction_type']=="Company Sale" ){
$current_trans_type = "Company Sale";
echo "<h2>I. Business Opportunities -Company Sales</h2>";
}
if ($n==1 && $object_ds['transaction_type'] == "Asset Sale"){
$current_trans_type = "Asset Sale";
echo "<h2>II. Business Opportunities - Asset Sales</h2>";
}
if ($n==1 && $object_ds['transaction_type'] == "Licensing Opportunity"){
$current_trans_type = "Licensing Opportunity";
echo "<h2>II. Business Opportunities - Licensing</h2>";
}
echo print_r($values)." - $user_ds[email] - $object_ds[transaction_type]<br>";
}
}
It generates Output with a List of Objects like this for every User
Number User: 8
Number Object: 7
<h2>I. Business Opportunities -Company Sales</h2>
22891 - user@customer.com - Company Sale
20071 - user@customer.com - Company Sale
24591 - user@customer.com - Company Sale
<u>(But needs another headline 'Asset Sales' here)</u>
24531 - user@customer.com - Asset Sale
20491 - user@customer.com - Asset Sale
<u>(But needs another headline 'Licesning' here)</u>
24621 - user@customer.com - Licensing Opportunity
24651 - user@customer.com - Licensing Opportunity
解决方案
考虑先填充数组,然后再遍历它。喜欢:
$headlines = array(
"Company Sale" => "I. Business Opportunities -Company Sales",
"Asset Sale" => "II. Business Opportunities - Asset Sales",
"Licensing Opportunity" => "II. Business Opportunities - Licensing"
);
$allData = array();
foreach ($addonFields AS $item=>$value){
$user_id = preg_replace('/[^0-9]/', '', $item); // Extract User ID from $item
$user_ds = holeDS( 'pmcts_users', 'id', $user_id ); // get DataRow from pmcts_users where id=$user_id
foreach ($value AS $values){
$object_ds = holeDS( 'pmcts_unternehmen_produkte', 'object_id', $values );
if (!isset($allData[$object_ds['transaction_type']])) {
$allData[$object_ds['transaction_type']] = array();
}
$allData[$object_ds['transaction_type']][] = print_r($values, 1)." - $user_ds[email] ;
}
}
然后显示:
foreach ($allData as $transaction_type => $users) {
echo "<h2>" . $headlines[$transaction_type] . "</h2>";
foreach ($users as $user) {
echo "<div>" . $user . " - " . $transaction_type . "</div>";
}
}
推荐阅读
- r - 跨表匹配 R 的行
- dns - 更改 DNS 提供商时需要多少 DNS 记录?
- python - 用斜线填充图例的 geopandas
- vue.js - 如何在 vue.js 中制作按钮动画
- android - 如何检查表是否已存在
- java - 单击按钮时如何动态更改BorderPane中心的视图?
- html - 为什么我的导航栏图标没有出现在我的网站上?
- jupyter - JupyterLab 和 JupyterHub 有什么区别?
- google-app-engine - 网站托管在 App Engine 上,如何访问云存储中的图像?
- php - 使用 HTML2PDF 创建 A4 PDF 时出现 css top 属性问题