Jump to content

Recommended Posts

Hello

 

  I've got code below that I'm needing help with.  The SQL database is as follows :

Food: 100

Labor: 50

Peasants: 50

 

The object of this set of code is to tell the player how many labors they may train

based on the amount of food and peasants they player has available at the time.

The "cost" of this exchange is 10 food and 1 peasants per labor that needs training...

 

Code below:

print "Labors<br>";
print "Basic working persons for the empire<br>";
print "-------";
print "<br>";
print "Base Output:<br>";
print "+2 Manpower<br>";
print "+2 Research<br><br>";

print "Cost Each:<br>";
print "10 food<br>";
print "1 peasant<br>";
print "<br>";
print "MAX: $max_train<br>";



      print "<br>Train:";
      print "<form action='labor2.php' method='post'>";
      print "<input type='text' name='train' size='16'><br>";
      print "<input type='submit' name='submit' value='Train'></form>";

 

labor2.php code takes the input of the player, checks to see if the player hasn't gone over the

amount of max_train and changes the labor, peasant and food..

 

labor2.php code below:

 

<?php
include('lib.php');
include('g1.php');

session_start();
$link = opendb();
?>

<link rel="stylesheet" href="style.css" type="text/css">
<?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']))
    {
       
     
       $train=$_POST['train'];
       $train=strip_tags($train);

// Loads stats below

$myfood=$userstats3[food];
$mypeasant=$userstats3[peasant];
$mylabor=$userstats3[labor];


// End stat loading


       if($train<0)
       {
          print "You cannot train negative workers.<br> <A href='trainworkers.php'>Back</a>.";
       }
       else if($train>$max_train)
       {
          die("You cannot train that many workers!<br> <A href='trainworkers.php'>Back</a>");
       }
      
       else if($train<=$max_train)
       {
           
$newfood=$train*10;


          $new1="update km_users set labor=labor+'$train', food=food-'$newfood', peasant=peasant-'$train' where ID='$userstats3[iD]'";
          mysql_query($new1) or die("Try Again");

// Announce new stats
$myfood=$userstats3[food];
$mypeasant=$userstats3[peasant];
$mylabor=$userstats3[labor];

print "TRAINING COMPLETE!<br>";
print "------<br>";
print "Labor: $mylabor (-$train)<br>";
print "Peasants: $mypeasant (-$train)<br>";
print "Food: $myfood (-$newfood)<br>";
print "------<br>";
print "<a href='trainworkers.php'>Back</a><br>";
print "<a href='index.php'>Main</a><br>";

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

?>

Link to comment
https://forums.phpfreaks.com/topic/238392-simple-code-help-please/
Share on other sites

 

The $max_train coes from labor.php it is the maxinum number of labors that the player may train.

 

The $max_train also is in the labor2.php file, I just recopied the same lines from labor.php into the

other file.  But what I cannot figure out is getting the right numbers for the $max_train such as:

 

mypeasants: 50

myfood: 50

 

$chk1=round($myfood/10,0); // Check food / 10

if(chk1>$mypeasant)
{
$max_train=$mypeasant;
}

if($chk1<$mypeasant)
{
$max_train=$chk1;
}

 

 

I was changing the numbers to the snipplet you gave:

 

$food=$userstats3[food];
$peasants=$userstats3[peasants];

$food_per_peasant = 10;
$max_train = (($total = ($food / $food_per_peasant)) > $peasants) ? $peasants : $total;
echo $max_train;

 

By doing so, max_train is blank .. what went wrong?

 

if the code is done like your example the $max_train echoes however if the code is fed using the

example above the max_train does not echo ..

I have no way of knowing how $userstats3['food'] or $userstats['peasants'] is defined in this script. Did you add that snippet to your code?  If I was to run this snippet as/is, I would get some undefined errors, and a 'cannot divide by 0' error.

 

Cast your strings to integers when you pull the data back from the database.

 

$food = intval($userstats3[food]);
$peasants = intval($userstats3[peasants]);

 

Post your full script.

The reason for the blank page was my dumba*s didn't add the necessary fields to the

SQL database .. thank you much for your help .. just one or two more questions about

this if you don't mind?

 

1. does the snipplet take into account the amount of food and the number of say labors

  needed to train a farmer ..

print "Farmers<br>";
print "Provides food for your growing empire<br>";
print "-------";
print "<br>";
print "Base Output:<br>";
print "+20 Food <br><br>";

print "Cost Each:<br>";
print "10 food<br>";
print "1 labor<br>";
print "<br>";
print "MAX: $max_train<br>";

 

2. Can and how could this script be reworked to check for three different variables as in the following for

manpower, wood and stone:

 

print "School<br>";
print "Provides more research for your empire<br>";
print "Max peasants: 10<br>";
print "-------<br>";
print "Cost Each:<br>";
print "10 wood<br>";
print "10 stone<br>";
print "Manpower: $manpower<br>";
print "MAX: $max_train<br>";

 

Thanks again

What you need to do, is create a function:

 

<?php
$food = 500;
$peasants = 50;
$food_per_peasant = 10;

$laborers = 10;
$food_per_laborer = 10;


function availableTraining($food, $required_persons, $food_per_persons) {
return  (($total = ($food / $food_per_persons)) > $required_persons) ? $required_persons : intval($total);
}

echo availableTraining($food, $peasants, $food_per_peasant) . ' laborers can be trained!<br />
         OR, <br />' .
         availableTraining($food, $laborers, $food_per_laborer) . ' farmers can be trained!';
?>

 

Here you can see, that you can plug in the necessary variables to make the function work with different values.  You could change food_per_laborer to 15, and it will still calculate it out correctly.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.