简体   繁体   中英

How do I use “and” “if” in this situation

It's a basic question about PHP.

  • I have MySQL database with table named "users" and this table contain two columns "downloads" and "vip"
  • Downloads column will count the download for each user if it reached 6 it will block the download.

My problem is: I want to setup different types of users. For example for normal users they only can download 6 files and for VIP users they can only download 15 file.

This is my code:

$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;
}

By default guests users "without account" will not be able to download anything, and I gave them variable "100" so it will lock it.

I'm trying to make the code like this:

  1. If user is guest => link (this part works)
  2. If user download exceed 6 > link (also this part works)
  3. But if the user exceed 6 and he is a VIP and the VIP column is 1 then restrict the user to 15 file (This does not work, how can I solve it?)

You could set VIP column to different values to check if the user is a VIP or not, example 0 = guest, 1 = member, 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)
    }

If you need to keep the data structure using the field download to detect the user type, i suggest just verifying if its 6, 15 or 100 and blocking or allowing the download based on that, but that way you wont be able to count the downloads using that field...

I have write code for you. I thing you understand about if else condition.

$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;    
}

If you look at the PHP logical operators documentation , there is a table that shows the various logical operators in PHP, one of which is the && operator. The result of this operator is:

TRUE if both $a and $b are TRUE.

With this in mind, if you need to check for multiple conditions in an if statement, you could use && to accomplish this. A posible solution for your problem could be:

$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
}

The keys is_vip and vip_col are obviously made up in this example, but if you have similar fields in your database you could write a conditional similar to the above.

I would test all my conditions at once since there's only few:

$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();

And if the VIP can be more than one so you know how many he can download, I would calculate it before like so

//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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM