Reputation: 59
I have a list of plants that comes as a MySQL query. I list the results in a table. I would like to perform math on each individual plant in the list, based on its size, which is chosen in a select dropdown list. I have placed a submit button at the end of each row to do the math for that row.
When I choose a plant size, and click the submit button for that row, the math is performed on all plants (rows) in the list, instead of just the plant in the row I chose. I would like to have the math work independently on each plant as a plant size is chosen. I suspect the problem is that I'm using a "while" loop to print the plant list to a table, but I haven't been able to figure out how to remedy this.
Here is my code. I apologize for any funkiness in portraying this. I'm fairly new to the game, and I'm not used to asking questions on this forum, only finding answers. Thank you in advance.
Lori
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<table><tr>
<td>' . $bot_name . '</td>
<td>' . $region_eval . '</td>
<td>
<form action="drip_worksheet.php" method="POST">
<select name="size" value="">
<option value="1">1 foot</option>
<option value="2">2 feet</option>
<option value="3">3 feet</option>
<option value="4">4 feet</option>
</select>
</td>
<td>';
if ($_POST['size']) {
$size = $_POST['size'];
$peak_water = $peak_et * .8 * 0.7854 * $size * $size * .623;
echo number_format($peak_water,1) . ' gallons per week';
}
echo '</td><td><input type="submit" name="Submit" value="submit" />
</tr></form>';
}
echo '</table><br>';
Upvotes: 0
Views: 137
Reputation: 609
Try something like this:
echo '<form action="drip_worksheet.php" method="POST">';
echo '<table>';
$row_count = 0;
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$size = isset($_POST['size_' . $row_count]) && $_POST['size_' . $row_count] ? intval($_POST['size_' . $row_count]) : '';
echo '<tr>
<td>' . $bot_name . '</td>
<td>' . $region_eval . '</td>
<td>
<select name="size_' . $row_count . '" value="' . $size . '">
<option value="1"' . ($size == 1 ? ' selected="selected"' : '') . '>1 foot</option>
<option value="2"' . ($size == 2 ? ' selected="selected"' : '') . '>2 feet</option>
<option value="3"' . ($size == 3 ? ' selected="selected"' : '') . '>3 feet</option>
<option value="4"' . ($size == 4 ? ' selected="selected"' : '') . '>4 feet</option>
</select>
</td>
<td>';
if ($size) {
$peak_water = $peak_et * .8 * 0.7854 * $size * $size * .623;
echo number_format($peak_water,1) . ' gallons per week';
}
echo '</td><td><input type="submit" name="Submit" value="submit" />
</tr>';
$row_count++;
}
echo '</table><br>';
echo '</form>';
The form is now containing all selects so all chosen values are transmitted each time the form is submitted. Inside the loop there is a counter which is appended to the name-attributes of the select boxes so they can be associated with the data items. That counter is also appended to the index used to access $_POST in order to access the right selected value in each row.
I did not test this code, but it should give you an idea how it could be done.
Upvotes: 1