Multiple input fields from PHP table

To summarize from a previous question, the user selected a starting date, an ending date, and a region. From these choices, an array of weeks and regions are created, and users enter values ("points") that correspond to a particular week and region. This is what it looks like right now (some parts are hard-coded for testing purposes):

$date_array = Array("01/01/2012", "01/08/2012", "01/15/2012");$region_array = Array("NYC", "DC");

<form name="test_form" id="test_form" method="post">
<table border="1">
<tr>
<td></td>
<?php
foreach ($date_array as$date)
{
echo "<td>".$date."</td>"; } ?> </tr> <?php foreach ($region_array as $region) { echo "<tr>"; echo "<td>".$region."</td>";
foreach ($date_array as$date)
{
echo '<td><input type="text" name='.$region."-value1_".$date.'><input type="text" name='.$region."-value2_".$date.'></td>';

}
echo "</tr>";
}
?>
</table>
<input type="submit" name="submit">


And the next part:

if (isset($_POST['submit'])) { foreach ($_POST as $k=>$v)
{
if ($k != "submit") // use even and odd count to differentiate TRP and CPP { // split the form input on _$input = explode("_", $k); echo "<BR>Region: " .$input[0];
echo "<BR>Date: " . $input[1]; echo "<BR>Value: " .$v;
}
echo "<br/>";
}
}


So basically, in this example it outputs 12 times:

Region: NYC-value1
Date: 01/01/2012
Value: 500

Region: NYC-value2
Date: 01/01/2012
Value: 5

Region: NYC-value1
Date: 01/08/2012
Value: 600


I want to catch the value1 and value2 while the loop is running, but as it stands it loops through both of them before going onto the next date/region. My idea, as I put in comments, was to have a counter of even and odd numbers and simply tell if it was doing value1 or value2 based on that.

Is this a good idea or am I missing something fundamentally simpler?

1 Answer

Try this:

<?php
if(isset($_POST['submit'])){ foreach ($_POST as $k=>$v){
// use even and odd count to differentiate TRP and CPP
if ($k != "submit") { // split the form input on _$input = explode("_", $k); echo "<BR>Region: " .$input[0];
echo "<BR>Date: " . $input[2]; if($input[1] == "value1"){
echo "<BR>Value 1: " . $v; } elseif($input[1] == "value2"){
echo "<BR>Value 2: " . $v; } } echo "<br/>"; } } else {$date_array = Array("01/01/2012", "01/08/2012", "01/15/2012");
$region_array = Array("NYC", "DC"); ?> <form name="test_form" id="test_form" method="post" action=""> <table border="1"> <tr> <td></td> <?php foreach ($date_array as $date){ echo "<td>".$date."</td>";
}
?>
</tr>
<?php
foreach ($region_array as$region){
echo "<tr>";
echo "<td>".$region."</td>"; foreach ($date_array as $date){ echo '<td><input type="text" name='.$region."_value1_".$date.'><input type="text" name='.$region."_value2_".\$date.'></td>';
}
echo "</tr>";
}
?>
</table>
<input type="submit" name="submit">
</form>
<?php
}
?>

• Ah I see, you created an extra "_" in the form and made the value inside a conditional clause for which value to display. In the future, I can replace the echo inside of the if statements with database queries. Looks like you did it again, thanks. I'm gonna try to keep learning about web development and thanks to you, I've learned a few nice ways to use the explode() function. – Custardboy Jul 23 '13 at 19:38
• sure no problem. Glad I could help. PHP has a very good collection of functions when it comes to string manipulation. If you are completely new to PHP/MySQL then you might want to focus next on should be using MySQLi/PDO and parametrized queries, input data validation and security etc. – Maximus2012 Jul 23 '13 at 19:40