Saya mengumpulkan variabel POST dengan Nama Bidang yang berisi String "object_id" dan User_ID (int). Saya mengekstrak ID Pengguna dari Nama Bidang

Di loop foreach yang saya butuhkan untuk menghasilkan

Berita utama

Saya berhasil mencapai ini untuk Baris Pertama, tetapi saya tidak tahu bagaimana menyelesaikannya untuk $n>1

Ada tip? terima kasih


  $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
0
Jackwiz007 25 Mei 2021, 00:52

1 menjawab

Jawaban Terbaik

Pertimbangkan untuk mengisi array terlebih dahulu, lalu mengulanginya nanti. Suka:

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

Kemudian untuk menampilkan:

foreach ($allData as $transaction_type => $users) {
  echo "<h2>" . $headlines[$transaction_type] . "</h2>";
  foreach ($users as $user) {
    echo "<div>" . $user . " - " . $transaction_type . "</div>";
  }
}
0
Kinglish 24 Mei 2021, 22:19