PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a form on a PHP page which generates a list of products with their descriptions from a database. Beside each product, a checkbox is generated dynamically:
Whenever a checkbox is selected and the form is submitted, the new page should display the value of the checkbox selected. I have no problem displaying a checkbox value when only one checkbox has been selected but how do i display multiple selections on the new page?
<?php
$query1 = "SELECT * FROM tblCoffee WHERE coffeeID = '{$_POST['checkbox']}' order by coffeeName";
$result1 = @mysql_query ($query1);
while ($row1 = mysql_fetch_array($result1)){
?>
foreach($_POST['checkbox'] as $val)
{
echo $val;
echo " is my favorite value.";
}
It's also just considered better practice to put the curly braces there (leaving them out with the one-liners can sometimes lead to bugs when you add more lines and forget the braces)
if (!empty($_POST['checkbox'] && count($_POST['checkbox']))
{
$query1 = 'SELECT * FROM tblCoffee WHERE coffeeID IN ('. implode(',', $_POST['checkbox']).') order by coffeeName';
//outputs something like
//SELECT * FROM tblCoffee WHERE coffeeID IN (1, 4, 6, 10) order by coffeeName
I don't think it's fair to say it's 'better practice' to include them.
for writing and reading code, it's easier if only one operation needs to take place to not include them. When reading code, it's an immediate identifier that only the next line matters, which simplifies understanding. When writing code, the same rule applies, plus you don't have to hit the shift key on your keyboard twice