3raser Posted September 8, 2010 Share Posted September 8, 2010 I want to create a calculator that allows you to type in how many pieces of data you have, or how many averages you have. Once that is done, the amount of boxes pop up correctly - and you can enter in your data. But how can I get all those pieces of data from EACH box using one form? Current Example: http://novacms.vacau.com/ Code: <?php $boxes = $_POST['input']; if(!$boxes) { echo '<b>How many pieces of data do you have?</b> <form action="index.php" method="POST"><input type="text" name="input"><input type="submit"></form><br/><font size="2">Ex: 88.8,92.3,76.5 - That would be 3 pieces of data</font>'; } else { echo '<form action="index.php" method="POST">'; for($i = 0; $i <= $boxes; $i++) { echo '<input type="text"><br/>'; } echo '<input type="submit" value="Get average"></form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/212815-question-about-multiple-boxes/ Share on other sites More sharing options...
wildteen88 Posted September 8, 2010 Share Posted September 8, 2010 If the user is inputting comma delimited list of data eg 88.8,92.3,76.5 Then you'll want to use explode to get each number in the list. $data = explode(',', $_POST['input']); Now $data will be contain array of each individual number, eg $data[0] will be 88.8, $data[1] will be 92.3 etc. Seeing as $data is an array you'd use a foreach loop to output each number within its own form field. echo '<form action="index.php" method="POST">'; $i = 0; foreach($data as $number) { echo ++$i . ': <input type="text" name="data[]" value="'.$number.'" /><br />'; } echo '</form>'; Quote Link to comment https://forums.phpfreaks.com/topic/212815-question-about-multiple-boxes/#findComment-1108498 Share on other sites More sharing options...
3raser Posted September 8, 2010 Author Share Posted September 8, 2010 If the user is inputting comma delimited list of data eg 88.8,92.3,76.5 Then you'll want to use explode to get each number in the list. $data = explode(',', $_POST['input']); Now $data will be contain array of each individual number, eg $data[0] will be 88.8, $data[1] will be 92.3 etc. Seeing as $data is an array you'd use a foreach loop to output each number within its own form field. echo '<form action="index.php" method="POST">'; $i = 0; foreach($data as $number) { echo ++$i . ': <input type="text" name="data[]" value="'.$number.'" /><br />'; } echo '</form>'; Can you give me an example of that code in use? I'm a bit confused. :/ Quote Link to comment https://forums.phpfreaks.com/topic/212815-question-about-multiple-boxes/#findComment-1108499 Share on other sites More sharing options...
wildteen88 Posted September 8, 2010 Share Posted September 8, 2010 <?php if(!isset($_POST['input'])) { echo ' <b>How many pieces of data do you have?</b> <form action="" method="POST"> <input type="text" name="input"> <input type="submit"></form><br/> <font size="2">Ex: 88.8,92.3,76.5 - That would be 3 pieces of data</font>'; } else { $data = explode(',', $_POST['input']); echo '<form action="" method="POST">'; $i = 0; foreach($data as $number) { echo ++$i . ': <input type="text" name="data[]" value="'.$number.'" /><br />'; } echo '</form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/212815-question-about-multiple-boxes/#findComment-1108505 Share on other sites More sharing options...
3raser Posted September 8, 2010 Author Share Posted September 8, 2010 <?php if(!isset($_POST['input'])) { echo ' <b>How many pieces of data do you have?</b> <form action="" method="POST"> <input type="text" name="input"> <input type="submit"></form><br/> <font size="2">Ex: 88.8,92.3,76.5 - That would be 3 pieces of data</font>'; } else { $data = explode(',', $_POST['input']); echo '<form action="" method="POST">'; $i = 0; foreach($data as $number) { echo ++$i . ': <input type="text" name="data[]" value="'.$number.'" /><br />'; } echo '</form>'; } ?> This is what I have: <?php if(!isset($_POST['input'])) { echo ' <b>How many pieces of data do you have?</b> <form action="index.php" method="POST"> <input type="text" name="input"> <input type="submit"></form><br/> <font size="2">Ex: 88.8,92.3,76.5 - That would be 3 pieces of data</font>'; } else { if($_POST["data"]) { $add = $data + $data; $answer = $add / $_POST["amount"]; } else { $data = explode(',', $_POST['input']); echo '<form action="index.php" method="POST">'; $i = 0; foreach($data as $number) { echo ++$i . ': <input type="text" name="data[]" value="'.$number.'" /><br />'; } echo '<input type="hidden" name="amount" value="'. $i .'"><input type="submit" value="Average"></form>'; } } ?> I'm trying to click "Average", and then see the average of all of the numbers. But I don't know how to get all of the numbers from the one form. Quote Link to comment https://forums.phpfreaks.com/topic/212815-question-about-multiple-boxes/#findComment-1108510 Share on other sites More sharing options...
wildteen88 Posted September 8, 2010 Share Posted September 8, 2010 Try this instead <?php if(!isset($_POST['input'])) { echo ' <b>How many pieces of data do you have?</b> <form action="" method="POST"> <input type="text" name="input"> <input type="submit" name="submit" value="Submit"></form><br/> <font size="2">Ex: 88.8,92.3,76.5 - That would be 3 pieces of data</font>'; } else { if(!is_array($_POST['input'])) $data = explode(',', $_POST['input']); // get data from first form else $data = $_POST['input']; // get data from secound form echo ' <b>Data Values</b> <form action="" method="POST">'; $i = 0; foreach($data as $number) { echo ++$i . ': <input type="text" name="input[]" value="'.$number.'" /><br />'; } // work out the average $dataSum = array_sum($data); $average = $dataSum / $i; echo ' <b>Average:</b>: ' . $average . '<br /> <input type="submit" name="submit" value="Recalculate Average" /> </form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/212815-question-about-multiple-boxes/#findComment-1108515 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.