在这种情况下如何使用“和”“如果”

时间:2019-03-26 13:35:33

标签: php

这是有关PHP的基本问题。

  • 我有一个名为“ users”的MySQL数据库,该表包含 两列“下载”和“ vip”
  • “下载”列将计算每个用户的下载量(如果达到) 6它将阻止下载。

我的问题是:我想设置不同类型的用户。例如,对于普通用户,他们只能下载6个文件,对于VIP用户,他们只能下载15个文件。

这是我的代码:

$dl_user = gator::getUser($dl_username);
if ($dl_user['downloads'] > 99){
    echo "<script type='text/javascript'>location.href='https://www.example.com/?signup=1&'</script>";
    die;
}

$dl_user = gator::getUser($dl_username);
if ($dl_user['downloads'] > 6){
    echo "<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>";
die;
}

默认情况下,“没有帐户”的来宾用户将无法下载任何内容,我为他们提供了变量“ 100”,以便将其锁定。

我正在尝试编写如下代码:

  1. 如果用户是访客=>链接(此部分有效)
  2. 如果用户下载量超过6>链接(此部分也有效)
  3. 但是如果用户超过6并且他是VIP,而VIP列是1,则 将用户限制为15个文件(这不起作用,该如何解决?)

4 个答案:

答案 0 :(得分:1)

您可以将VIP列设置为不同的值,以检查用户是否为VIP,例如0 =来宾,1 =成员,2 = VIP ...

    $dl_user = gator::getUser($dl_username);

    if ($dl_user['vip'] == 0){
        // BLOCK ANY download.
    } else if($dl_user['vip'] == 1) {
        // User is a member (allow 6 downloads)
        if($dl_user['downloads'] > 6) {
            //BLOCK the download
        } else {
            //ALLOW download and COUNT
            $dl_user['downloads'] = $dl_user['downloads'] + 1;
        }
    } else if($dl_user['vip'] == 2) {
        // User is a VIP (allow 15 downloads)
        if($dl_user['downloads'] > 15) {
            //BLOCK the download
        } else {
            //ALLOW download and COUNT
            $dl_user['downloads'] = $dl_user['downloads'] + 1;
        }
    } else {
        // User has unknown status (not logged or some error)
    }

如果您需要使用字段下载来保留数据结构以检测用户类型,我建议您仅验证其6、15或100并基于此阻止或允许下载,但是那样您将无法使用该字段计算下载次数...

答案 1 :(得分:1)

我已为您编写代码。我了解您是否还有其他条件。

$dl_user = gator::getUser($dl_username);
if($gustuser){
    echo "<script type='text/javascript'>location.href='https://www.example.com/?signup=1&'</script>";
    die;
}elseif($normaluser && ($dl_user['downloads'] >6 )){
        echo "<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>";
        die;    
}elseif($vipuser && ($dl_user['downloads'] >15 )){
        echo "<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>";
        die;    
}

答案 2 :(得分:1)

如果您查看PHP logical operators documentation,将有一张表显示PHP中的各种逻辑运算符,其中一个是 && 运算符。该运算符的结果是:

  

如果$ a和$ b都为TRUE,则为

考虑到这一点,如果需要在if语句中检查多个条件,则可以使用 && 来完成此操作。可能的解决方案是:

$dl_user = gator::getUser($dl_username);

if ($dl_user['downloads'] > 99){
    echo "<script type='text/javascript'>location.href='https://www.example.com/?signup=1&'</script>";
    die;
}

if ($dl_user['downloads'] > 6){
    echo "<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>";
    die;
}

if ($dl_user['downloads'] > 6 && $dl_user['is_vip'] && $dl_user['vip_col'] === 1 {
      // Code that restricts user to 15 files
}

此示例中显然组成了键 is_vip vip_col ,但是如果数据库中具有相似的字段,则可以编写类似于上面的条件。

答案 3 :(得分:1)

由于只有少数几个条件,因此我会立即测试所有条件:

$echo = 
// is the user have more than 15 downloads OR have more than 5 but not vip?
$dl_user['downloads'] > 14 || ($dl_user['downloads'] > 5 && $dl_user['vip'] < 1) ? 
//we kick him out
"<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>" : 
// does he have 100 downloads (guest)
$dl_user['downloads'] > 99 ? 
// get subscribed
"<script type='text/javascript'>location.href='https://www.example.com/?signup=1&'</script>" : 
//is none of the above? He can downloads
false;

// we echo the result if there is one, or we leave;
echo $echo ? $echo : die();

如果VIP可以不止一个,所以您知道他可以下载多少,我会像这样计算出来

//assuming each vip gives you the right to get 10 more download, I multiply vip by 10
$permited_download = $dl_user[vip] > 0 ? intval($dl_user[vip])*10 : '5';

//then I do the same as the previous exemple but with my calculated variable
$echo = 
// is the user have more than permited downloads (if he's a guest, it's 5) ?
$dl_user['downloads'] > ($permited_download - 1) ? 
//we kick him out
"<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>" : 
// does he have 100 downloads (guest)
$dl_user['downloads'] > 99 ? 
// get subscribed
"<script type='text/javascript'>location.href='https://www.example.com/?signup=1&'</script>" : 
//is none of the above? He can downloads
false;

// we echo the result if there is one, or we leave;
echo $echo ? $echo : die();
相关问题