我有5个复选框,用户可以从中选择一个或多个选项。然后,选定的选项将在数据库中更新。然后,用户的选择将在另一页上显示/查看。但是我的问题是,在PHP中执行foreach循环时,我想显示更新的选择以及未选择的选择。
这是5个复选框
<input type="checkbox" name="interest[]" value="fishing">Fishing
<input type="checkbox" name="interest[]" value="camping">Camping
<input type="checkbox" name="interest[]" value="hiking">Hiking
<input type="checkbox" name="interest[]" value="swimming">Swimming
<input type="checkbox" name="interest[]" value="running">Running
<br><br>
<input type="submit" name="submit" value="Submit">
在这里更新代码
if (isset($_POST["submit"])) {
$interestArr = $_POST['interest'];
$interest = new Interest();
$newArr = implode(',', $interestArr);
$interest->updateInterests($id=19, $newArr);
}
这里显示的代码
<?php
$interest = new Interest();
$interests = $interest->showInterests($userid=19)->interests;
$newArr = explode(',', $interests);
foreach ($newArr as $data) {
echo '<input type="checkbox" name="interest[]" value="'.$data .'" checked>'.$data;
}
更新选项像这样存储在数据库的兴趣列下 钓鱼,露营,跑步
foreach循环将其选中的复选框显示为正确的相应标签。
如何显示其他未选中的复选框,以便用户可能要进行更改?
谢谢。
答案 0 :(得分:0)
假设您有一个选择列表数组,例如:
$choices = ["Fishing", "Camping" , "Hiking","Swimming" , "Running" ]
用户选择他们的选择后
$interests = ["Fishing" , "Running" ];
在您的情况下,最重要的一行是:
$interest = new Interest();
$interests = $interest->showInterests($userid=19)->interests;
$newArr = explode(',', $interests);
在示例中,假设 $ newArr 等于 $ interests 。
foreach ($interests as $interest) {
echo 'you have choose '.$interest.PHP_EOL;
}
结果:
you have choose Fishing
you have choose Running
对于未选中:
$notInterests = array_diff($choices,$interests);
foreach ($notInterests as $notInterest) {
echo 'you have not choose '.$notInterest.PHP_EOL;
}
结果:
you have not choose Camping
you have not choose Hiking
you have not choose Swimming
要在一个循环中处理它:
foreach ($choices as $choice) {
if(in_array($choice,$interests )){
echo'you have choose '.$choice.PHP_EOL ;
}else{
echo 'you have not choose '.$choice.PHP_EOL;
}
}
希望这对您有所帮助。
答案 1 :(得分:0)
这是一个简单的例子来说明主要思想。该代码旨在在单个脚本中运行。
主要思想是:
index.php
<?php
// Keep this outside of the if statement so the form has access to it.
$availableInterests = [
'fishing',
'camping',
'hiking',
'swimming',
'running',
];
// Keep this outside of the if statement so the form has access to it.
$selectedInterests = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Cast the posted interests to an array in case the user submitted an empty list.
// An empty list would be NULL if we didn't cast it.
$selectedInterests = (array)$_POST['interest'];
// foreach $selectedInterests insert into DB...
// Let this code `fall through` to render the form again.
// $selectedInterests is now populated and can be used in to the form below to keep the selected checkboxes checked.
}
?>
<form method="post">
<!-- Using the `global` $availableInterests array to drive our form. -->
<? foreach ($availableInterests as $interest): ?>
<!-- Do we want to render the checkbox as checked? -->
<?php $checked = (in_array($interest, $selectedInterests)) ? ' checked' : ''; ?>
<input type="checkbox"
name="interest[]"
value="<?php echo $interest; ?>"
<?php echo $checked; ?>>
<?php echo ucfirst($interest); ?>
<?php endforeach; ?>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
答案 2 :(得分:0)
为帮助其他可能处于类似情况的人,我想至少根据穆罕默德·雅西·查伯利(Mohammed Yassine CHABLI)的灵感,发布对我来说至少是可行的解决方案。最早发表评论的Vantiya确实帮助我理解了接下来的事情。谢谢大家。
<?php
$interest = new Interest();
$interests = $interest->showInterests($userid=19)->interests;
$choices = $interest->showInterests($userid=19)->choices;
$selected = explode(',', $interests);
$choices = explode(',', $choices);
foreach ($choices as $choice) {
if(in_array($choice,$selected )){
echo '<input type="checkbox" name="interest[]" value="'.$choice .'"
checked>'.$choice;
}else{
echo '<input type="checkbox" name="interest[]" value="'.$choice .'"
unchecked>'.$choice;
}
}