2

我一直在开发一个与订阅系统有关的网站,用户必须从一个级别升级到另一个级别。让我解释一下。有四个计划,即;基本款、白银款、黄金款、白金款。费用如下:

  • 基本:0.00 美元
  • 白银:20.00 美元
  • 黄金:30.00 美元
  • 白金奖:50.00 美元

预付款选项有:

  • 1个月
  • 3个月
  • 6个月
  • 12个月。

这些是我的桌子;

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>&nbsp;</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 返回一个负值

4

1 回答 1

1

您还需要实际的注册日期来计算缺失的天数。

让我们使用这个例子:

我们在 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现在有一个负值(这不应该发生),他的帐户已经过期了一段时间。但是由于您的应用程序的工作方式,这通常会产生一个正值,为您提供用户计划的剩余天数。

于 2011-06-03T22:26:39.357 回答