alwaysStuckJava alwaysStuckJava - 9 days ago 7
CSS Question

How do I correctly echo checkboxes in a PHP table?

I have a table which is full with data from my database. I am now trying to implement a delete feature on that table, I do have the correct syntax to echo checkboxes but I don't know how to format the line correctly so the corresponding checkbox aligns correctly with each row. Any ideas? (I currently have the line echo'd above the closing table tag. Which makes the boxes appear above the table)

$query = "SELECT * FROM products";
$result = mysqli_query($connection, $query);

if ($result->num_rows > 0)
{
echo "<table><tr><th>ID</th><th>Name</th><th>Description</th><th>Price</th><th>Cost Price</th><th>Stock</th><th>EAN</th><th>Delete Product</th></tr>";

while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["product_id"]
. "</td><td>" . $row["product_name"]
. "</td><td> " . $row["product_description"]
. "</td><td> " . $row["product_price"]
. "</td><td> " . $row["product_cost_price"]
. "</td><td> " . $row["product_stock"]
. "</td><td>" . $row["product_ean"]
. "</td></tr>";
echo '<input name="delete['.$row['product_id'].']" type="checkbox">';
}
echo "</table>";
}
else
{
echo "0 results";
}

Answer

Insert the checkbox in an additional table cell (td). A <tr> element contains one or more <th> or <td> elements. Another content is not treated as part of that row.

echo "<tr><td>" . $row["product_id"]
 . "</td><td>" . $row["product_name"]
 . "</td><td> " . $row["product_description"]
 . "</td><td> " . $row["product_price"]
 . "</td><td> " . $row["product_cost_price"]
 . "</td><td> " . $row["product_stock"]
 . "</td><td>" . $row["product_ean"]
 . "</td><td>" .'<input name="delete['.$row['product_id'].']" type="checkbox">'
 . "</td></tr>";