Jirka Mára
Jirka Mára

Reputation: 15

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 !!

Upvotes: 0

Views: 1112

Answers (2)

GStaes
GStaes

Reputation: 328

I suggest you change the line where you create your input-tag to this:

echo "<td> <input type=\"checkbox\" name=\"interest[" . $row['id'] . "]\" value=\"" . $row['date'] . "\"></td>";

This way, the id will be used as the key of the date-value in your interest-array.

Then change the foreach in your other file to this:

$data = $_POST['interest'];        
$selected_items = array();         
foreach ($data as $id => $date) {  
    // $id is the key of the array, $date is the value of the array

    // Add an array with the data for this selected value to the end of the array with selected items
    $selected_items[] = array('id' => $id, 'date' => $date);
}

var_dump($selected_items); // This should display all the selected items in your form

You can now use a foreach to loop through your array of selected items.

Upvotes: 2

vaso123
vaso123

Reputation: 12391

Explode is good for you, I don't know why isn't it worked for you.

if (isset($_POST['submit'])) {//to run PHP script on submit
    $dataArray = array();
    if (!empty($_POST['interest'])) {
        foreach ($_POST['interest'] as $selected) {
            $pieces = explode($selected);
            $dataArray = array(
                'id' => $selected[0],
                'date' => $selected[1]
            );
        }
        var_dump($dataArray);
    }
}

You can add your ID as the key of array, if you want, if you use this:

$dataArray[$selected[0]] = $selected[1];

Upvotes: 1

Related Questions