首页 > 解决方案 > 当表单提交中项目的类别发生变化时,在 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

标签: phpforeach

解决方案


考虑先填充数组,然后再遍历它。喜欢:

$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>";
  }
}

推荐阅读