PHP Calculator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
perfect123
Forum Newbie
Posts: 6
Joined: Wed Jul 15, 2009 12:11 pm

PHP Calculator

Post by perfect123 »

Hi

Im trying to create a form that will add two numbers and input the answer automatically into a text box. Please help.

Basically, I have created two text boxes called length and width and another called total. So if I entered 5 in width and 50 in Length, I want the answer displayed automatically in Total. How would I do that.

Appreciate it
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

Re: PHP Calculator

Post by iamngk »

it seems to be client slide script. if it is very simple calculation you can perform through javascript.
text box on change function you can perform calculation and print int by automatic.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP Calculator

Post by Benjamin »

Hi. Can you please post the code you have written so far? Where are you stuck?
perfect123
Forum Newbie
Posts: 6
Joined: Wed Jul 15, 2009 12:11 pm

Re: PHP Calculator

Post by perfect123 »

I got it to work now, but stuck on another problem which I need urgent help with. Please can you help me.

I have created a search box which potentially works. But I wish to insert the results in to the text box rather than having them on the page

I have a text box and a search button next to it. so When I put in a search and click search, the results should appear in the ref text box. I hope you understand what I mean. Please Help. Here is the code:

Code: Select all

 
<form action="test_search.php" method="get">
<input type="text" name="query" id="query" size="40" value="" action="Search.php" columns="2" autocomplete="off" delay="1500"> 
<input type="submit" value="Search">
<input type="hidden" name="search" value="1">
</form>
<center></center>
</div>
<p>&nbsp;</p>
 
<?
$myServer = '*****';
$myUser = '****';
$myPass = '*****'; 
$myDB = '*******';
$SpecRef = $_GET['query'];
print ("$SpecRef");
 
 
 
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
 
 
 
//select a database to work with 
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//error message (not found message)begins 
$XX = "No Matches Found"; 
//query details table begins
$query = ("SELECT * FROM ******** WHERE Spec_Ref LIKE'$SpecRef%' "); 
// $query = ("SELECT * FROM *******");
 
print(' is what you searched for:');
 
 
//execute the SQL query and return records
$result = mssql_query($query);
 
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Record" . ($numRows == 1 ? "" : "s") . " Found </h1>";
 
//display the results
echo '<textarea>'; 
while($row = mssql_fetch_array($result)) 
{
echo $row["Spec_Ref"] . "\n"; }
echo '</textarea>'; 
//close the connection 
mssql_close($dbhandle); 
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["Spec_Ref"] . "</li>"; 
}
//close the connection
mssql_close($dbhandle);
 
//textareaContent = "";
 
//while ($row = @mysql_fetch_array($query))
//{
//$var1=$row["Player"];
//$var2=$row["Avg"];
//$var3=$row["HR"];
//$var4=$row["RBI"];
 
$textareaContent .= "Spec Ref: $var1 Avg: $var2 HR: $var3 RBI $var4 <br/>";
//}
 
//if (!isset($var1) || $var1=="") {
//echo $XX;
//}
 
?>
Last edited by Benjamin on Tue Jul 21, 2009 6:58 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: PHP Calculator

Post by turbolemon »

Code: Select all

 
<form action="test_search.php" method="get">
<input type="text" name="query" id="query" size="40" value="" action="Search.php" columns="2" autocomplete="off" delay="1500">
<input type="submit" value="Search">
<input type="hidden" name="search" value="1">
</form>
<center></center>
</div>
<p>&nbsp;</p>
 
<?
$myServer = '*****';
$myUser = '****';
$myPass = '*****';
$myDB = '*******';
$SpecRef = $_GET['query'];
print ("$SpecRef");
 
 
 
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
 
 
 
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//error message (not found message)begins
$XX = "No Matches Found";
//query details table begins
$query = ("SELECT * FROM ******** WHERE Spec_Ref LIKE'$SpecRef%' ");
// $query = ("SELECT * FROM *******");
 
print(' is what you searched for:');
 
 
//execute the SQL query and return records
$result = mssql_query($query);
 
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Record" . ($numRows == 1 ? "" : "s") . " Found </h1>";
 
//display the results
echo '<textarea>';
while($row = mssql_fetch_array($result))
{
echo $row["Spec_Ref"] . "\n"; }
echo '</textarea>';
//close the connection
mssql_close($dbhandle);
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["Spec_Ref"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
 
//textareaContent = "";
 
//while ($row = @mysql_fetch_array($query))
//{
//$var1=$row["Player"];
//$var2=$row["Avg"];
//$var3=$row["HR"];
//$var4=$row["RBI"];
 
$textareaContent .= "Spec Ref: $var1 Avg: $var2 HR: $var3 RBI $var4 <br/>";
//}
 
//if (!isset($var1) || $var1=="") {
//echo $XX;
//}
 
?>
 
You ought to wrap you're code in php bbcode tags . I have noticed that where you are outputting to the textarea, you feed it from mssql_fetch_array, then close the connection and try to repeat the process for the list following. This won't work because you have already exhausted the fetch in the first repetition. You ought to first add the output into an array, then loop through that:

Code: Select all

 
...
while ($row = mssql_fetch_array($result,MSSQL_ASSOC))
     $tmp[] = $row;
 
mssql_close($dbhandle);
 
echo "<textarea>";
 
foreach ($tmp as $record) {
...
 
As a quick check, you can use var_dump to check the contents of that array, and check your server logs for any errors encountered (if they don't show up in the output). I also assume also that you are using Microsoft SQL Server and not MySQL? I also notice you have an error in your query syntax: "SELECT * FROM ******** WHERE Spec_Ref LIKE'$SpecRef%'' should be "SELECT * FROM ******** WHERE Spec_Ref LIKE '%$SpecRef%'" I think, unless you are always searching from the beginning of the data, in which case you just need the space between LIKE and the term.
Post Reply