Finding the highest number using a while loop - php

I've received some c++ code regarding finding the highest number until the input been set as 0 using the form and while statement, but my echo doesn't appear, even after several times trying to key in the highest number
here some reference of C++ code that need to be transform into php format:
http://prntscr.com/plw8mw
<html>
<body>
<form method="POST">
Enter First Number:
<input type="text" name="num1"/><br>
<input type="submit"><br>
<form/>
<?php
if(isset($_POST['submit'])) {
$bignum = 0;
$num = $_POST['num1'];
while($num != 0) {
if ($num > $bignum)
$bignum = $num;
}
print "$bignum";
}
?>
<body/>
<html/>

Use echo insteed of print
echo($bignum);

Related

Printing a variable amount of checkboxradio depending on an array

I'll introduce the problem with an image :
These are checkboxradio from jquery, and what I'm trying to do is
print these checkbox but not always the same way, because the content in the checkbox will differ every time (because depending on what a user previously entered in an array)
For exemple, the suggestion 1 is of length 3 on the image, but could have a different length another time, up to a length of 10.
As well as the total number of row, which could be 1 or 2 or ... Up to 10.
I tried to code something, but it doesn't work, mostly because it's kind of disgusting and probably is a wrong way of doing it.
<?php
/*
$words is a 2 dimensional array, which contains :
row -> specific type (ex : Metal's type / Water's name...)
column -> each words of a type (ex : Iron / Copper / Barium...)
*/
$i = 1; //Iterator through lignes
$j = 0; //Iterator through columns
$length = 0; //Length of the array
/*
Print a certain number of radio button type
*/
if(!empty($words['0']) && $i==1) {
$length = count($words['0']); //Needed to know how many radio button we have to show
debug($length);
echo('Mot : 1');
//Now things starts to be wrong
?>
<html>
<fieldset>
<legend>Choose one : </legend>
<!--
Print radio button depending on the number of words,
could be all of them, or only 1.
-->
</html>
<?php
if($j < $length) {
?>
<html>
<label for="radio-1">$words['0']['0']</label>
<input type="radio" name="radio-1" id="radio-1">
</html>
<?php
$j++;
}
if($j < $length) {
?>
<html>
<label for="radio-2">$words['0']['1']</label>
<input type="radio" name="radio-1" id="radio-2">
</html>
<?php
$j++;
}
if($j < $length) {
?>
<html>
<label for="radio-3">$words['0']['2']</label>
<input type="radio" name="radio-1" id="radio-3">
</html>
<?php
$j++;
}
if($j < $length) {
?>
<html>
<label for="radio-4">$words['0']['3']</label>
<input type="radio" name="radio-1" id="radio-4">
</html>
<?php
$j++;
}
?>
<!--... Up to 10 times-->
</fieldset>
</html>
<?php
}
$i++; //Moving to the next row
//Same as above, but on an other row
if(!empty($words['1']) && $i==2) {
$length = count($words['0']);
echo('Mot : 2');
//etc...
I know it's monstrous to veteran but I couldn't find something else.
So, is there a way of printing a various amount of checkboxes properly ?
ANSWER
Thanks to the answer of Pol, I managed to do something that's working, it's still a bit disgusting, but it's wayyy better than my original way :
<?php
$i=1;
foreach ($newkeywords as $words) {?>
<h2>Groupe <?php $i?></h2>
<fieldset>
<legend>word '<?php echo($words[0])?>' :</legend>
<?php foreach ($words as $word) {?>
<label for=<?php 'checkbox-'.$i ?>><?php echo($word)?></label>
<input type="checkbox" name=<?php 'checkbox'.$i ?> id=<?php
'checkbox'.$i ?></br>
<?php $i=$i+1;?>
<?php
}
$i=1;
?></br><?php
}
?>
Still eager for other answers, it probably could be done better
Im not sure I followed but if you have a variable named 'words' and depending on how many words the length would be different. You can solve this using a foreach.
foreach($words as $word) :
// here you just print the row
// because the foreach will go through all words one by one.
// if you dont know how to access $word you can use var_dump($word)
// and then figure it out
echo '<label for="radio-1">'.$word['0'].'</label>';
echo '<input type="radio" name="radio-1" id="radio-1">':
endforeach;

Why is the num_tries counter not working?

Hi I am clearing my basics of php and i am kind of stuck in implementing this guess game. My problem is that I want $num_tries to work as a counter and it increases everytime by 1 whenever the user answers incorrectly.
<?php
$target=58;
$message="";
$num_tries=0;
if(isset($_POST["num_tries"])) {
print "set";
++$num_tries;
} else {
print "not set";
$num_tries=0;
}
if (!isset($_POST["guess"])) {
$message="Welcome to the game of Guess!!!!!!";
} elseif($_POST["guess"]> $target) {
$message= "Try a smaller Number";
} elseif($_POST["guess"] < $target) {
$message= "Try a larger Number";
} else {
$message= "congrats....You got it.";
}
?>
<HTML>
<HEAD><TITLE>Guess Game</TITLE>
</HEAD>
<BODY>
<H1><?php print $message ?></H1>
Guess Number:<?php print $num_tries ?>
<FORM action="<?php print $_SERVER['PHP_SELF'];?>" method="POST">
<INPUT type="text" name="guess">
<INPUT type="hidden" name="num_tries" value="<?php $num_tries?>">
</FORM>
</BODY>
</HTML>
You can solve it in your first lines:
$target=58;
$message="";
$num_tries = (!empty($_POST['num_tries']) ? $_POST['num_tries'] : 0);
That means if the form is not submitted before, $num_tries will be zero, otherwise, the value will be the last submitted value.
And then you need to print in the hidden element
<INPUT type="hidden" name="num_tries" value="<?php echo $num_tries?>">
---------------------------------------------------^^^^
$message="";
$num_tries=0;
change the above declaration to $num_tries=isset($_POST["num_tries"]) ? $_POST["num_tries"] : 0; which means if the post var value is set, then assign post var value, else assign 0.
and next change what you need to make is printing the value under hidden element.
you can use print or print_r or echo.
<INPUT type="hidden" name="num_tries" value="<?php echo $num_tries; ?>">
You are using <?php $num_tries ?> which literally does nothing.
You need to use <?php echo $num_tries ?> (print also works, but for your use case, echo is more efficient) in order to actually place the value into the form.
Additionally, $num_tries = 0; will not bother reading the existing value. You can use $num_tries = $_POST['num_tries'] | 0; although it will generate a PHP notice when the field isn't set, if you care about this, use isset with a ternary operator or if/else.

I want to write this idea in php

i want a input field where i can write in a number from 0 to 10.
if i write number 10 the output must be 9,7,5,3,1
or if i would write the number 8 the output should be 7 5 3 1
So only the odd numbers must be outputted when clicked on submit after
putting a number in the input form. html is combined with this php code i think.
please help.
<form action="#" method ="post">
<input type="text" name="number">
<input type="submit" name ="submit" value="submit">
</form>
<?php
if(isset($_POST['submit'])){
$number = $_POST['number'];
for($i=$number;$i>0;$i--){
if($i%2 != 0 ) // modulo prueba por pares o impares
echo $i.'<br>';
}
}
Please check above code
try this one
$inputNumber = intval($_POST[inputNumber"]);
for($i = $inputNumber ; $i >=1 ; $i--){
if($i % 2 == 1){
echo $i." ";
}
}

Setting variable and string from POST with loop

Currently I have 100 text input boxes named contact0 through contact101. I am trying to get the Post data and name each string to itself. Meaning $contact0 = $_POST['contact0'];all the way up to $contact101 = $_POST['contact101']; there has to be a simpler way to set them in a loop or something. Overall the end result is I just want the data entered in the textbox to become the value of the textbox when submitted. Any suggestions will help I might be doing this wrong for the results I want.
for ($i = 0; ;$i++){
if($i < 101){
$contact.$i = $_POST['contact'].$i;
echo $contact.$i;
}
else{
break;
}
}
for ($i = 0; $i <= 101; $i++){
${"contact".$i} = $_POST['contact'.$i];
echo ${"contact".$i};
}
You may want to consider amending your HTML form. Rather than create 100 new variables, you can assign all the contacts to an array and then reference them with the array index.
HTML
<input type="text" name="contacts[]" value="Contact 1 name"/>
<input type="text" name="contacts[]" value="Contact 2 name"/>
// etc...
PHP
$contacts = $_POST['contacts'];
var_dump($contacts);
// prints array(0 => 'Contact 1 Name', 1 => 'Contact 2 Name'...
As it now an array, you can reference the contact e.g. $contacts[34] and know it will be a valid entry.
EDIT
A working example:
<?php
if (isset($_POST['contacts'])) {
$contacts = $_POST['contacts'];
echo "<p>The contacts are below:</p>";
print_r($contacts);
} else {
echo "<p>Please enter the contacts</p>";
}
?>
<form method="post" action="">
<?php for($x = 0; $x < 100; $x++): ?>
<input type="text" name="contacts[]" value="<?php echo (isset($contacts[$x])) ? $contacts[0] : ''; ?>"/>
<?php endfor; ?>
<input type="submit" name="submit" value="submit"/>
</form>
Edit 2
I have now made the form a loop, meaning it will create all our contact inputs for us. On top of this, if we have already posted the form each field will have the correct contact values.
If you are not aware of the syntax, echo isset($contacts[$x])) ? $contacts[$x] : ''; is a ternary operator which is a one line if/else statement.
Which can also be tested here: http://phpfiddle.org/api/run/ugb-cta

calculating values from input, in PHP

I'm studying for finals and i came across this question:
Write a php script to read a positive integer, n from a input box and calculate the value of 1+2+-- n...
ive have tried for long and done sufficient research, but i have not been able to complete this so far i have:
<html>
<head>
<title>
</title>
</head>
<body>
<form action="inputnum.php" method="post" >
num:<input type="text" name="num" size ="5"/>
<input type = "submit" value = "Submit Order" />
<?php
$num=$_POST["num"];
if ($num==0)$num=="";
for($i=0; $i<=$num; $i++){
}
echo"($num+$i)";
?>
</form>
can anyone help me out?
Thanks in advance!
<?php
$num = (int)$_POST["num"];
$total = 0;
for ($i=0; $i <= $num; $i++) {
$total = $total + $i;
}
echo $total;
?>
If your code is expecting to deal with a number, it's better to do an explicit casting via (int) of the posted value
You mixed up paranthesis, you also mix = with ==. Anyway there exists a faster way of computing such sum, i.e. n * (n + 1) / 2
<?php
$num=$_POST["num"];
if ($num==0)$num="";
else
{
for($i=0; $i<=$num; $i++){
$sum=$sum+$i;
}
}
echo $sum;
?>
To be more precise you must first check whether the submit button is set or not before calculating the sum.
Firstly, this if ($num==0)$num==""; is wrong. $num=""; is what it should be. And in anycase, this would break your if-statement.
I would suggest putting the for-loop in the if-statement and changing the condition to $num>0.
Let $i begin from 1, not 0.
You could use something like this (not tested, no PHP interpreter available here):
<html>
<head>
<title></title>
</head>
<body>
<?php
$num = (int)$_POST['num'];
if(!$num) {
?>
<form action="inputnum.php" method="post" >
num: <input type="text" name="num" size ="5"/>
<input type = "submit" value = "Submit Order" />
</form>
<?php
} else {
$total = 0;
for($i=1; $i<=$num; $i++){
$total = $total + $i;
}
echo $num;
}
?>
</body>
</html>

Categories