如何从用户选择的值生成复选框
How to generate a checkbox from a user-selected value
我有一个应用程序,在第一页上向用户显示他可以选择的四种产品。在他保存数据后,我必须显示所有四种产品的列表,并在用户选择的产品旁边放置一个 "check"。我的逻辑有问题 - 谁能帮忙?
// user can choose from these products
$products = array(
'1'=>'Product One',
'2'=>'Product Two',
'3'=>'Product Three',
'4'=>'Product Four' );
// user has choosen these products
$selected_products = explode(',', 'Product One,Product Four');
foreach($selected_products as $product)
{
// Have to print out all products from $products variable, and check
// the ones that the user selected from the $selected_products string.
}
您可以使用 in_array()
来检查它是否是所选值中的选中选项之一。粗略示例:
$products = array('1'=>'Product One','2'=>'Product Two','3'=>'Product Three','4'=>'Product Four');
// user has choosen these products
$selected_products = explode(',', 'Product One,Product Four');
foreach($products as $id => $p) {
$checked = (in_array($p, $selected_products)) ? 'checked' : '';
echo "<input type='checkbox' name='check' value='{$id}' $checked /> {$p}<br/>";
}
请确保所选产品作为 ID 而不是产品名称返回。然后你可以使用 in_array()
来确定一个复选框是否应该被选中。
<?php
$products = array(1=>'Product One', 2=>'Product Two', 3=>'Product Three', 4=>'Product Four');
$selectedProducts = array(1,4);
foreach($products as $key => $value) {
$checked = in_array($key, $selectedProducts) ? ' checked' : '';
echo '<input type="checkbox" id="product'.$key.'" value="'.$key.'"'.$checked.' /> <label for="product'.$key.'">'.$value.'</label>';
}
$products = array('1'=>'Product One','2'=>'Product Two','3'=>'Product Three','4'=>'Product Four');
// user has choosen these products
$selected_products = explode(',', 'Product One,Product Four');
foreach($products as $id => $p) {
$checked = (in_array($p, $selected_products)) ? 'checked' : '';
echo "<input type='checkbox' name='check' value='{$id}' $checked /> {$p}<br/>";
}
我有一个应用程序,在第一页上向用户显示他可以选择的四种产品。在他保存数据后,我必须显示所有四种产品的列表,并在用户选择的产品旁边放置一个 "check"。我的逻辑有问题 - 谁能帮忙?
// user can choose from these products
$products = array(
'1'=>'Product One',
'2'=>'Product Two',
'3'=>'Product Three',
'4'=>'Product Four' );
// user has choosen these products
$selected_products = explode(',', 'Product One,Product Four');
foreach($selected_products as $product)
{
// Have to print out all products from $products variable, and check
// the ones that the user selected from the $selected_products string.
}
您可以使用 in_array()
来检查它是否是所选值中的选中选项之一。粗略示例:
$products = array('1'=>'Product One','2'=>'Product Two','3'=>'Product Three','4'=>'Product Four');
// user has choosen these products
$selected_products = explode(',', 'Product One,Product Four');
foreach($products as $id => $p) {
$checked = (in_array($p, $selected_products)) ? 'checked' : '';
echo "<input type='checkbox' name='check' value='{$id}' $checked /> {$p}<br/>";
}
请确保所选产品作为 ID 而不是产品名称返回。然后你可以使用 in_array()
来确定一个复选框是否应该被选中。
<?php
$products = array(1=>'Product One', 2=>'Product Two', 3=>'Product Three', 4=>'Product Four');
$selectedProducts = array(1,4);
foreach($products as $key => $value) {
$checked = in_array($key, $selectedProducts) ? ' checked' : '';
echo '<input type="checkbox" id="product'.$key.'" value="'.$key.'"'.$checked.' /> <label for="product'.$key.'">'.$value.'</label>';
}
$products = array('1'=>'Product One','2'=>'Product Two','3'=>'Product Three','4'=>'Product Four');
// user has choosen these products
$selected_products = explode(',', 'Product One,Product Four');
foreach($products as $id => $p) {
$checked = (in_array($p, $selected_products)) ? 'checked' : '';
echo "<input type='checkbox' name='check' value='{$id}' $checked /> {$p}<br/>";
}