0

I want to add a column in my table that will contain check boxes that if it will be selected I may be able to get all the id's of the selected row data.

Here's my code:

<table>
    <thead>
        <th>Name</th>
        <th>Company</th>
        <th>Address</th>
    </thead>
    <tbody>
            <?php 
                $sql = "SELECT * FROM Client";
                $qry = mysql_query($sql);

                while($row = mysql_fetch_array($qry)){
                    echo "<tr>
                            <td>$row[name]</td>
                            <td>$row[company]</td>
                            <td>$row[address]</td>
                        </tr>";
                }
    </tbody>
</table>

My problem is, how could I add another column that will contain check boxes that will represent the id of row data being selected. And how can I retrieve those values? Thanks!

5
  • what do you want to do with the values? its quite easy to work with (id - primary key field) such as you did with (name, company, and address) just add <td>$row[id]</td> please replace id with id in your table. After this, you can replace it with ( <input name="id" type="checkbox" value=""> ) please this is just a pattern not exact answer. Commented Jan 28, 2013 at 5:24
  • I wanted to get those id's selected to be able to perform a delete function that will delete all those selected values. Commented Jan 28, 2013 at 5:29
  • can you post your table columns? Commented Jan 28, 2013 at 5:30
  • ClientId,Name,Company,Address these are my columns in the database. Commented Jan 28, 2013 at 5:31
  • add this to while loop. <input name="ClientId[$row[ClientId]]" type="checkbox" value="$row[ClientId]"> after this, you need a form for sending selected chekcboxes, or you can depend on javascript. Commented Jan 28, 2013 at 5:38

3 Answers 3

2

Just add an another and create a check box array and store the value.

while($row = mysql_fetch_array($qry)){
   echo "<tr>
            <td>$row[name]</td>
            <td>$row[company]</td>
            <td>$row[address]</td>
            <td><input type='checkbox' name='row_id[]' id='rowid_<?php echo $id ?>' value='<?php echo $id ?>' />
         </tr>";
}

Then you can retrieve the values by using $_POST[] method and use whatever you want.

$rowid      = $_POST['row_id'];

But it will return the selected checkboxes value in an array format. You can use a loop to access all the elements or you can use implode() function to use as a string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'll be trying that one.
0

It depends on your database table layout. In the best case you have an auto increment column called company_id. Otherwise it will be very hard to distinguish between rows.

Comments

0

Try like this

while($row = mysql_fetch_array($qry)){
                echo "<tr>
                        <td><input type='checkbox' id='$row[id]' name='$row[id]'></td>
                        <td>$row[name]</td>
                        <td>$row[company]</td>
                        <td>$row[address]</td>
                    </tr>";
            }

1 Comment

There is a condition fro my answer is it need an id field in that table and it may auto increment or an manual entry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.