Jump to content

Technique question - creating a calculator


tlavelle

Recommended Posts

I have been struggling with making a calculator and could use a hand.  The concept is a benchmarking calculator where a user:

1.  Selects a comparison group and year

2.  Enters values per metric

3.  Gets a result that shows the benchmarking data vs the suer entered data (basically subtract the best practice cfiled from the user field per record.

 

Here are the pages I have so far

 

page 1 - allows user to select the year and group - gets data from db

<html>
<head>
<basefont face="Arial">
</head>
<body>

<?php

// set server access variables
$host = "localhost";
$user = "root";
$pass = "";
$db = "wercbench";


// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$year_query = "SELECT distinct year FROM userdata";
$industry_query = "SELECT distinct comp_desc FROM comp_desc";

// execute query
$year_result = mysql_query($year_query) or die ("Error in query: $query. ".mysql_error());
$industry_result = mysql_query($industry_query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($year_result) > 0 or mysql_num_rows($industry_result)){
     // yes
     // print them one after another

 echo"<form method=\"POST\" action=\"uservals.php\" name=\"wercbench1\">";
     echo"<table style=\"width: 100%;\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
     echo"<tbody>";
     echo" <tr>";
     echo"   <td>Select the comparison year and group then click next";
     echo"   </td>";
     echo"   <td></td>";
     echo" </tr>";
     echo" <tr>";
     echo"  <td>";
     echo"   <select name=\"year\">";
     while($row = mysql_fetch_row($year_result)) {
     echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>";
 }
     echo"   </select>";
     echo"   </td>";
     echo"   <td></td>";
     echo" </tr>";


     echo" <tr>";
     echo"   <td>";
 echo"   <select name=\"industry\">";
  while($row = mysql_fetch_row($industry_result)) {
     echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>";
}
     echo"   </select>";
 echo"</td>";
     echo"   <td></td>";
     echo" </tr>";
     echo" <tr>";
     echo"   <td><input type=\"Submit\" value=\"Next\" name=\"wercbench1_next\"></button></td>";
     echo"   <td></td>";
     echo" </tr>";

    echo"</tbody>";
  echo"</table>";
echo"</form>";
}
else {
    // no
    // print status message
    echo "No rows found!";
}
// free result set memory
mysql_free_result($year_result);
mysql_free_result($industry_result);

// close connection
mysql_close($connection);

?>

</body>
</html>

page 2 - generates for fro user to enter data and submits to calculation page

<html>
<head>
<basefont face="Arial">
</head>
<body>

<?php
include 'index.php';

$useryear=$_POST['year'];
$userindustry=$_POST['industry'];


// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$metric_query = "SELECT metric_desc.met_desc, userdata.median, userdata.best_pract FROM metric_desc, userdata where metric_desc.met_key=userdata.met_key and $useryear=userdata.year";


// execute query
$metric_result = mysql_query($metric_query) or die ("Error in query: $query. ".mysql_error());



// see if any rows were returned
if (mysql_num_rows($metric_result) > 0) {
    // yes
    // print them one after another
echo "<form action=\"results.php\" method=\"post\">";
    echo "<table cellpadding=10 border=1>";
    echo "<tr>";
        echo "<td>Metric Description</td>";
        echo "<td>Average</td>";
        echo "<td>Best Practice</td>";
	echo "<td>Enter your value here</td>";
        echo "</tr>";
    while($row = mysql_fetch_row($metric_result)) {
        echo "<tr>";
        echo "<td>".$row[0]."</td>";
        echo "<td>" . $row[1]."</td>";
        echo "<td>".$row[2]."</td>";
	echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>";
        echo "</tr>";
    }
    echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Compare\"></td></tr></table>";
}
else {
    // no
    // print status message
    echo "No rows found!";
} 

// free result set memory
mysql_free_result($metric_result);


// close connection
mysql_close($connection);
?>


</body>
</html>

 

page 3 - displays tabel containing all benchmark data, user entered data and difference between the 2  this code is nowhere close

<html>
<head>
<basefont face="Arial">
</head>
<body>

<?php
include 'index.php';

$useryear=$_POST['year'];
$userindustry=$_POST['industry'];
$uservals=$_POST['myval[]'];

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$metric_query = "SELECT metric_desc.met_desc, userdata.median, userdata.best_pract FROM metric_desc, userdata where metric_desc.met_key=userdata.met_key and $useryear=userdata.year";


// execute query
$metric_result = mysql_query($metric_query) or die ("Error in query: $metric_query. ".mysql_error());



// see if any rows were returned
if (mysql_num_rows($metric_result) > 0) {
    // yes
    // print them one after another
echo "<form action=\"results.php\" method=\"post\">";
    echo "<table cellpadding=10 border=1>";
    echo "<tr>";
        echo "<td>Metric Description</td>";
        echo "<td>Average</td>";
        echo "<td>Best Practice</td>";
	echo "<td>Enter your value here</td>";
        echo "</tr>";
    while($row = mysql_fetch_row($metric_result)) {
        echo "<tr>";
        echo "<td>".$uservals[0]."</td>";
        echo "</tr>";
    }
    echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Compare\"></td></tr></table>";
}
else {
    // no
    // print status message
    echo "No rows found!";
} 

// free result set memory
mysql_free_result($metric_result);


// close connection
mysql_close($connection);
?>


</body>
</html>

 

My questions are:

 

Is my general approach correct? 

how do i generate the results for page 3?

how do I resubmit the query to generate the table from page 2 without having the user resubmit that form?

 

I am a total n00bie and would love some metoring.

 

Thanks

 

 

Sorry about the code tag omission...claiming n00bie ignorance

Link to comment
https://forums.phpfreaks.com/topic/64671-technique-question-creating-a-calculator/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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