当人们在页面中多次捐款时, 它只会显示他们最近的捐款, 而不是他们的总捐款(在捐款列表中)。例如, 如果我现在捐赠5美元, 以后再捐赠10美元, 它将仅显示10美元, 而不显示5美元。那是网站:gesher-jds.org/giving-tuesday
我如何解决它?
<div class="donors_info">
<div class="totalenteries"></div>
<?php
$valueid = 2727;
//Get donors based on forms IDs (can be single or multiple forms)
if(function_exists('give_get_payments')) {
$args = array(
'give_forms' => $valueid, 'number' => -1, );
$donations = give_get_payments( $args );
echo "<table id='dtab'>";
echo "<thead>";
echo "<th align='center'>" . 'Name' . "</th>";
echo "<th align='center'>" . 'Amount' . "</th>";
echo "</thead>";
echo "<tbody>";
$donors = array();
foreach($donations as $donation) {
//Now get donor information from this donation ("customer" aka "donor")
$customer_id = give_get_payment_customer_id( $donation->ID );
$customers = new Give_Customer( $customer_id );
$nameis = $customers->name;
if (in_array($nameis, $donors)) {} else {
$amt = get_post_meta($donation->ID, '_give_payment_total', true) + 0;
echo "<tr class='select'>";
echo "<td align='left'>" . $nameis . "</td>";
echo "<td align='center'>" . $purchaseis = "$" . $amt . "</td>";
echo "</tr>";
$donors[] = $nameis;
}
}
echo "</tbody>";
echo "</table>";
}
?>
</div>
#1
$amt = get_post_meta($donation->ID, '_give_payment_total', true)
” true”仅返回指定元键的第一个值, ” false”将为你提供元值数组。你可以使用foreach迭代返回的数组。
get_post_meta()
编辑:
是的, 你需要将其更改为false, 然后获得一个值数组。现在, 你仅获得一个值。
然后迭代数组:
$amt = get_post_meta($donation->ID, '_give_payment_total', false);
foreach ($amt as as $key => $value) {
echo "<tr class='select'>";
echo "<td align='left'>" . $nameis . "</td>";
echo "<td align='center'>" . $purchaseis = "$" . $value . "</td>";
echo "</tr>";
}
代码未经检查, 但这是可行的方法。有关foreach的一些信息:
https://www.php.net/manual/de/control-structures.foreach.php
编辑2:如果要在一行中显示所有捐款:
<div class="donors_info">
<div class="totalenteries"></div>
<?php
$valueid = 2727;
//Get donors based on forms IDs (can be single or multiple forms)
if(function_exists('give_get_payments')) {
$args = array(
'give_forms' => $valueid, 'number' => -1, );
$donations = give_get_payments( $args );
echo "<table id='dtab'>";
echo "<thead>";
echo "<th align='center'>" . 'Name' . "</th>";
echo "<th align='center'>" . 'Amount' . "</th>";
echo "</thead>";
echo "<tbody>";
$donors = array();
$dtmp=array(); //tmp array
foreach($donations as $donation) {
//Now get donor information from this donation ("customer" aka "donor")
$customer_id = give_get_payment_customer_id( $donation->ID );
$customers = new Give_Customer( $customer_id );
$nameis = $customers->name;
$amt = get_post_meta($donation->ID, '_give_payment_total', false);
$donationAll = 0;
foreach ($amt as $key => $value) {
$donationAll = $donationAll + $value;
}
//Write a tmp array with name as key and donation as value
if(array_key_exists($nameis, $dtmp)) {
//name already exists -> add donation value
$dtmp[$nameis] = $dtmp[$nameis] + $donationAll;
} else {
//name is not in the array -> new array: key name -> donation
$dtmp[$nameis] = $donationAll;
}
$donors[] = $nameis;
}
foreach ($dtmp as $key => $value) {
//iterate the tmp array
echo "<tr class='select'>";
echo "<td align='left'>" . $key . "</td>";
echo "<td align='center'>" . $purchaseis = "$" . $value . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
}
?>
</div>
代码未选中。
问候汤姆
评论前必须登录!
注册