Php Session Help

Trouble with calculations

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1869 Views - Last Post: 09 June 2010 - 08:46 AM Rate Topic: -----

#16 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Php Session Help

Posted 08 June 2010 - 06:10 PM

Please, :code:

Since case two (below) onwards all follow the same pattern, you could use a loop to automate this. Just define a standard mathematical formula, then you can plug and play.
If ($_SESSION['Land'] >= 101 && $_SESSION['Land'] <= 150) 
$_SESSION['HLand'] = ($_SESSION['Land'] - 100) * 2 + 100;


Was This Post Helpful? 0
  • +
  • -

#17 Stoutn   User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 18
  • Joined: 07-June 10

Re: Php Session Help

Posted 08 June 2010 - 07:12 PM

View Postmacosxnerd101, on 08 June 2010 - 05:10 PM, said:

Please, :code:

Since case two (below) onwards all follow the same pattern, you could use a loop to automate this. Just define a standard mathematical formula, then you can plug and play.
If ($_SESSION['Land'] >= 101 && $_SESSION['Land'] <= 150) 
$_SESSION['HLand'] = ($_SESSION['Land'] - 100) * 2 + 100;




Thanks for the suggestion, I should of thought of the code tags sooner lol


As for the code itself, I am not really sure of the pattern, hence my problem lol so for now the single if's seems to work well enough, until I see the pattern ;)


new problem is:
Fatal error: Call to undefined function filter_input() in *site* on line 2

<?php
$RMili = filter_input(INPUT_GET, "inputMilitary");
$RWeap = filter_input(INPUT_GET, "inputWeapons");
$ROff = filter_input(INPUT_GET, "inputOffence");
$RDef = filter_input(INPUT_GET, "inputDefence");
$RRes = filter_input(INPUT_GET, "inputResidential");
$RAg = filter_input(INPUT_GET, "inputAgriculture");
$RInd = filter_input(INPUT_GET, "inputIndustrial");
$RMed = filter_input(INPUT_GET, "inputMedical");
$REsp = filter_input(INPUT_GET, "inputEspionage");
$RRef = filter_input(INPUT_GET, "inputRefinery");
$RLand = filter_input(INPUT_GET, "inputLand");
?>

This post has been edited by Stoutn: 08 June 2010 - 07:18 PM

Was This Post Helpful? 0
  • +
  • -

#18 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Php Session Help

Posted 08 June 2010 - 11:58 PM

View PostStoutn, on 08 June 2010 - 06:12 PM, said:

As for the code itself, I am not really sure of the pattern, hence my problem lol so for now the single if's seems to work well enough, until I see the pattern ;)

Consider, if I were to put the parameters for your IF clauses into an array, like so:
$params = array(
    array("ceil" => 100, "multi" => 1, "add" => 0),
    array("ceil" => 150, "multi" => 2, "add" => 100),
    array("ceil" => 200, "multi" => 3, "add" => 200),
    // etc...
);

The "ceil" would be the ceiling, the higher end of the value of "land" allowed by your IF statements.
The "multi" and "add" would be the values used in your formula.

With that, you can use a simple loop to do the calculations, which would remain the same even if you need to change/add to the parameters..
Can't seem to explain that in words at the moment, so I'll just show you what I mean xD
$land = $_SESSION['Land'];
$hland = null;

// Search for the first ceiling that is higher than the
// "land" value.
foreach($params as $_i => $_values)
{
    if($land <= $_values['ceil'])
    {
        // No reduction for the first element, so use 0
        // if this is the first element. Othervise get 
        // the ceiling for the previous param.
        $reduction = $_i == 0 ? 0 : $params[$_i-1]['ceil'];

        // The standard formula
        $hland = ($land - $reduction) * $_values['multi'] + $_values['add'];

        // Break out of the loop, or you will always 
        // end up with the highest posibility.
        break;
    }
}
$_SESSION['HLand'] = $hland;


This way you could alter the parameters for the calculations without actually having to modify the code itself (only the array). You could even pull it from a database, or something along those lines.
Was This Post Helpful? 3
  • +
  • -

#19 Stoutn   User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 18
  • Joined: 07-June 10

Re: Php Session Help

Posted 09 June 2010 - 08:46 AM

That is quite interesting, I will have to read up on that :) Thank you very much for pointing it out to me :P












View PostAtli, on 08 June 2010 - 10:58 PM, said:

View PostStoutn, on 08 June 2010 - 06:12 PM, said:

As for the code itself, I am not really sure of the pattern, hence my problem lol so for now the single if's seems to work well enough, until I see the pattern ;)

Consider, if I were to put the parameters for your IF clauses into an array, like so:
$params = array(
    array("ceil" => 100, "multi" => 1, "add" => 0),
    array("ceil" => 150, "multi" => 2, "add" => 100),
    array("ceil" => 200, "multi" => 3, "add" => 200),
    // etc...
);

The "ceil" would be the ceiling, the higher end of the value of "land" allowed by your IF statements.
The "multi" and "add" would be the values used in your formula.

With that, you can use a simple loop to do the calculations, which would remain the same even if you need to change/add to the parameters..
Can't seem to explain that in words at the moment, so I'll just show you what I mean xD
$land = $_SESSION['Land'];
$hland = null;

// Search for the first ceiling that is higher than the
// "land" value.
foreach($params as $_i => $_values)
{
    if($land <= $_values['ceil'])
    {
        // No reduction for the first element, so use 0
        // if this is the first element. Othervise get 
        // the ceiling for the previous param.
        $reduction = $_i == 0 ? 0 : $params[$_i-1]['ceil'];

        // The standard formula
        $hland = ($land - $reduction) * $_values['multi'] + $_values['add'];

        // Break out of the loop, or you will always 
        // end up with the highest posibility.
        break;
    }
}
$_SESSION['HLand'] = $hland;


This way you could alter the parameters for the calculations without actually having to modify the code itself (only the array). You could even pull it from a database, or something along those lines.

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2