Jump to content

Question about multiple boxes


3raser

Recommended Posts

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>';
}
?>		

Link to comment
https://forums.phpfreaks.com/topic/212815-question-about-multiple-boxes/
Share on other sites

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

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

<?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>';
}
?>

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

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>';
}

?>

Archived

This topic is now archived and is closed to further replies.

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