oobradersoo Posted January 25, 2014 Share Posted January 25, 2014 could some one help with: creating a function to help you output each number, the number of times the number is … each on a separate line. So if I entered numbers 2, 3, 4 and 5 in my form then the output would be: 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Quote Link to comment Share on other sites More sharing options...
.josh Posted January 25, 2014 Share Posted January 25, 2014 what have you tried? Quote Link to comment Share on other sites More sharing options...
oobradersoo Posted January 25, 2014 Author Share Posted January 25, 2014 i have made a form which adding up each value which is entered. but i want to know if there is a way instead of adding up which is entered it will output it like i stated above. just asking for pointers really? Quote Link to comment Share on other sites More sharing options...
.josh Posted January 25, 2014 Share Posted January 25, 2014 use a loop to count from $min (e.g. 1) to $max (e.g. 10). Or if it's a list of arbitrary numbers, loop through those instead. Use str_repeat for the repetition n times. Quote Link to comment Share on other sites More sharing options...
oobradersoo Posted January 25, 2014 Author Share Posted January 25, 2014 thanks mate Quote Link to comment Share on other sites More sharing options...
oobradersoo Posted January 25, 2014 Author Share Posted January 25, 2014 i get what your saying but if a user had 4 boxes to type in how can you tell it to echo out amount of times she has entered? Quote Link to comment Share on other sites More sharing options...
.josh Posted January 25, 2014 Share Posted January 25, 2014 I'm not sure I understand what you mean. Do you mean 4 input fields where you can enter in 4 numbers, and then repeat those 4 numbers how ever many times the number is? Be more specific about what you are wanting to do. Show what code you do have, etc. Quote Link to comment Share on other sites More sharing options...
oobradersoo Posted January 25, 2014 Author Share Posted January 25, 2014 "" Do you mean 4 input fields where you can enter in 4 numbers, and then repeat those 4 numbers how ever many times the number is? "" yes that's exactly what it is theirs my code which i am wanting to change <form action="form.php" method="POST"> <input type="hidden" name="id" value="1234" /> number 1:<input type="number" name="number1" value="<?php echo $number1;?>"/> <br> number 2:<input type="number" name="number2" value="<?php echo $number2;?>"/> <br> number 3:<input type="number" name="number3" value="<?php echo $number3;?>"/> <br> number 4:<input type="number" name="number4" value="<?php echo $number4;?>"/> <input type="submit" value="submit"/> </form> <?php if(null==($number1 && $number2 && $number3 && $number4)){ echo "Please fill in form above?"; } elseif(!(is_numeric($_POST['number1']) && is_numeric($_POST['number2']) && is_numeric($_POST['number3']) && is_numeric($_POST['number4']))){ echo "please put numbers in form"; } elseif(isset($_POST['number1']) && isset($_POST['number2']) && isset($_POST['number3']) && isset($_POST['number4'])){ echo "Answer: " . ($number1+$number2+$number3+$number4); } ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted January 25, 2014 Share Posted January 25, 2014 Okay so there are a number of improvements that can be made to your existing script, but I see a difference between what you're asking for and what you're actually doing. Are you asking to just add the 4 numbers up and show the total, or are you wanting to repeat each number n times? In other words, let's say the user enters in 4 numbers: 2, 4, 5, 6 Do you want to give the user back this: 17 (the total) Or do you want to give the user back this: 2 2 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 Those are 2 different things... Quote Link to comment Share on other sites More sharing options...
oobradersoo Posted January 25, 2014 Author Share Posted January 25, 2014 i want the user to have on his screen 1 22 333 4444 Quote Link to comment Share on other sites More sharing options...
.josh Posted January 25, 2014 Share Posted January 25, 2014 <form action="" method="POST"> <input type="hidden" name="id" value="1234" /> number 1:<input type="number" name="number[1]" value="<?php echo $_POST['number'][1];?>"/> <br> number 2:<input type="number" name="number[2]" value="<?php echo $_POST['number'][2];?>"/> <br> number 3:<input type="number" name="number[3]" value="<?php echo $_POST['number'][3];?>"/> <br> number 4:<input type="number" name="number[4]" value="<?php echo $_POST['number'][4];?>"/> <input type="submit" value="submit"/> </form> <?php if ($_POST) { // trim any whitespace $numbers = array_map('trim',$_POST['number']); // filter out numbers not entered in $numbers = array_filter($numbers); // if there aren't 4 numbers, echo out error if ( count($numbers) != 4 ) { echo "Please fill in form above?"; // there are 4 numbers, echo out results } else { // for each number... foreach ($numbers as $num) { // cast to integer just in case someone alters your form's input type $num = (int) $num; echo str_repeat($num.' ',abs($num)) . '<br/>'; } } } ?> Okay so this is a better way to write the script (but there are more improvements that could be made). 1) notice in the input fields, 2 changes. First is instead of using individual input names like "number1" "number2" etc. You should use an array. This will allow you to loop through the input or use built-in functions to loop through it, instead of having to evaluate each one individually. the 2nd thing is the input field value. You were previously using $number1, $number2, etc.. which you never actually define anywhere. If this was working for you, that must mean you have register globals turned on and that is BAD. Whoever is in charge of your server needs to turn off register globals, as this is a HUGE security risk. 2) look how i replaced your form validation. Since the input fields are put into an array instead of individual variables, you can significantly decrease the code by using functions that will walk through the array, and also loop through the array. 3) finally is the part that actually outputs the repeated numbers. a simple loop made possible by using an array for your form fields. First thing to note is that I cast the value to an integer type. This is to keep php from throwing an error if someone alters your form to submit something other than a number (very easy to do). Also note that I added abs around how many times to repeat the number in str_repeat(). This is because currently your form allows you to enter in negative numbers, and you can't repeat a string negative number of times. So abs() will give the absolute value, so for example if you enter in -4, it will repeat -4 four times. Quote Link to comment Share on other sites More sharing options...
oobradersoo Posted January 25, 2014 Author Share Posted January 25, 2014 there is a reason the form is like i have set it out. is there a way i can keep the form the same and do what i have asked? Quote Link to comment Share on other sites More sharing options...
Solution .josh Posted January 25, 2014 Solution Share Posted January 25, 2014 Yes. I've given you everything you need and then some. If you want to insist on doing it like that, then apply some critical thinking to pick the relevant parts of the code out and adapt it to your own. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.