I'm very new to programming, I am trying to take create a converter from Celsius to Fahrenheit to Kelvin. The user inputs the values (in celsius) they want converted in the 2 input boxes and it creates a table using loops. The first set of data where it outputs the amounts in celsius looks great however the second data stream (fahrenheit) will only output one value of celsius which is the converted value of the final nuber in the celsius loop.
<form name="calculator" action="" method="post">
From: <input class="inputbox" type="number" name="one" value="" /><br />
<p>to</p><br>
To: <input class="inputbox" type="number" name="two" value="" /><br />
<input type="submit" class="submit" name="submit" value="Get Conversions!" />
</form>
<br>
<table border='1' cellpadding='5px'>
<tr>
<th>Degrees Celsius</th>
<?php
if ($_POST['submit']) {
$one = $_POST['one'];
$two = $_POST['two'];
}
if ($two < $one) {
echo "<p>Please put the lowest number in the first input box.</p>";
} else if ($one < (-273) OR $two < (-273)) {
echo "<p> Tempature cant go below -273 Celsius (0 kelvin), please enter higher values.</p>";
} else {
$c = $one - 1;
do {
$c++;
echo "<td>" . $c . "</td>";
} while ($c < $two);
}
?>
</tr>
<tr>
<th>Degrees Fahrenheit</th>
<?php
$f = (1.8 * $c) + 32;
do {
$c++;
echo "<td>" . $f . "</td>";
} while ($c < $two);
$k = $x - 273;
?>
</tr>
</table>
Your have 2 problems in your code.
One problem is that you are only assigning c once, when you are running the first while, it's counting C up and then when you get to the second do{}while() it's allready at the maximum.
you should "reset" C after The first while loop like here:
<th>Degrees Fahrenheit</th>
<?php
$c = $one - 1;
Secondly your only counting your f variable once, you should either make a function for it (might be overkill in this case) or move your calculation of f down inside the while loop, the last part of your code would be something like this
Degrees Fahrenheit
<?php
$c = $one - 1;
do {
$f = (1.8 * $c) + 32;
$c++;
echo "<td>" . $f . "</td>";
} while ($c < $two);
$k = $x - 273;
?>
</tr>
Related
I am trying to create an adding game PHP and HTML, where the user guesses the sum of two random integers. However, when the user submits the answer, the integers randomize again, changing the sum and making the user's guess incorrect.
<?php
$x = (rand(1,10));
$y = (rand(1,10));
$sum = $x + $y;
if (isset($_POST['submit']))
{
$guess = $_POST['guess'];
if ($sum == $guess)
{
echo "nice, you got it right.";
}
else {
echo "aw, too bad. <br>";
}
}
echo "<br>What is " . $x . " + " . $y . "? <br>";?>
<form action="" method="post">
<input name="guess" type="text" />
<input name="submit" type="submit" />
</form>
I am expecting the output to be "nice, you got it right" when the $guess==$sum, but $sum changes when the form is submitted, making the 2 variables unequal.
Try to use session and store the two random number. PHP Session
just like this.
<?php
session_start();
if (isset($_POST['submit']))
{
$sum = $_SESSION['y'] + $_SESSION['x'];
$guess = $_POST['guess'];
if ($sum == $guess)
{
echo "nice, you got it right.";
}
else {
echo "aw, too bad. <br>";
}
}
$_SESSION['x']= (rand(1,10));
$_SESSION['y']= (rand(1,10));
echo "<br>What is " . $_SESSION['x'] . " + " . $_SESSION['y'] . "? <br>";
?>
<form action="" method="post">
<input name="guess" type="text" />
<input name="submit" type="submit" />
</form>
I hope I answer your problem. just read the PHP session it helps you a lot.
You can create a hidden input field to post the sum value. Then compare that value with the guessed value.
<?php
$x = (rand(1,10));
$y = (rand(1,10));
$sum = $x + $y;
if (isset($_POST['submit'])) {
$guess = $_POST['guess'];
$value = $_POST['sum'];
if ($value == $guess) {
echo "nice, you got it right.";
} else {
echo "aw, too bad. <br>";
}
}
echo "<br>What is " . $x . " + " . $y . "? <br>";
?>
<form action="" method="post">
<input name="guess" type="text" />
<input type="hidden" name="sum" value="<?=$sum?>" />
<input name="submit" type="submit" />
</form>
You should store your $x and $y in a hidden input.
Calculate the sum by using the post value instead of generating new value.
How can i do 3*3 or 3*1 matrix multiplication in php with following code. where should i have to change the logic ?????
this code completely work for only 2*2 matrix multiplication
Here is my code:
$a = Array( Array(1,2),Array(4,5));
$b = Array( Array(7,5), Array(3,2));
$sumArray = array();
$c = array();
for($i=0;$i<2;$i++)
{
for($j=0;$j<2;$j++)
{
$c[$i][$j]=0;
for($k=0;$k<2;$k++)
{
$c[$i][$j]=$c[$i][$j]+($a[$i][$k]*$b[$k][$j]);
}
}
}
echo "<pre/>";
print_r($c);
?>
Matrix Multiplication
Rules :
First matrix's column and Second matrix's row must be same
Result matrix's size will be First matrix's Row and Second matrix's Column
$a = Array( Array(1,2),Array(4,5));
$b = Array( Array(7,5), Array(3,2));
$r=count($a);
$c=count($b[0]);
$p=count($b);
if(count($a[0]) != $p){
echo "Incompatible matrices";
exit(0);
}
$result=array();
for ($i=0; $i < $r; $i++){
for($j=0; $j < $c; $j++){
$result[$i][$j] = 0;
for($k=0; $k < $p; $k++){
$result[$i][$j] += $a[$i][$k] * $b[$k][$j];
}
}
}
print_r($result);
Matrix computation is achieved by multiplying any column or row elements by their own cofactors.
Codes according to your request;
<?php
if ($_POST['hsp']) {
$de3t11=$_POST['m11']; $de3t12=$_POST['m12']; $de3t13=$_POST['m13'];
$de3t21=$_POST['m21']; $de3t22=$_POST['m22']; $de3t23=$_POST['m14'];
$de3t31=$_POST['m31']; $de3t32=$_POST['m23']; $de3t33=$_POST['m15'];
//show on screen
echo $de3t11 ." ". $de3t12. " ". $de3t13;
echo "<br>";
echo $de3t21 ." ". $de3t22. " ". $de3t23;
echo "<br>";
echo $de3t31 ." ". $de3t32. " ". $de3t33;
echo "<br>";
//calculate matrix
$m3m11=(($de3t22*$de3t33)-($de3t23*$de3t32))*(1);
$m3m12=(($de3t21*$de3t33)-($de3t23*$de3t31))*(-1);
$m3m13=(($de3t21*$de3t32)-($de3t22*$de3t31))*(1);
//Determinant
$det3t3=(($de3t11*$m3m11)+($de3t12*$m3m12)+($de3t13*$m3m13));
echo $det3t3;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="POST">
<input type="number" name="m11">
<input type="number" name="m12">
<input type="number" name="m13">
<br><br>
<input type="number" name="m21">
<input type="number" name="m22">
<input type="number" name="m23">
<br><br>
<input type="number" name="m31">
<input type="number" name="m32">
<input type="number" name="m33">
<input type="submit" name="hsp">
</form>
</body>
</html>
Writing a simple script that takes the users string, if it has even characters it counts to 100 with 5's, if odd it count's to 100 with 10's. When loaded in the browser by default it is printing to 100 by 5's. As an empty string 0 qualifies for the first part of my loop. Looking for advice on how to do this properly. Thank you in advance.
<form method="post"<br />
PLEASE ENTER A WORD: <input type="text" name="word" value="" /><br />
<input type="submit" value="submit" name="submit"><br />
</form>
<?php
if (isset($_POST['word'])){
$word = $_POST['word'];
} else {
$word ='';
}
?>
<?php
$n = 0;
$word_length = strlen($word);
while ($n < 100){
if($word_length % 2 == 0){
$n += 5;
echo $n . "<br />";
} else if($word_length % 2 == 1){
$n += 10;
echo $n . "<br />";
}
}
?>
I tried to make a quadratic equation solver in php:
index.html:
<html>
<body>
<form action="findx.php" method="post">
Find solution for ax^2 + bx + c<br>
a: <input type="text" name="a"><br>
b: <input type="text" name="b"><br>
c: <input type="text" name="c"><br>
<input type="submit" value="Find x!">
</form>
</body>
</html>
findx.php:
<?php
if(isset($_POST['a'])){ $a = $_POST['a']; }
if(isset($_POST['b'])){ $b = $_POST['b']; }
if(isset($_POST['c'])){ $c = $_POST['c']; }
$d = $b*$b - 4*$a*$c;
echo $d;
if($d < 0) {
echo "The equation has no real solutions!";
} elseif($d = 0) {
echo "x = ";
echo (-$b / 2*$a);
} else {
echo "x1 = ";
echo ((-$b + sqrt($d)) / (2*$a));
echo "<br>";
echo "x2 = ";
echo ((-$b - sqrt($d)) / (2*$a));
}
?>
the problem is that it's returning wrong answers (d is right,x1 and x2 are not) seems like sqrt() is returning zero or maybe something else.
There's a typo in this line:
elseif($d = 0)
which is assigning the value 0 to $d instead of comparing it. That means you are always evaluating sqrt(0), which is 0, in your else block.
It should be:
elseif($d == 0)
I do a form, inside I would like to do a loop for to show the same field. Each field will have different value and I would like to use sessions to take all the value.
Here is my code:
for ($i = 1; $i <= 5; $i++) { //normally not 5 but a random number, choose by user
echo "Numero ";
echo $i;
?>
<input type="text" name="number2" id="number2"/>
<?php
}
?>
</form>
<?php
echo $_POST['number2'];
$my_array=array($_POST['number2']);
$_SESSION['countnumb']=$my_array;
in another page:
foreach($_SESSION['countnumb'] as $key=>$value)
{
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
I can't register any number. How can I do this? thanks
Basics First - ids should be unique in a webpage.
Declaring <input type="text" name="number2" id="number2"/> in a loop is wrong.
For creating multiple input using loop try like this-
echo "<input type='text' name='number[$i]' id='number{$i}' />";
<input type="text" name="number[2]" id="number2"/>
would make $_POST['number'] an array which you can loop though server side, This is described here http://www.php.net/manual/en/faq.html.php#faq.html.arrays
foreach ($_POST['number'] as $number){
echo $number;
}
for example
this would make your code
for ($i = 1; $i <= 5; $i++) { //normally not 5 but a random number, choose by user
echo "<input type="text" name="number[$i]" id="number{$i}"/> ";
?>
<?php
}
?>
</form>
<?php
print_r( $_POST['number'] );
$_SESSION['countnumb']= $_POST['number'];