Adding a checkbox in each row to a PHP table

Hi guys,

Here a table I made using PHP:

    print ("<TABLE $table_format>\

");

// table header, two columns:
print(“<TR>
“);
print(”<TH>Title</TH>”);
print(“<TH>Price</TH>”);
print("</TR>
");

//table body:
while($row = mysql_fetch_row($result)) {
print(“<TR ALIGN=CENTER VALIGN=TOP>”);
for($column_num = 0; $column_num < $column_count; $column_num++) {
print("<TD>$row[$column_num]</TD>
“);
}
print(”</TR>
“);
}
print(”</TABLE>
");

How could I add a checkbox at the left of every row?
An then, make a new table considering only checked rows.

As a final request, I have been suggested to replace print() instructions by echo ones. I have tried but I guess I don’t know the syntaxis of echo sentences very well.
How should I write these ones, for instance:
print("</TR>
“);
print(”<TD>$row[$column_num]</TD>
");

Thanks a lot!!!

How could I add a checkbox at the left of every row?

Add a new column to table, and put a checkbox in each cell.

How should I write these ones, for instance:
print("</TR>
“);
print(”<TD>$row[$column_num]</TD>
");


echo "</TR>\
");
echo "<TD>$row[$column_num]</TD>\
";

Hi Guido,

“Add a new column to table, and put a checkbox in each cell.”

Could you please help me with the PHP code for going what you suggested?

Thanks a lot!!!

Why don’t you start by adding a new column to the table? You wrote the code that creates a two column table, I’m sure you can add a third.

Yed, done. Now I have a third column, with only the header and with no values in the table body.
How can I fill it with checkboxes?

I already changed print sentences by echo ones!

Thanks!!!

    echo "&lt;table $table_format&gt;\

";
// table header:
echo "</tr>
";
echo “<th>Title</th>”;
echo “<th>Price</th>”;
echo “<th>Checkbox</th>”;
echo "</tr>
";

//table body:
while($row = mysql_fetch_row($result)) {
echo “<tr ALIGN=CENTER VALIGN=TOP>”;
for($column_num = 0; $column_num < $column_count; $column_num++) {
echo "<td>$row[$column_num]</td>
";
}
echo "</tr>
";
}
echo "</table>
";

Ok, now in the table body part add an echo after the for loop:


echo '<td><input type="checkbox" name="check[]" value="..."/></td>";

If you want to show the checked rows once the form has been submitted, then you’ll have to put a value where I put ‘…’ that identifies the row (for example the ID).

If you google for ‘php form’ you’ll find lots of tutorials and examples. This one for example: http://www.html-form-guide.com/php-form/php-form-checkbox.html

Great!!

I will take a look.
How can I writh the sentence in order checkboxes appear selected?

Thanks!!