I want to write this idea in php - 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." ";
}
}

Related

Form Inputs and PHP

Need a little help with my coding. I'm working on a project in which it will accept 5 different numbers and insert them into a PHP array. The codes I have tried are written below. Both of them would make all the 5 contents of my array the same number, appreciate a little help please? Never dealt with array in PHP yet.
<form action="activity_1.php" method="post">
<input type="text" name="number">
<input type="submit" value="Submit" name="submit">
</form>
So far these are the ones I've tried:
$no = array();
for($i=1; $i<=5; $i++){
array_push($no, $_POST['number']);
}
Also
$no = array();
for($i=1; $i<=5; $i++){
//$no[$i]= $_POST['number'];
}
You may use one session variable to store the posted data.
So slightly amend your code into the following :
<?php session_start(); ?>
<form action="#" method="post">
<input type="text" name="number">
<input type="submit" value="Submit" name="submit">
</form>
<?php
if ( !isset($_SESSION["no"]) ) {
$_SESSION["no"]=array();
}
if (isset($_POST["number"])){
array_push($_SESSION["no"], $_POST['number']);
}
for($i=0; $i<5; $i++){
if (isset($_SESSION["no"][$i])) {
if ($_SESSION["no"][$i]!="") {
echo " Number you entered was: " .$_SESSION["no"][$i] . "<br>";
}
}
}
?>
In my opinion the code can be further amended so that
a) you should have a button to "clear" all the previous entered number;
b) I don't know whether it is necessary, but you may wish to add a function to check whether a newly entered number is the same as one of the past numbers entered, and if they are the same, do not put it into the array.

Finding the highest number using a while loop

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);

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;

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