Jump to content

Use an array or...?


twilitegxa

Recommended Posts

I would like to create a set of values that would represent levels such as to get to:

 

Level 2 - You must have 500 Experience Points

Level 3 - You must have 1500 Experience Points

 

etc...

 

Can someone help me get started on how I could accomplish this? Would I use an array? I want to use this to figure out how much experience the character needs to get to the next level and then to update the level when they reach enough experience. How would I get started? Here is what I have so far:

 

<?php

session_start();

include("connect_db.php");

if ((isset($_GET['train'])) && ($_GET['train'] == 'test1')) {

$display_block = "<h3>Train Your Character To Level Up</h3>
<h4>Choose a different <a href=choose_train.php>character</a> to train?</h4>
<table cellpadding=3 cellspacing=3>
<tr>";

$get_player_info = "select * from training WHERE identity = '$_GET[identity]'";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];
    $update_energy = ($energy - 2);
    $update_experience = ($experience + 50);
    
    if ($energy < 2) {
    $display_block .= "You do not have enough energy. Rest or train in something else.";
    }
    
    if ($energy > 0) {

$lose_energy = mysql_query("UPDATE training SET energy ='$update_energy' WHERE identity = '$identity'");
$gain_experience = mysql_query("UPDATE training SET experience ='$update_experience' WHERE identity = '$identity'");

}

$display_block .= "
<td valign=top>
<form action=train.php>
<select name=train>
<option>test1</option>
<option>test2</option>
</select>
<input type=submit name=submit value=Train>
<input type='hidden' name='identity' value='$identity' />
</form>
</td>
<td valign=top>Player: $identity<br />
Level: $level<br />
Energy: $energy<br>
Current Experience: $experience<br />
Experience To Next Level:<br />
</td>
</tr>";

}//end while

}//end if

elseif ((isset($_GET['train'])) && ($_GET['train'] == 'test2')) {

$display_block = "<h3>Train Your Character To Level Up</h3>
<h4>Choose a different <a href=choose_train.php>character</a> to train?</h4>
<table cellpadding=3 cellspacing=3>
<tr>";

$get_player_info = "select * from training WHERE identity = '$_GET[identity]'";
$get_player_info_res = mysql_query($get_player_info, $conn) or die(mysql_error());

while ($player_info = mysql_fetch_array($get_player_info_res)) {

    $id = $player_info['id'];
    $identity = $player_info['identity'];
    $level = $player_info['level'];
    $energy = $player_info['energy'];
    $experience = $player_info['experience'];
    $update_energy = ($energy - 10);
    $update_experience = ($experience + 100);
    
    if ($energy > 0) {

$lose_energy = mysql_query("UPDATE training SET energy ='$update_energy' WHERE identity = '$identity'");
$gain_experience = mysql_query("UPDATE training SET experience ='$update_experience' WHERE identity = '$identity'");

}

$display_block .= "
<td valign=top>
<form action=train.php>
<select name=train>
<option>test1</option>
<option>test2</option>
</select>
<input type=submit name=submit value=Train>
<input type='hidden' name='identity' value='$identity' />
</form>
</td>
<td valign=top>Player: $identity<br />
Level: $level<br />
Energy: $energy<br>
Current Experience: $experience<br />
Experience To Next Level:<br />
</td>
</tr>";

}

if ($energy <= 0) {
$display_block .= "You cannot train once your energy is depleted. Please rest.";
}



} else {

$display_block .= "Your character did not train.";

}
$display_block .= "</table>";

?>

<html>
<head>
<title>Sailor Moon RPG - Training Board</title>
<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
</head>
<body>
<!-- HEADER -->
<h1 class="logo">Sailor Moon RPG</h1>
<!-- /HEADER -->
<?php include("topnav.php"); ?>
<div id="main">
<?php include("includes/log.php"); ?>
<?php include("mainnav.php"); ?>
<h1>Sailor Moon RPG - Training Board</h1>
<?php print $display_block; ?>
</div>
<?php include("bottomnav.php"); ?><!-- FOOTER -->
<!-- FOOTER -->
<div id="footer_wrapper">
<div id="footer">
<p>Sailor Moon and all characters
are<br /> 
trademarks of Naoko Takeuchi.</p>
<p>Copyright © 2009 Liz Kula. All rights reserved.<br />
A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p>
<div id="foot-nav">
<ul>
<li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li>
<li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li>
</ul>
</div>
</div>
</div>
<!-- /FOOTER -->
</body>
</html>

Link to comment
Share on other sites

I would use an array. Based upon your code I suspect there may be multiple types of levels (experience, energy, etc.). So this function can be used for multiple levels. Just create an array for each level type, as in the example. Then pass the array and the user points for that level to the function. You will have an object returned with two value the current level name and the points needed to achieve the next level.

 

<?php

$exp_levels = array(
    'Level 1' => 0,
    'Level 2' => 100,
    'Level 3' => 300,
    'Level 4' => 1000,
    'Level 5' => 5000
    );

function currentLevel($levelAry, $userPoints)
{
    $level->name = '';
    $level->pointsToNext = 0;

    foreach($levelAry as $levelName => $levelPoints)
    {
        if ($userPoints<$levelPoints)
        {
            $level->pointsToNext = ($levelPoints-$userPoints);
            return $level;
        }
        $level->name = $levelName;
    }
    //User is over maximum
    return $level;
}

$experience = 525;

$userLevel = currentLevel($exp_levels, $experience);

echo "User is at level " . $userLevel->name . "<br>";
echo "Points to next level " . $userLevel->pointsToNext;

//Output:
//User is at level Level 3
//Points to next level 475

?>

Link to comment
Share on other sites

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.