Page 1 of 1

Level Restrictions Help Please [Resolved]

Posted: Mon Sep 02, 2013 4:12 pm
by Epiales
Okay, I have a business section where users can purchase different businesses. The problem I have is that I've tried forever now to implement a level restriction on the businesses. I've tried using if their user level is less than 10, echo a message and such, but doesn't seem to work no matter where I place it. Any help would be great. Here is the code I have for the businesses. Thx!

Code: Select all

 <?php    
///////////////////////////////////////////////////BEGINNING OF GOLD INCOME BUSINESSES////////////////////////////////////////////////

    //user buys a business Fruit Stand//
    if(isset($_POST['business'])){
        $fruit = protect($_POST['fruit']);
        $gold_needed = (($business['fruit'] * 1) * $fruit);
        if($fruit < 0){
            output("You must purchase a positive number of Fruit Stands!");
        }elseif($stats['gold'] < $gold_needed){
            output("You do not have enough gold!");
        }else{
            $business['fruit'] += $fruit;            
            $update_business = mysql_query("UPDATE `business` SET `fruit`='".$business['fruit']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            $stats['gold'] -= $gold_needed;
            $update_gold = mysql_query("UPDATE `stats` SET `gold`='".$stats['gold']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            include("update_businesses.php");
            output("You have purchased Fruit Stands!");
    }
}
    //user buys a business Tire Shop//
    
 if(isset($_POST['business'])){
        $tire = protect($_POST['tire']);
        $gold_needed = (($business['tire'] * 2) * $tire);
        if($tire < 0){
            output("You must purchase a positive number of Tire Shops!");
        }elseif($stats['gold'] < $gold_needed){
            output("You do not have enough gold!");
        }else{     
            $business['tire'] += $tire;
            $update_business = mysql_query("UPDATE `business` SET `tire`='".$business['tire']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            $stats['gold'] -= $gold_needed;
            $update_gold = mysql_query("UPDATE `stats` SET `gold`='".$stats['gold']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            include("update_businesses.php");
            output("You have purchased businesses!");
    }        
}
//user buys a business Parts Store//
 if(isset($_POST['business'])){       
        $parts = protect($_POST['parts']);
        $gold_needed = (($business['parts'] * 5) * $parts);
        if($parts < 0){
            output("You must purchase a positive number of Part Stores!");
        }elseif($stats['gold'] < $gold_needed){
            output("You do not have enough gold!");
        }else{            
            $business['parts'] += $parts;            
            $update_business = mysql_query("UPDATE `business` SET `parts`='".$business['parts']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            $stats['gold'] -= $gold_needed;
            $update_gold = mysql_query("UPDATE `stats` SET `gold`='".$stats['gold']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            include("update_businesses.php");
            output("You have purchased businesses!");
    }
}
//user buys a business Chop Shop//
 if(isset($_POST['business'])){        
        $chop = protect($_POST['chop']);
        $gold_needed = (($business['chop'] * 10) * $chop);
        if($chop < 0){
            output("You must purchase a positive number of Chop Shops!");
        }elseif($stats['gold'] < $gold_needed){
            output("You do not have enough gold!");
        }else{           
            $business['chop'] += $chop;            
            $update_business = mysql_query("UPDATE `business` SET `chop`='".$business['chop']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            $stats['gold'] -= $gold_needed;
            $update_gold = mysql_query("UPDATE `stats` SET `gold`='".$stats['gold']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            include("update_businesses.php");
            output("You have purchased businesses!");
    }
                

///////////////////////////////////////////////////END OF GOLD INCOME BUSINESSES////////////////////////////////////////////////
        
?>

Re: Level Restrictions Help Please

Posted: Mon Sep 02, 2013 9:32 pm
by Jackolantern
So you mean you want the user to get a level requirement check when they try to buy the business, and each business will have a different level requirement?

If so, I would probably wrap the level check around each business' ending ELSE clause.

Re: Level Restrictions Help Please

Posted: Mon Sep 02, 2013 10:39 pm
by Epiales
Jackolantern wrote:So you mean you want the user to get a level requirement check when they try to buy the business, and each business will have a different level requirement?

If so, I would probably wrap the level check around each business' ending ELSE clause.
Yes, when they first register, they are able to purchase the first business. Then when they reach like, level 10, then they can purchase the next one. And so forth.

Re: Level Restrictions Help Please

Posted: Tue Sep 03, 2013 5:37 am
by MikuzA
you have a nice codebase there already. So as Jackolantern said, IF-ELSE statement wrappings and there ye go.

And to get the logic as you mentioned,

The most simple solution is to do the following restriction on the leveling:
IF($level == 0 && $level == 10 && $level == 20) { show_all_the_businesses_the_player_can_buy; }

As an example, but you seem to be quite far on PHP so perhaps you do not need that much code assistance :)

Re: Level Restrictions Help Please

Posted: Tue Sep 03, 2013 6:08 pm
by Epiales
I always have so much trouble with access levels and levels grrrr....

Okay, I have the businesses where you can purchase all of them but the tire shop. No matter if I'm under level 10 or over level 10, it won't let me purchase one. Why?

Code: Select all

 elseif(isset($_POST['business'])){
 if($stats['level'] < 10) {
        echo "You must be level 10 to purchase Tire Shops";
        }
 if($stats['level'] >=10) {

        $tire = protect($_POST['tire']);
        $gold_needed = (($business['tire'] * 2) * $tire);
        if($tire < 0){
            output("You must purchase a positive number of Tire Shops!");
        }elseif($stats['gold'] < $gold_needed){
            output("You do not have enough gold!");
        }else{     
            $business['tire'] += $tire;
            $update_business = mysql_query("UPDATE `business` SET `tire`='".$business['tire']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            $stats['gold'] -= $gold_needed;
            $update_gold = mysql_query("UPDATE `stats` SET `gold`='".$stats['gold']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            include("update_businesses.php");
            output("You have purchased businesses!");

Re: Level Restrictions Help Please

Posted: Tue Sep 03, 2013 6:45 pm
by MikuzA
Do you get in both cases the 'You must be level 10 to purchase Tire Shops'?

Just for the kicks,
does it work if you test it like this:

Code: Select all

$stats['level'] = 9; // Now you should get the 'you must be dididi' message.
//$stats['level'] = 10; // Now you should be able to buy.
if($stats['level'] < 10) {
        echo "You must be level 10 to purchase Tire Shops";
        }
 if($stats['level'] >=10) { 
By overwriting the variable and confirming it to work as expected, you can direct your troubleshoot towards the database query result.
Since from the looks of it, that should work.

Re: Level Restrictions Help Please

Posted: Tue Sep 03, 2013 7:02 pm
by Epiales
Okay I did this:

Code: Select all

     elseif(isset($_POST['business'])){
$stats['level'] = 9; // Now you should get the 'you must be dididi' message.
//$stats['level'] = 10; // Now you should be able to buy.

        echo "You must be level 10 to purchase Tire Shops";
        }
 if($stats['level'] >=10) {
        $tire = protect($_POST['tire']);
        $gold_needed = (($business['tire'] * 2) * $tire);
        if($tire < 0){
            output("You must purchase a positive number of Tire Shops!");
        }elseif($stats['gold'] < $gold_needed){
            output("You do not have enough gold!");
        }else{     
            $business['tire'] += $tire;
            $update_business = mysql_query("UPDATE `business` SET `tire`='".$business['tire']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            $stats['gold'] -= $gold_needed;
            $update_gold = mysql_query("UPDATE `stats` SET `gold`='".$stats['gold']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            include("update_businesses.php");
            output("You have purchased businesses!"); 
I have to be 10 or above to purchase, but it doesn't echo anything except I bought a business.

Re: Level Restrictions Help Please

Posted: Tue Sep 03, 2013 7:08 pm
by MikuzA

Code: Select all

     elseif(isset($_POST['business'])){

$stats['level'] = 10; // Change this to 9, in order to test not high enough level. Change it to 10 or higher to test working access level. Comment out this row to test it with database entry

 if($stats['level'] < 10) {
        echo "You must be level 10 to purchase Tire Shops";
        }
 elseif($stats['level'] >=10) {
        $tire = protect($_POST['tire']);
        $gold_needed = (($business['tire'] * 2) * $tire);
        if($tire < 0){
            output("You must purchase a positive number of Tire Shops!");
        }elseif($stats['gold'] < $gold_needed){
            output("You do not have enough gold!");
        }else{     
            $business['tire'] += $tire;
            $update_business = mysql_query("UPDATE `business` SET `tire`='".$business['tire']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            $stats['gold'] -= $gold_needed;
            $update_gold = mysql_query("UPDATE `stats` SET `gold`='".$stats['gold']."' WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());
            include("update_businesses.php");
            output("You have purchased businesses!"); 
         }
else {
echo ("Your level is undefined - Bug detected!");
}
 
Try this

Re: Level Restrictions Help Please

Posted: Tue Sep 03, 2013 7:12 pm
by Epiales
Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\flash\income.php on line 149

This line:

Code: Select all

else {
echo ("Your level is undefined - Bug detected!");
Not sure why I have so much problem with this, but I do lol Thanks

Re: Level Restrictions Help Please

Posted: Tue Sep 03, 2013 7:30 pm
by MikuzA
last else, add one bracket

} else {
like this.