Teletubbies
Teletubbies

Reputation: 113

Get value from dropdown in a table row codeigniter

I'm having problem getting value from dropdownlist that generated using foreach $data as $d according to database the code is like this

<table>
    <thead>
        <th>ID</th>
        <th>Action</th>
    </thead>
    <tbody>
        <?php
            foreach ($data as $d) {
        ?>
        <tr>
            <td><?php echo $d->id; ?></td>
            <td>
                <select name="option" id="option">
                    <option value="A" selected>A</option>
                    <option value="B">B</option>
                </select>
            </td>
        </tr>
    <?php } ?>
</tbody>

how can i get the dropdown value according the data that showing on the table?

like if there's 3 data showing (that means 3 dropdown with same name and id) and i get 3 values for each data?

Edit: in short, the data that i need to sent to my controller is: - the value from selected dropdownlist - the id values from each row so i can using it for my query later combining with value from dropdownlist

Note: i tried using this in foreach

$x=1;


$x++;

with

name/id="option<?php echo $x;?>"

and this for each table row

<input type="hidden" id="id<?php echo $x; ?>" name="id<?php echo $x; ?>" value="<?php echo $d->id; ?>">

it works, but i think there's still better way for this

Upvotes: 0

Views: 1180

Answers (2)

Edwin
Edwin

Reputation: 1135

So assuming your data is structured in an array e.g.

$data = [
    "A" => [
        "id" => 1,
        "values" => [
            "lorem",
            "ipsum",
            "donec"
        ]
    ],
    "B" => [
        "id" => 2,
        "value" => [
            "lorem",
            "ipsum",
            "donec"
        ]
    ],
    "C" => [
        "id" => 3,
        "value" => [
            "lorem",
            "ipsum",
            "donec"
        ]
    ],
];

Printing the array as dropdowns would be done like this

<form method="post" action="/url/to/post/form/to">
...
    <?php foreach ($data as $row): ?>
        <select name="options[<?php $row['id']; ?>]" id="option">
            <?php foreach ($row['values'] as $value): ?>
                <option value="<?php echo $value; ?>"><?php echo $value; ?></option>
            <?php endforeach; ?>
        </select>
    <?php endforeach; ?>
...
</form>

This will result in a dropdown with three options; A, B and C. If the user select one of these options and the form is POSTed You can read the selected option as:

...
    foreach ($_POST['options'] as $option) {
        var_dump($option);
    }
...

Note: Make sure to do proper validation on the field and it's values to prevent unwanted, undesired behaviour in your application.

Reference material:
https://www.codeigniter.com/user_guide/libraries/input.html
https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_and_retrieving_form_data
https://www.w3schools.com/php/php_form_validation.asp
http://phpsec.org/projects/guide/2.html

Upvotes: 1

Husso
Husso

Reputation: 272

If I understand the question correctly, below is the proper implementation of a select element with a foreach loop creating the list items.

<select name="foo">
    <option value="0">-------</option>
    <?php 
        foreach($foo as $row)
            echo '<option value="'.$row->DataID.'">'.$row->Data.'</option>';
    ?>
</select>

Upvotes: 1

Related Questions