Reputation: 51
is there any way I can get the iterate through a lot of input type number's and only print out the values that have a value greater than 0?
Let's say I have 10
<form action="displayInfo.php" method="post">
<table cellpadding="6">
<tr>
<th>Description</th>
<th>Price</th>
<th>Weight</th>
<th>Image</th>
<th>Quantity</th>
</tr>
<!-- PHP LANGUAGE -->
<?PHP
$sql = "SELECT * FROM parts";
$q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorIndo()));
while( $row = $q->fetch(PDO::FETCH_ASSOC))
{
echo '<tr>' .
'<td>' . $row['description'] . '</td>' .
'<td>' . $row['price'] . '</td>' .
'<td>' . $row['weight'] . '</td>' .
'<td> <img src="' . $row[pictureURL] .'" alt="' . $row[number] . '" style="width:50px;height:50px;">' .
'<td> <input type="number" name = "' . $row[number] ." id= "'. $row[number] . '" value="0">' .
'</td>' . '</tr>';
}
echo "</table>";
?>
</table> <br>
<input type="button" id="submit" value="Submit" />
</form>
So they are dynamically created with an id that have values 1,2,3,...10. The values are then updated through user input.
Is there anyway I can catch the data passed by the submit button for the values entered by the user that are greater than 0? If so, how would I also pass the description or even the $row[number] along with the value that is associated with it. the id of the input type number = the $row[number] as displayed in the code.
Upvotes: 3
Views: 6765
Reputation: 146390
The simplest way is probably to use a PHP feature, explained at How do I create arrays in a HTML <form>
?
In short, you rename the form controls to use a common prefix, add square brackets to make PHP convert them into array and then loop data as any other array:
'<td> <input type="number" name = "row[' . $row[number] . ']" id= "'. $row[number] . '" value="0">' .
I've also fixed a missing quote but I presume it isn't in your actual code.
Then:
foreach ($_POST['row'] as $number => $value) {
}
And since it's an array you can use the usual goodies, e.g.:
$rows = array_filter($_POST['row'], function($val){
return is_numeric($val) && $val>10;
});
Upvotes: 3