Jag klistrade in följande kod. Jag upptäckte dock ett mindre fel som dock bara märks om man skickar formuläret utan data. Felmeddelandet vill inte alltid synas. Men det är mitt minsta problem just nu.
Code: Select all
/*
Created: 18 July 2011
*/
// If the form hasn't been submitted the form is displayed.
if ($_SERVER['REQUEST_METHOD'] != 'POST')
{
$notSent = $_SERVER['PHP_SELF'];
include('../database/php_oop/view/jadinko_calcform.htm');
}
else
{
$sent = $_POST;
$object = new Jadinko();
$object->calculation($sent);
}
// Here starts the Jadinko class.
class Jadinko
{
// A multidimensional array storing name of jadinkos, experience and levels.
private $jadinko = array
(
array( Name => "Common", Exp => 350, Lvl => 70),
array( Name => "Shadow", Exp => 475, Lvl => 71),
array( Name => "Igneous", Exp => 465, Lvl => 74),
array( Name => "Cannibal", Exp => 475, Lvl => 75),
array( Name => "Aquatic", Exp => 475, Lvl => 76),
array( Name => "Amphibian", Exp => 485, Lvl => 77),
array( Name => "Carrion", Exp => 505, Lvl => 78),
array( Name => "Diseased", Exp => 580.5, Lvl => 78),
array( Name => "Camoflaged", Exp => 600, Lvl => 79),
array( Name => "Draconic", Exp => 525, Lvl => 80),
array( Name => "Saradomin", Exp => 600, Lvl => 81),
array( Name => "Guthix", Exp => 600, Lvl => 81),
array( Name => "Zamorak", Exp => 600, Lvl => 81)
);
// Level array with experience.
private $exp = array
(
"70"=>737627, "71"=>814445, "72"=>899257, "73"=>992895, "74"=>1096278, "75"=>1210421, "76"=>1336443, "77"=>1475581, "78"=>1629200, "79"=>1798808,
"80"=>1986068, "81"=>2192818, "82"=>2421087, "83"=>2673114, "84"=>2951373, "85"=>3258594, "86"=>3597792, "87"=>3972294, "88"=>4385776, "89"=>4842295,
"90"=>5346332, "91"=>5902831, "92"=>6517253, "93"=>7195629, "94"=>7944614, "95"=>8771558, "96"=>9684577, "97"=>10692629, "98"=>11805606, "99"=>13034431
);
// Lists the array contents in a dropdown menu.
public function listLevels()
{
echo '<option value="">Level</option>';
foreach ($this->exp as $num => $value)
{
echo '<option value="' . $value . '">' . $num . '</option>';
}
}
// The form values are checked for numeric values. If they are not, an error message is printed.
// If the values are numeric, they are checked for
public function calculation($var)
{
$currExp = stripslashes($var['currentxp']);
$currLevel = stripslashes($var['currentlvl']);
$goalExp = stripslashes($var['goalxp']);
$goalLevel = stripslashes($var['goallvl']);
// If nothing has been added to the form, an error message is echoed.
if (!(isset($currExp) && isset($currLevel) && isset($goalExp) && isset($goalLevel)))
{
include('../database/php_oop/view/jadinko_calcform.htm');
echo '<p>You have to make a choice for the calculator to work.</p>';
}
// If current exp and goal exp has been filled in, data is sent to corresponding function.
elseif ($currExp && $goalExp)
{
$this->currExpGoalExp($currExp, $goalExp);
}
// ...else, if current exp and goal level has been filled in, data is sent to corresponding function.
elseif ($currExp && $goalLevel)
{
$this->currExpGoalLvl($currExp, $goalLevel);
}
// ...else, if current level and goal exp has been filled in, data is sent to corresponding function.
elseif ($currLevel && $goalExp)
{
$this->currLvlGoalExp($currLevel, $goalExp);
}
// ...else, if current level and goal level has been filled in, data is sent to corresponding function.
elseif (($currLevel != null) && ($goalLevel != null))
{
$this->currLvlGoalLvl($currLevel, $goalLevel);
}
}
// Loops through the multidim. jadinko array and prints name, level and $difference divided by jadinko xp.
// The result is rounded down to two decimals.
public function forLoopWithTable($leftover)
{
include('../database/php_oop/view/jadinko_calcform.htm');
echo '<p></p><table><tr><td>Type</td><td>Level</td><td>Iterations</td></tr>';
for ($row = 0; $row < count($this->jadinko); $row++)
{
echo '<tr><td>', $this->jadinko[$row]["Name"], '</td><td>', $this->jadinko[$row]["Lvl"], '</td><td>', round($leftover / $this->jadinko[$row]["Exp"], 2), '</td></tr>';
}
echo '</table>';
}
// Current xp and goal xp are collected, the difference is calculated and is sent to forLoopWithTable().
public function currExpGoalExp($varCurrExp, $varGoalExp)
{
$difference = $varGoalExp - $varCurrExp;
$this->forLoopWithTable($difference);
}
// Current xp and goal level are collected, the difference is calculated and is sent to forLoopWithTable().
public function currExpGoalLvl($varCurrExp, $varGoalLvl)
{
$difference = $varGoalLvl - $varCurrExp;
$this->forLoopWithTable($difference);
}
// Current level and goal xp are collected, the difference is calculated and is sent to forLoopWithTable().
public function currLvlGoalExp($varCurrLvl, $varGoalExp)
{
$difference = $varGoalExp - $varCurrLvl;
$this->forLoopWithTable($difference);
}
// Current level and goal level are collected, the difference is calculated and is sent to forLoopWithTable().
public function currLvlGoalLvl($varCurrLvl, $varGoalLvl)
{
$difference = $varGoalLvl - $varCurrLvl;
$this->forLoopWithTable($difference);
}
}