Reputation: 93
So, I have a form with a HTML table.
Each box of the table contains a text field. Each of those fields have the same name like this :
<input name="quantity[]" type="text" />
The HTML table looks like this :
Row are numbers. Columns are month. Users have to put a number (a quantity) in at least one box.
Then, they can submit the form.
My SQL table should be like this :
Number - Month - Quantity
222000 - 12 - 50
222000 - 02 - 31
300000 - 01 - 25
221000 - 02 - 28
As you see, 1 line in my table = 1 input text.
In PHP, I get the data like this :
foreach($_POST['quantity'] as $key => $text_field){
if(!empty($text_field)) {
$requete = $bdd->prepare('INSERT INTO my table VALUES(:number, :month, :quantity)');
$requete->execute(array(
':number' => *???*,
':month' => *???*,
':quantity' => $text_field
));
}
}
My question : how to detect the row number and the column number from my HTML table in PHP and put the result into my SQL table?
Upvotes: 3
Views: 2065
Reputation: 1758
lets assume that you have 12 moths and array "numbers" with number value for each row:
foreach($_POST['quantity'] as $key => $text_field){
if(!empty($text_field)) {
$number= $numbers[floor($key/12)];
$month=($key%12)+1;
$requete = $bdd->prepare('INSERT INTO my table VALUES(:number, :month, :quantity)');
$requete->execute(array(
':number' => $number,
':month' => $month,
':quantity' => $text_field
));
}
}
Just dont forget about 0 indexes.
Upvotes: 1
Reputation: 165
<?php
$n=1;
$num = array(111,222,333,...);
foreach($num as $key=>$n_val)
{
?>
<tr>
<td>
<input type="hidden" name="number[<?php echo $key ?>]"
value='<?php echo $n_val ?>'/>
<?php echo $n_val ?>
</td>
<td><input name="quantityNov[<?php echo $key ?>]" type="text" /></td>
<td><input name="quantityDec[<?php echo $key ?>]" type="text" /></td>
....
</tr>
<?php
}
?>
Then in your submit action portion of php;
<?php
foreach($_POST['quantityNov'] as $key => $val)
{
$fixed_number = $_POST['number'][$key];
$month[11] = $val;
$month[12] = $_POST['quantityDec'][$key];
$month[1] = $_POST['quantityJan'][$key];
...
$count = 0;
foreach($month as $month_num=>$n_val)
if(!empty($n_val))
{
$sql = 'INSERT INTO my table (column(s)) VALUES ('.$fixed_number.','.$month_num.', '.$n_val.')';
$result = $connection->query($sql);
$count += $connection->affected_rows;
}
}
echo 'Number of rows inserted: '.$count;
?>
This is how I like to write my code, but you can write it in your preferred style, but I think this will get you going.
Upvotes: 0
Reputation: 40690
You could do the following :
When you populate the table:
<input name="quantity[<?= $row ?>][<?= $column ?>]" type="text" />
Then in your post handler script:
foreach($_POST['quantity'] as $row => $columns){
foreach ($columns as $column => $text_field) {
if(!empty($text_field)) {
$requete = $bdd->prepare('INSERT INTO my table VALUES(:number, :month, :quantity)');
$requete->execute(array(
':number' => $row,
':month' => $column,
':quantity' => $text_field
));
}
}
}
In short you make the post fields an associative array rather than a normal one.
Upvotes: 1