我有一个复选框格式的项目数组(cheese),如下所示
array(
'type' => 'checkbox',
"holder" => "div",
"class" => "",
"heading" => __("Choose your cheese topping", 'rbm_menu_item'),
'param_name' => 'cheesbox',
'value' => array( 'Cheddar'=>'Chedder', 'Gouda'=>' Gouda', 'Bleu'=>' Bleu'),
"description" => __("<br /><hr class='gduo'>", 'rbm_menu_item')
),
数组的值显示在页面上(在一个隐藏的div中),使用heredoc声明使用if来检查复选框是否被使用-如果返回为空,则div不显示-如果其中一个复选框被"选中",则它被选中。
///Cheese selections
if (!empty($cheesbox)) {
$output .= <<< OUTPUT
<br />
<div class="sides">Comes with: <p>{$cheesebox}</p></div>
<br />
OUTPUT4;
}
我需要做的是拉出数组中的任何一个值,如果它在$cheesebox中,就做一些事情。
我试过像这样使用ifelse
if ( ('$cheesebox') == "Cheddar" ){
echo "Your topping is Cheddar";
}
elseif ( ('$cheesebox') == "Gouda" ){
{
echo "Your topping is Gouda";
}
elseif ( ('$cheesebox') == "Bleu" ){
{
echo "Your topping is Bleu";
}
然而,这不起作用-我敢肯定我有错的地方沿着线或herdoc函数只允许一个?
如果有,有办法做到这一点吗?
PHP中的单引号表示不解析变量的文本字符串。试试这个:
if ($cheesebox == "Cheddar") {
echo "Your topping is Cheddar";
} else if ($cheesebox == "Gouda") {
echo "Your topping is Gouda";
} else if ($cheesebox == "Bleu") {
echo "Your topping is Bleu";
}
更好:
if (in_array($cheesebox, ['Cheddar', 'Gouda', 'Bleu'])) {
echo "Your topping is {$cheesebox}";
}
编辑:回应您在评论中的进一步要求:
在你的PHP中:
$cheeses = ['Cheddar', 'Gouda', 'Bleu'];
$cheeseBoxes = '';
foreach ($cheeses as $cheese) {
$cheeseClass = $cheesebox == $cheese ? '' : 'cheese-hidden';
$cheeseBoxes .= <<<CHEESE
<div class="cheese {$cheeseClass}">
<p>Cheese: {$cheese}</p>
<img src="/images/cheeses/{$cheese}.png" alt="A picture of some {$cheese}" />
</div>
CHEESE;
}
// Then, wherever you need it, or just use $cheeseBoxes in your chosen heredoc:
echo $cheeseBoxes;
在你的CSS中,隐藏不活动的:
.cheese.cheese-hidden {
display: none;
}