如何解决当表单提交中的项目的类别发生变化时,在 foreach 循环中添加标题
我收集带有包含字符串“object_id”和 User_ID (int) 的 Field-Names 的 POST 变量。 我从字段名称中提取用户 ID
在 foreach 循环中,每次 $object_ds['transaction_type'] 更改值时,我都需要生成
Headlines
。 但仅在循环中第一次出现之前。我设法为第一行实现了这一点,但我不知道如何解决 $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>";
}
}
,
我实现了这样的标题的每个用户基础列表:
$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";
$headlines = array(
"Company Sale" => "I. Business Opportunities - Company Sales","Licensing Opportunity" => "III. Business Opportunities - Licensing"
);
foreach ($addonFields AS $item=>$value){
echo "Number of Objects: ".count($value)."<br>";
$count_objects = count($value);
$user_id = preg_replace('/[^0-9]/',$item); // Extract User ID from $item
$user_ds = holeDS( 'pmcts_users',$user_id ); // get DataRow from pmcts_users where id=$user_id
$flag=1;
$flag2=1;
$flag3=1;
$n=0;
foreach ($value AS $values){
$n++;
$object_ds = holeDS( 'pmcts_unternehmen_produkte',$values ); // get Product DataRow
if ($object_ds['transaction_type']=='Company Sale' && $flag==1) {
echo "<h3>I. Business Opportunities - Company Sales</h3>";
echo $n.' '.print_r($values,true)." - $user_ds[email] - $object_ds[transaction_type]<br>";
$flag=0;
}
elseif ($object_ds['transaction_type']=='Company Sale' && $flag==0) {
echo $n.' '.print_r($values,true)." - $user_ds[email] - $object_ds[transaction_type]<br>";
$flag=0;
}
if ($object_ds['transaction_type']=='Asset Sale' && $flag2==1){
echo "<h3>II. Business Opportunities - Asset Sales</h3>";
echo $n.' '.print_r($values,true)." - $user_ds[email] - $object_ds[transaction_type]<br>";
$flag2=0;
}
elseif ($object_ds['transaction_type']=='Asset Sale' && $flag2==0) {
echo $n.' '.print_r($values,true)." - $user_ds[email] - $object_ds[transaction_type]<br>";
$flag2=0;
}
if ($object_ds['transaction_type']=='Licensing Opportunity' && $flag3==1){
echo "<h3>III. Business Opportunities - Licensing</h3>";
echo $n.' '.print_r($values,true)." - $user_ds[email] - $object_ds[transaction_type]<br>";
$flag3=0;
}
elseif ($object_ds['transaction_type']=='Licensing Opportunity' && $flag3==0) {
echo $n.' '.print_r($values,true)." - $user_ds[email] - $object_ds[transaction_type]<br>";
$flag3=0;
}
}
}
也许对有同样问题的人有帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。