Jump to content

[SOLVED] Need Help With Math Calculations


twilitegxa

Recommended Posts

I have the following form:

 

<?php

session_start();

//register our session variables
session_register('id');
session_register('create_time');
//session_register('username');
session_register('identity');
session_register('name');
session_register('element_of_influence');
session_register('age');
session_register('birth_month');
session_register('birth_date');
//session_register ('birth_year');
session_register('blood_type');
session_register('hobbies');
session_register('favorite_color');
session_register('favorite_gemstone');
session_register('favorite_food');
session_register('least_favorite_food');
session_register('favorite_school_subject');
session_register('least_favorite_school_subject');
session_register('strengths');
session_register('weaknesses');
session_register('goal');
session_register('mission');
session_register('biography');
session_register('height_feet');
session_register('height_inches');

//store our posted values in the session variables
$_SESSION['id'] = '';
$_SESSION['create_time'] = 'now()';
//$_SESSION['username'] = $_POST['username'];
$_SESSION['identity'] = $_POST['identity'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['element_of_influence'] = $_POST['element_of_influence'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['birth_month'] = $_POST['birth_month'];
$_SESSION['birth_date'] = $_POST['birth_date'];
//$_SESSION['birth_year'] = $_POST['birth_year'];
$_SESSION['blood_type'] = $_POST['blood_type'];
$_SESSION['hobbies'] = $_POST['hobbies'];
$_SESSION['favorite_color'] = $_POST['favorite_color'];
$_SESSION['favorite_gemstone'] = $_POST['favorite_gemstone'];
$_SESSION['favorite_food'] = $_POST['favorite_food'];
$_SESSION['least_favorite_food'] = $_POST['least_favorite_food'];
$_SESSION['favorite_school_subject'] = $_POST['favorite_school_subject'];
$_SESSION['least_favorite_school_subject'] = $_POST['least_favorite_school_subject'];
$_SESSION['strengths'] = $_POST['strengths'];
$_SESSION['weaknesses'] = $_POST['weaknesses'];
$_SESSION['goal'] = $_POST['goal'];
$_SESSION['mission'] = $_POST['mission'];
$_SESSION['biography'] = $_POST['biography'];
$_SESSION['height_feet'] = $_POST['height_feet'];
$_SESSION['height_inches'] = $_POST['height_inches'];

//Access Tracking Snippet

//set up static variables
$page_title = "stats.php";
$user_agent = getenv("HTTP_USER_AGENT");
$date_accessed = date("Y-m-d");

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
$db = mysql_select_db("smrpg", $conn) or die(mysql_error());

//create and issue query
$sql = "insert into access_tracker values
('', '$page_title', '$user_agent', '$date_accessed')";
mysql_query($sql,$conn);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sailor Moon RPG - Character Creation Form | Scout</title>
<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
<!-- to get derived values -->
<script type="text/javascript">
function handle(a,b,c) {
			var e = document.getElementsByTagName('input');
			for (i in e) {
				if (e[i].name == a) a = e[i];
				if (e[i].name == b) b = e[i];
				if (e[i].name == c) c = e[i];
			}
			c.value = (Number(a.value) + Number(b.value)) * 5;
		}

</script>

</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>Step 2: Character Outline - Creation</h1>
<h2>Stats And Derived Values</h2>
<form action="insert.php" method="post">
<table border="0">
  <tr>
    <td>Body:</td>
    <td><input type="text" onKeyUp="handle('body','soul','health')" name="body" size="10" maxlength="2"></td>
  </tr>

  <tr>
    <td>Mind:</td>
    <td><input type="text" onKeyUp="handle('mind','soul','energy')" name="mind" size="10" maxlength="2"></td>
  </tr>
  <tr>
    <td>Soul:</td>
    <td><input type="text" onKeyUp="handle('body','soul','health')" name="soul" size="10" maxlength="2"></td>
  </tr>

<tr>
    <td></td>
    <td> </td>
  </tr>
  <tr>
    <td>Health Points:</td>
    <td><input type="text" onKeyUp="handle('body','soul')"
name="health" size="10" maxlength="3" /></td>
  </tr>
  <tr>
    <td>Energy Points:</td>
    <td><input type="text" onKeyUp="handle('mind','soul')" name="energy" size="10" maxlength="3"></td>
  </tr>
  <tr>
    <td>Attack Combat Value:</td>

    <td><input type="text" name="acv1" size="10" maxlength="3">    </td>
  </tr>
  <tr>
    <td>(Special Attack)</td>
    <td><input type="text" name="acv2" size="10" maxlength="3">    </td>
  </tr>

  <tr>
    <td>Defense Combat Value:</td>
    <td><input type="text" name="dcv1" size="10" maxlength="30">    </td>
  </tr>
  <tr>
    <td>(Special Defense)</td>
    <td><input type="text" name="dcv2" size="10" maxlength="3">    </td>
  </tr>
  <tr>
    <td>Total Character Points:</td>
    <td><input type="text" name="total_cp" size="10" maxlength="3">    </td>
  </tr>
</table>
<p style="text-align: center" class="submit"><input type="submit" value="Create Character" />
<input type="reset" value="Reset" /></p>

</form>
</div>
<?php include("bottomnav.php"); ?>
<!-- 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>

 

And the JavaScript part so far is calculating the health and energy from the three fields body, mind, and soul.

 

the function:

<script type="text/javascript">
function handle(a,b,c) {
			var e = document.getElementsByTagName('input');
			for (i in e) {
				if (e[i].name == a) a = e[i];
				if (e[i].name == b) b = e[i];
				if (e[i].name == c) c = e[i];
			}
			c.value = (Number(a.value) + Number(b.value)) * 5;
		}

</script>

 

the form:

<form action="insert.php" method="post">
<table border="0">
  <tr>
    <td>Body:</td>
    <td><input type="text" onKeyUp="handle('body','soul','health')" name="body" size="10" maxlength="2"></td>
  </tr>

  <tr>
    <td>Mind:</td>
    <td><input type="text" onKeyUp="handle('mind','soul','energy')" name="mind" size="10" maxlength="2"></td>
  </tr>
  <tr>
    <td>Soul:</td>
    <td><input type="text" onKeyUp="handle('body','soul','health')" name="soul" size="10" maxlength="2"></td>
  </tr>

<tr>
    <td></td>
    <td> </td>
  </tr>
  <tr>
    <td>Health Points:</td>
    <td><input type="text" onKeyUp="handle('body','soul')"
name="health" size="10" maxlength="3" /></td>
  </tr>
  <tr>
    <td>Energy Points:</td>
    <td><input type="text" onKeyUp="handle('mind','soul')" name="energy" size="10" maxlength="3"></td>
  </tr>
  <tr>
    <td>Attack Combat Value:</td>

    <td><input type="text" name="acv1" size="10" maxlength="3">    </td>
  </tr>
  <tr>
    <td>(Special Attack)</td>
    <td><input type="text" name="acv2" size="10" maxlength="3">    </td>
  </tr>

  <tr>
    <td>Defense Combat Value:</td>
    <td><input type="text" name="dcv1" size="10" maxlength="30">    </td>
  </tr>
  <tr>
    <td>(Special Defense)</td>
    <td><input type="text" name="dcv2" size="10" maxlength="3">    </td>
  </tr>
  <tr>
    <td>Total Character Points:</td>
    <td><input type="text" name="total_cp" size="10" maxlength="3">    </td>
  </tr>
</table>
<p style="text-align: center" class="submit"><input type="submit" value="Create Character" />
<input type="reset" value="Reset" /></p>

</form>

 

I need some help with a way to alter the current function or writing a new function if necessary, to calculate the acv1 and dcv1. The formula for acv1 is [(Body+Mind+Soul)/3] (rounded down or floored I think it is called), and the dcv1 is [(Body+Mind+Soul)/3] (rounded down) - 2.

 

Can anyone help me out here?

Link to comment
Share on other sites

  • 1 month later...

I found a way to do it:

 

<?php

session_start();
if(!isset($_SESSION['loggedIn'])) {
header("Location: login.php");
}

function yearOfBirth ($day, $month, $age)
{
    $now = strtotime('1992-03-23');

    $yob = date('Y', $now) - $age;
    
    if (date('md', $now) < sprintf('%02d%02d', $month, $day)) $yob--;
    
    return $yob;
}

$year =  yearOfBirth($_POST['birth_date'], $_POST['birth_month'], $_POST['age']);

//store our posted values in the session variables
$_SESSION['identity'] = $_POST['identity'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['element_of_influence'] = $_POST['element_of_influence'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['birth_month'] = $_POST['birth_month'];
$_SESSION['birth_date'] = $_POST['birth_date'];
$_SESSION['birth_year'] = $year;
$_SESSION['blood_type'] = $_POST['blood_type'];
$_SESSION['hobbies'] = $_POST['hobbies'];
$_SESSION['favorite_color'] = $_POST['favorite_color'];
$_SESSION['favorite_gemstone'] = $_POST['favorite_gemstone'];
$_SESSION['favorite_food'] = $_POST['favorite_food'];
$_SESSION['least_favorite_food'] = $_POST['least_favorite_food'];
$_SESSION['favorite_school_subject'] = $_POST['favorite_school_subject'];
$_SESSION['least_favorite_school_subject'] = $_POST['least_favorite_school_subject'];
$_SESSION['strengths'] = $_POST['strengths'];
$_SESSION['weaknesses'] = $_POST['weaknesses'];
$_SESSION['goal'] = $_POST['goal'];
$_SESSION['mission'] = $_POST['mission'];
$_SESSION['biography'] = $_POST['biography'];
$_SESSION['height_feet'] = $_POST['height_feet'];
$_SESSION['height_inches'] = $_POST['height_inches'];

//Access Tracking Snippet

//set up static variables
$page_title = "stats.php";
$user_agent = getenv("HTTP_USER_AGENT");
$date_accessed = date("Y-m-d");

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
$db = mysql_select_db("smrpg", $conn) or die(mysql_error());

//create and issue query
$sql = "insert into access_tracker values
('', '$page_title', '$user_agent', '$date_accessed')";
mysql_query($sql,$conn);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sailor Moon RPG - Character Creation Form | Stats</title>
<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
<!-- Script for calculating Derived Values -->
<script language="javascript">

function filledOut(){
if (document.forms[0].bodyv.value != "" && document.forms[0].mind.value != "" && document.forms[0].soul.value != ""){
	acv();
	dcv();
	healthv();
	energyv();
}
}

function healthv()
{
var first,sec,third,res1;
//Take the value of first textbox and convert it to float
first=parseFloat(document.forms[0].bodyv.value);
//Take the value of second textbox and convert it to float
sec=parseFloat(document.forms[0].mind.value);
//third
third=parseFloat(document.forms[0].soul.value);
res1=(first+third)*5;
//show the result in the result textbox
document.forms[0].health.value=res1;
}

function energyv()
{
var sec,third,res2;
//Take the value of first textbox and convert it to float
first=parseFloat(document.forms[0].bodyv.value);
//Take the value of second textbox and convert it to float
sec=parseFloat(document.forms[0].mind.value);
//third
third=parseFloat(document.forms[0].soul.value);
res2=(sec+third)*5;
//show the result in the result textbox
document.forms[0].energy.value=res2;
}

function acv()
{
var first,sec,third,res3;
//Take the value of first textbox and convert it to float
first=parseFloat(document.forms[0].bodyv.value);
//Take the value of second textbox and convert it to float
sec=parseFloat(document.forms[0].mind.value);
//third
third=parseFloat(document.forms[0].soul.value);
res3=Math.floor((first+sec+third)/3);
//show the result in the result textbox
document.forms[0].acv1.value=res3;
}

function dcv()
{
var first,sec,third,res4;
//Take the value of first textbox and convert it to float
first=parseFloat(document.forms[0].bodyv.value);
//Take the value of second textbox and convert it to float
sec=parseFloat(document.forms[0].mind.value);
//third
third=parseFloat(document.forms[0].soul.value);
res4=Math.floor((first+sec+third)/3 -2);
//show the result in the result textbox
document.forms[0].dcv1.value=res4;
}
</script>
<!-- End Derived Values Script -->
</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>Step 2: Character Outline - Creation</h1>
<h2>Stats And Derived Values</h2>
<form name="stats" method="post" action="insert_scout.php">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="left"><strong>Body:</strong></div></td>
<td><input name="bodyv" type="text" id="bodyv" size="5" maxlength="2" onBlur="filledOut()" /></td>
</tr>
<tr>
<td><div align="left"><strong>Mind:</strong></div></td>
<td><input name="mind" type="text" id="mind" size="5" maxlength="2" onBlur="filledOut()" /></td>
</tr>
<tr>
<td><div align="left"><strong>Soul:</strong></div></td>
<td><input name="soul" type="text" id="soul" size="5" maxlength="2" onBlur="filledOut()" /></td>
</tr>
<tr>
<td></td>
<td> </td>
</tr>
<tr>
<td><div align="left"><strong>Health Points:</strong></div></td>
<td><input name="health" type="text" id="health"readonly="readonly" size="5" maxlength="2" /></td>
</tr>
<tr>
<td><div align="left"><strong>Energy Points:</strong></div></td>
<td><input name="energy" type="text" id="energy" readonly="readonly" size="5" maxlength="2" /></td>
</tr>
<tr>
<td><div align="left"><strong>Attack Combat Value:</strong></div></td>
<td><input name="acv1" type="text" id="acv1" readonly="readonly" size="5" maxlength="2" /></td>
</tr>
<tr>
<td><div align="left"><strong>Defense Combat Value:</strong></div></td>
<td><input name="dcv1" type="text" id="dcv1" readonly="readonly" size="5" maxlength="2" /></td>
</tr>
<tr>
<td> </td>
<td>
<input name="total_cp" type="hidden" id="total_cp" value="10" /></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input name="calcstats" type="submit" id="submit" value="Next" >
<input type="reset" value="Reset" /></div></td>
</tr>
</table>
</form>
</div>
<?php include("bottomnav.php"); ?>
<!-- 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

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.