Level Restrictions Help Please [Resolved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Level Restrictions Help Please [Resolved]

Post 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////////////////////////////////////////////////
        
?>
Last edited by Epiales on Tue Sep 03, 2013 8:41 pm, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Level Restrictions Help Please

Post 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.
The indelible lord of tl;dr
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Level Restrictions Help Please

Post 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.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Level Restrictions Help Please

Post 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 :)
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Level Restrictions Help Please

Post 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!");
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Level Restrictions Help Please

Post 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.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Level Restrictions Help Please

Post 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.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Level Restrictions Help Please

Post 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
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Level Restrictions Help Please

Post 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
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Level Restrictions Help Please

Post by MikuzA »

last else, add one bracket

} else {
like this.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
Post Reply

Return to “Beginner Help and Support”