首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为网站制定订阅计划

为网站制定订阅计划
EN

Stack Overflow用户
提问于 2011-06-03 20:30:43
回答 1查看 4.6K关注 0票数 2

我一直在致力于一个与订阅系统相关的网站,用户将不得不从一个级别升级到另一个级别。让我解释一下。有四个计划,即:基本计划,银,黄金,普拉蒂玛。费用如下:

$30.00

  • Platimum:

  • Basic:$0.00

  • Silver:$20.00

  • Gold:

$50.00

预缴款项的选择如下:

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

F 219

这些是我的桌子;

代码语言:javascript
复制
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)

现在对于一个用户升级系统将检查平衡表,看看用户必须如何离开,新计划的成本。如果余额超过用户升级到的新计划的成本,则升级将成功并重定向到成功页面,如果不是,将显示支付按钮,让用户使用任何集成的支付系统支付任何新成本。此外,如果用户正在计划的中间,并希望升级到更高的计划,系统也将不得不预先评估新的成本,并向用户收取同样的费用。我一直试图用下面的代码来完成这个任务。

代码语言:javascript
复制
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;
    }
}

下面是用户选择要升级到的计划的表单。

代码语言:javascript
复制
<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>

有人能帮我找到正确的方向吗。这上面有我可以学习的教程吗?谢谢。

感谢所有的答复。

基本上这就是我觉得我面临挑战的地方

代码语言:javascript
复制
$plan_expiration = ($plan_expiration = "NULL") ? '30' : '$plan_expiration';

此plan_expiration字段是此格式的日期字段,如2011-06-03年月日.现在这个值的默认值是"NULL“。当用户第一次注册到我们的网站,用户将分配一个基本的会员计划,费用是$0.00,他们的到期日期设置为"NULL“。

如果用户想升级,我们必须计算他们的计划到期前剩下的天数,因为我们将根据所剩的天数对此进行评估。所以我做了这个

如果根据上面的代码将"30“赋值为空,则将”30“分配给$plan_expiration。然后,我使用下面的语法计算了剩下的天数

代码语言:javascript
复制
 $today = date("m-d-Y"); 
    $dateDiff = strtotime($plan_expiration) - strtotime($today);
    $Daysleft = floor($dateDiff/(60*60*24));

第一个错误是$Daysleft正在返回一个负值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-03 22:26:39

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

让我们使用这个例子:

我们在5月15日(2011-05-15)注册,并假定30天到期(这将导致6月15日(2011-06-15)到期)。

PHP计算可以这样工作:

代码语言:javascript
复制
$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现在有一个负值(这是不应该发生的),他的帐户已经过期一段时间了。但是,由于您的应用程序的工作方式,这通常会产生一个积极的价值,给您的日子,为用户的计划。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6232344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档