Jump to content

creating a function!


oobradersoo
Go to solution Solved by .josh,

Recommended Posts

"" 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);
		}
?>
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

<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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.