mercredi 24 décembre 2014

PHP, checkbox which returns multiple values (rows)

I have a table with some attributes and I need a checkbox in every row. This table is shown to users and they can check some rows. At least one row should be checked, but there is no limit of how many of them can be checked, all checkboxes can be checked. When user chooses some rows, I need to get two attributes of every selected row - date and id. After that I need to have these attributes in an array, where I can reach them like this:



echo $pole[0]; --> output: 1.12.2014
echo $pole[1]; --> output: 4
echo $pole[2]; --> output: 5.12.2014
echo $pole[3]; --> output: 8


Here's my code:



session_start();
include('connect.php');

$stmt = $db->query('SELECT * FROM tabulka');

foreach($stmt as $row)
{
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['deadline'] . "</td>";
echo "<td> <input type=\"checkbox\" name=\"interest[]\" value=\"" . $row['date'] . " " . $row['id'] . " " . "\"></td>";
}


The last echo creates a checkbox in every row of the table, but I'm not sure, if it's correct. When user chooses some rows and press the submit button it goes to another file:



session_start();

if(isset($_POST['submit'])) {//to run PHP script on submit
if (!empty($_POST['interest'])) {
$data = $_POST['interest'];
foreach ($data as $selected) {
echo $selected."</br>";
}


This works, but I can't reach those attributes as I want to. I tried to split $data, but it hasn't worked. I can only reach a char of the array or the whole row (date and id, but not only date or only id).


Can someone help, please? Also if there is a better solution, I'd be grateful.


BTW: Merry Christmas stackoverflowers !!





Aucun commentaire:

Enregistrer un commentaire