我一直在开发一个与订阅系统有关的网站,用户必须从一个级别升级到另一个级别。让我解释一下。有四个计划;基本,银色,金色,Platimum。费用如下:
预付款选项包括:
这些是我的表格;
plans table:
id name cost
1 Basic 0.00
2 Silver 20.00
3 Gold 30.00
4 Platinum 50.00
plans_period table:
id sub_period
1 1
2 3
3 6
4 12
users table:
user_id plan plan_cost user_plan_period balance plan_expiration
1 2 0.00(default) 0 (default) 0.00 (default) NULL (default)
现在,对于用户升级系统,将检查余额表以查看用户必须如何离开,新计划的成本。如果余额超过用户升级到的新计划的成本,升级将成功并重定向到成功页面,否则将显示付款按钮,以便用户使用任何新成本支付任何新成本支付系统集成。此外,如果用户处于计划的中间并且想要升级到更高的计划,则系统还必须对新成本进行分配并同样向用户收费。我一直试图用下面的代码来做这件事。
if ($plan & $period) {
$user = new fncs();
// What plan was chosen?
list($name, $cost) = $db->get_row("select name, cost from plans where id='$plan'", ARRAY_N);
list($sub_period) = $db->get_row("select sub_period from plans_period where sub_period='$period'", ARRAY_N);
list($plan, $user_plan_period, $plan_cost, $plan_expiration) = $db->get_row("select u.plan, u.user_plan_period, u.plan_cost, u.plan_expiration, p.id from users u LEFT JOIN plans p ON p.id=u.plan where u.user_id='{$_SESSION['user_id']}'", ARRAY_N);
$plan_expiration = ($plan_expiration = "NULL") ? '30' : '$plan_expiration';
/*Current Cost of plan chosen */
$totalcost1 = $cost * $sub_period;
/* Cost of the user current plan */
$totalcost2 = $plan_cost * $user_plan_period;
/* Total number of days of the user current plan */
$totaldays1 = 30 * $user_plan_period;
/* Total number of days of the left for the user current plan */
$today = date("m-d-Y");
$dateDiff = strtotime($plan_expiration) - strtotime($today);
$Daysleft = floor($dateDiff/(60*60*24));
$totaldays2 = $totaldays1 - $Daysleft;
$expiredate = strtotime($plan_expiration);
$totalcost = $totalcost1 - ($totalcost2 * ($Daysleft / $totaldays1));
$finalcost = number_format($totalcost, 2);
// sufficient balance?
if ($totalcost > $user->getBalance())
{
$errs[] = 'You have insufficient balance for the upgrade. The total cost is: '.$Daysleft.'';
}
else {
$r = $db->query("update users set plan='$plan', user_plan_period='$period', plan_cost='$cost', plan_expiration=date_add(now(), interval 30 day) where user_id='{$_SESSION['user_id']}'");
// Update balance
$balance = $user->getBalance() - $cost;
$db->query("update users set balance='$balance' where user_id='{$_SESSION['user_id']}'");
// Reset sessions
$user->processLogin($_SESSION['email']);
// Insert into transactions
$txt = "Account upgrade: <strong>$name</strong> for <s>N</s>$cost. Expires ".$_SESSION['plan_expiration'];
$db->query("insert into transactions (user_id, txt, direction, amount, balance, date) values ('{$_SESSION['user_id']}', '$txt', 'd', '$cost', '$balance', now())");
$_SESSION['msg'] = 'Account successfully upgraded. Plan expires on '.$_SESSION['plan_expiration'];
header('location:my-account.php');
exit;
}
}
以下是用户将选择要升级到
的计划的表单<div class="mainbar">
<h3>Upgrade Account</h3>
<?php
if (isset($errs)) {
echo '<p class="alert">';
foreach($errs as $v){
echo "$v ";
}
echo '</p>';
}
?>
<form class="form" method="post">
<div class="rc">
<p><label>Current Balance</label> <b><s>N</s><?= number_format($_SESSION['balance'], 2); ?></b></p>
<p><label>Plan</label>
<select class="txt iwdt" name="plan">
<?php
$r = $db->get_results("select id, name, cost from plans where id > 1");
foreach ($r as $v) {
list ($id, $name, $cost) = $v;
echo '<option value="'.$id.'"';
echo $id == $_SESSION['plan'] ? ' selected="selected"' : '';
echo '>'.$name.' for N'.$cost.'</option>';
}
?>
</select>
<select class="txt iwdt" name="period">
<option value="">Select Period</option>
<?php
$r = $db->get_results("select id, sub_period from plans_period");
foreach ($r as $v) {
list ($id, $sub_period) = $v;
echo '<option value="'.$id.'"';
echo $id == $_SESSION['period'] ? ' selected="selected"' : '';
echo '>'.$sub_period.' month(s)</option>';
}
?>
</select>
</p>
<p>
<label> </label> <input type="submit" name="submit" class="button" value="Upgrade" /> or <a href="my-account.php">Cancel</a>
</p>
</div>
</form>
</div>
有人能指出我正确的方向吗?有没有我可以从中学到的教程?感谢。
感谢所有回复的人。
基本上这是我认为我有一些挑战的地方
$plan_expiration = ($plan_expiration = "NULL") ? '30' : '$plan_expiration';
此plan_expiration字段是此格式年日月份的日期字段,例如2011-06-03。现在,它的默认值是“NULL”。当用户首次注册我们的网站时,用户将分配一个基本会员计划,其成本为0.00美元,其到期日期设置为“NULL”。
如果用户想升级,我们必须计算他们的计划到期前剩余的天数,因为我们将根据剩余的天数对此进行分配。所以我做了这个
如果根据上面的代码将其取消,我将$ 30分配给$ plan_expiration。然后我使用下面的语法计算剩余的天数
$today = date("m-d-Y");
$dateDiff = strtotime($plan_expiration) - strtotime($today);
$Daysleft = floor($dateDiff/(60*60*24));
第一个错误是$ Daysleft给我一个负值
答案 0 :(得分:0)
您还需要实际的注册日期来计算缺失的天数。
让我们使用这个例子:
我们于5月15日(2011-05-15)注册,并假设30天到期(导致6月15日到期(2011-06-15)。
PHP计算可以这样工作:
$signupDate = strtotime('2011-05-15');
$expiration = 30; //this is in days, lets remember that for later
$expirationDate = $signupDate+($expiration*24*60*60); //calculate the expiration date
$today = time(); //theres no need for using date and strtotime of you just want the timestamp
$daysLeft = floor(($expirationDate-$today)/(60*60*24));
如果$daysLeft
现在有负值(不应该发生),那么他的帐户已经过期了。但是,由于您的应用程序的运行方式,这通常会产生正值,为您提供用户计划的剩余时间。