我正在尝试改进一个打印带有db值的列表的php脚本,我需要做一些复杂的(对我来说)操作来实现我想要的:
列出打印月份以及是否支付的列表,奖励值为前3个ID以及用户已经收到的额外奖励值。
if ($stmt = $mysqli->prepare(" SELECT members.*, account_type.*, friends.*, friendsCount.friendID,
COUNT(friendsCount.friendID) AS num_f
FROM members
INNER JOIN account_type ON account_type.id = ?
INNER JOIN friends ON friends.friendID = ?
INNER JOIN friends AS friendsCount ON friendsCount.userID = ?
WHERE members.id = ?")) {
$stmt->bind_param('iiii', $_SESSION['acc_type'], $_GET['id'], $_SESSION['user_id'], $_GET['id']);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array();
$monthNames = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
$paid = array(); //Placeholder for paid months.
for ($i = 1; $i < 13; $i++) {
$month = 'month' . $i;
$bonus_month = 'bonus_month' . $i;
// check if the user received the monthly bonus
if ($row[$bonus_month] == 0) {
// check if the friend's member account is active
if ($row['status'] == 1) {
// check if the friend paid the month
if ($row[$month] == 1) {
$paid[] = 'Pagado';
// check the user account type
if ($row['type'] == 'Base') {
// TODO: check if the friend is one of the three first (special promotion)
// (user gets double bonus for the first three invited friends)
if ($row['extra'] == 1) {
$extra = $row['bonus'] + 5;
$bonus[] = $extra . '€';
}
else {
$bonus[] = $row['bonus'] . '€';
}
}
else {
$bonus[] = $row['bonus'] . '€';
}
$cashed[] = 'No';
$non_cashed[] = $bonus[];
}
else {
$paid[] = 'No Pagado';
$bonus[] = 'n/a';
$cashed[] = 'n/a';
}
}
else {
$paid[] = 'n/a';
$bonus[] = 'n/a';
$cashed[] = 'n/a';
}
}
else {
$paid[] = 'Pagado';
$bonus[] = $row[$bonus_month] . '€';
// TODO: find a way to check if user already received the bonus
// 12 new columns? (bonus_received_month1-12)
if (1 == 0) {
$cashed[] = 'Si';
$total_cashed[] = $row[$bonus_month];
}
else {
$cashed[] = 'No';
$non_cashed[] = $row[$bonus_month];
}
}
}
//Now make the HTML list
foreach ($monthNames as $key => $month) {
echo '
<div class="list">
<ul>
<li><a class="month">' . $month . '</a></li>
<li><a class="status">' . $paid[$key] .'</a></li>
<li><a class="bonus">' . $bonus[$key] . '</a></li>
<li><a class="cashed">' . $cashed[$key] . '</a></li>
</ul>
</div>';
}
} else echo $mysqli->error;
实际上它工作得很好但有些事情还没有实现:
1º前3个ID的额外奖励值(不包括状态0账户) 2º一种检查用户是否已收到奖金的方法,可以显示余额和收到的总数:
首先,我想改进增加额外奖励的方法:
// this column contains 1 for the first 3 friendIDs
if ($row['extra'] == 1) {
$extra = $row['bonus'] + 5;
$bonus[] = $extra . '€';
} else $bonus[] = $row['bonus'] . '€';
要做到这一点:我必须检查前3个friendID(其中userID = $ _SESSION [user_id])并比较其中一个是否匹配当前的$ _GET ['id']。
// the number of friends
$count = $row['num_f'];
// the current friendID
$f_id = $_GET['id'];
$match = 'the first 3 ids (with acc status 1)';
if ($count > 2 && $f_id == $match) {
$extra = $row['bonus'] + 5;
$bonus[] = $extra . '€';
} else $bonus[] = $row['bonus'] . '€';
统计信息:
// not received
echo '<p>Total acumulado: ' . array_sum($non_cashed[]) . '€</p>';
// total received
echo '<p>Total recibido: ' . array_sum($total_cashed[]) . '€</p>';
我面临的一个大问题是:如果三个第一个帐户中的一个处于非活动状态(状态0),如何“接受”下一个活动的帐户......
如果帐户1处于非活动状态但2和3不是,则帐号4应该有资格获得额外奖励。这些数字只是“来自朋友的select friendID,其中userID =?”
的顺序我假设我可以使用array_key_exists来检查$ f_id是否存在于包含前3个ID的数组中...
我该怎么做?对不起,如果我犯了一些错误,我会工作几个小时。
表格是:
1º成员:获取朋友信息 - &gt; “id”,“status”(0或1;非活动/活动),“name”和“month1-12”(12列:0或1;未付费/已付费月份)
2ºcount_type:获取用户acc类型和奖金值 - &gt; “id”(members.acc_type),“type”(类型名称)和“bonus”(5,10,15等......)
3º朋友:获取与他的朋友相关的用户信息 - &gt; “friendID”(members.id,受邀用户),“userID”(members.id,邀请朋友的用户),“额外”(前3名受邀用户1名,其余0名; 1名双倍奖金,0是正常奖金。此列将被删除),“bonus_month1-12”(12列; 0表示当前月份或未来月份,1 +是用户本月获得的奖励)
要检查用户是否已收到奖金,我想添加12个列来了解它。除非建议使用更好的方法,否则对于未收到和收到1,它将为0:)