Jump to content

IF/Then/Else


lmninfo

Recommended Posts

 

Hello

 

  The following code handles the players transfer of their peasants

to labor.  When the player has less than (5) peasants it should

tell the player "You cannot train labor at this time", which it does

however, the code continues with following lines:

 

print "<br>";

print "Manpower: +1<br>";

print "Research: +1<br>";

print "<br>";

print "Cost:<br>";

print "Food: -5<br>";

print "Peasants: -1<br>";

 

then allows the player to train their labor anyway even though the player

has less than 1 peasant or 5 units of food.

 

Could someone take a look and see why this is happening?

 

labor.php

<?php
include 'connect.php';
session_start();
?>


<?php
if (isset($_SESSION['player'])) 
  {
    $player=$_SESSION['player'];
    $userstats="SELECT * from km_users where playername='$player'";
    $userstats2=mysql_query($userstats) or die("Could not get user stats");
    $userstats3=mysql_fetch_array($userstats2);

print "<table class='maintable'><tr class='headline'><td><center>Train Labor</center></td></tr>";
      print "<tr class='mainrow'><td>";
           
if($userstats3[food]<5)
       {
          print "You cannot train labor at this time. <A href='index.php'>Go back to main</a>.<br>";
       }
       else if($userstats3[peasants]<1)
       {
          print "You cannot train labor at this time. <A href='index.php'>Go back to main</a>.<br>";
       }
       else if($userstats3[food]>=5)
       
print "<br>";
print "Manpower: +1<br>";
print "Research: +1<br>";
print "<br>";
print "Cost:<br>";
print "Food: -5<br>";
print "Peasants: -1<br>";
        
      print "<form action='peasant2labor.php?ID=$user3stats[iD]' method='post'>";
      print "<input type='text' name='scipts' size='6'><br>";
      print "<input type='submit' name='submit' value='submit'></form>";
      print "</td></tr></table>";
      print "</td>";
}

 

peasant2labor.php

<?php
include 'connect.php';
session_start();
?>

<?php
if (isset($_SESSION['player'])) 
  {
    $player=$_SESSION['player'];
    $userstats="SELECT * from km_users where playername='$player'";
    $userstats2=mysql_query($userstats) or die("Could not get user stats");
    $userstats3=mysql_fetch_array($userstats2);
    if(isset($_POST['submit']))
    {
         
        $scipts=$_POST['scipts'];
        $scipts=strip_tags($scipts);
        $maxfood=$scipts*5;
        
        print "<table class='maintable'>";
        print "<tr class='headline'><td><center>Train Labor</center></td></tr>";
        print "<tr class='mainrow'><td>";
        if($userstats3[food]<5)
       {
          print "You cannot train labor at this time. <A href='index.php'>Go back to main</a>.";
       }
       else if($userstats3[peasants]<1)
       {
          print "You cannot train labor at this time. <A href='index.php'>Go back to main</a>.";
       }
       else if($userstats3[food]>=5)
       {
            $updatestats="update km_users set labor=labor+'$scipts', peasants=peasants-'$scipts', food=food-$maxfood where ID='$userstats3[iD]'";
            mysql_query($updatestats) or die("Could not update stats");
            print "Trained Labor";
print "<br>";
print "Labor: $userstats3[labor] ( + $scipts )";
print "<br>";
print "Food: $userstats3[food]";
print "<br>";
print "Peasants: $userstats3[peasants] ( - $scipts ) ";
print "<br>";
print "<tr class='mainrow'><td><A href='index.php'>Back</a></td></tr>";  


        }
        print "</td></tr></table>";



    }
  }
else
  {
    print "Sorry, not logged in  please <A href='login.php'>Login</a><br>";
  
  }

?>

Link to comment
https://forums.phpfreaks.com/topic/151738-ifthenelse/
Share on other sites

      else if($userstats3[food]>=5) { <------ missing

     

print "<br>";

print "Manpower: +1<br>";

print "Research: +1<br>";

print "<br>";

print "Cost:<br>";

print "Food: -5<br>";

print "Peasants: -1<br>";

       

      print "<form action='peasant2labor.php?ID=$user3stats[iD]' method='post'>";

      print "<input type='text' name='scipts' size='6'><br>";

      print "<input type='submit' name='submit' value='submit'></form>";

      print "</td></tr></table>";

      print "</td>";

}<------ missing

 

}

Link to comment
https://forums.phpfreaks.com/topic/151738-ifthenelse/#findComment-796791
Share on other sites

Your else if($userstats3[food]>=5) statement does not have any {}, so, only the first print "<br>"; statement is part of that condition and the rest of the code is executed unconditionally.

 

Edit: The } at the end of the posted code appears like it is a match for the { that is part of - if (isset($_SESSION['player']))

Link to comment
https://forums.phpfreaks.com/topic/151738-ifthenelse/#findComment-796792
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.