试图用foreach和switch语句显示数组中的值


Trying to display values from array with foreach and switch statements

所以我试图在wordpress中构建一些自定义帖子类型所需的函数,但在我试图弄清楚如何动态调用变量之前,我需要使此代码工作。我认为我的问题可能是在数组本身或Foreach循环。当我运行代码时,我期望得到高、中、低价格点的结果,但是它只显示了高价格点三次。我做错了什么?

<?php
//variables that will be pulled from multiple post types and fields
//p_high, med and low are product multipliers
$client_lvl =array("High","Med", "Low");
$p_high= 2;
$p_med= 1.5;
$p_low =1.1;
$cogs= 10;
$img_count =3;
$img_cost= 10;
$area= 40;
$m_type=  'Flat';
//Loops through array to display high, medium, and low price
Foreach ($client_lvl as $lvl)
{
If ($lvl ='High'){
$multiplier=$p_high;}
elseif($lvl ='Med')
{$multiplier=$p_med;
$m_high=$p_high;}
else {
$multiplier=$p_low;
$m_high=$p_high;}
//calculator that determines price based on multiplier type (m_type)
switch ($m_type) {
     case 'Area':
         if (isset($m_high)){
            $price_high= $m_high * $area; 
            echo "The High price was '${$price_high}<br/>";
            }
         $price= $multiplier * $area;
         echo "The {$lvl} price is '${$price}<br/>";
         break;
     case 'Image':
     if (isset($m_high)){
            $price_high= $m_high * $img_count *$cogs;
            echo "The High price was '${$price_high}<br/>";
            } 
         $price= $multiplier * $img_count *$cogs;
         echo "The {$lvl} price is '${$price}<br/>";
         break;
     case 'Commission':
     if (isset($m_high)){
            $price_high= $m_high + $cogs;
            echo "The High price was '${$price_high}<br/>";
            } 
         $price= $multiplier + $cogs;
         echo "The {$lvl} price is '${$price}<br/>";
         break;
     case 'Flat':
     if (isset($m_high)){
            $price_high= $m_high;
            echo "The High price was '${$price_high}<br/>";
            } 
         $price= $multiplier;
         echo "The {$lvl} price is '${$price}<br/>";
         break;
     case 'Commission+Image':
     if (isset($m_high)){
            $price_high= $m_high + ($img_cost*$img_count);
            echo "The High price was '${$price_high}<br/>";
            } 
         $price= $multiplier + ($img_cost*$img_count);
         echo "The {$lvl} price is '${$price}<br/>";
         break;
     case'Price':
     if (isset($m_high)){
            $price_high= $m_high * $cogs;
            echo "The High price was '${$price_high}<br/>";
            } 
         $price= $multiplier * $cogs;
         echo "The {$lvl} price is '${$price}<br/>";
         break;
}}
/*End goal is to display the $price variable next to the product depending on if client has the High, medium, or low rate. Rate will be pulled from "client" data, m_type and multipliers will be pulled from "Product group" data, and all other variables will be found in the "product" data. 
If client is not logged in, multiplier uses high value.<br><br>

*/
?>

在if语句中,您必须使用双等号("==")而不是简单的等号来进行比较。

简单的等号用于变量赋值,因此$level将始终等于"high"。

if条件下,您应该检查不分配。试试这个:

<?php
//variables that will be pulled from multiple post types and fields
//p_high, med and low are product multipliers
$client_lvl =array("High","Med", "Low");
$p_high= 2;
$p_med= 1.5;
$p_low =1.1;
$cogs= 10;
$img_count =3;
$img_cost= 10;
$area= 40;
$m_type=  'Flat';
//Loops through array to display high, medium, and low price
Foreach ($client_lvl as $lvl)
{
If ($lvl =='High'){
$multiplier=$p_high;}
elseif($lvl =='Med')
{$multiplier=$p_med;
$m_high=$p_high;}
else {
$multiplier=$p_low;
$m_high=$p_high;}
//calculator that determines price based on multiplier type (m_type)
switch ($m_type) {
     case 'Area':
         if (isset($m_high)){
            $price_high= $m_high * $area; 
            echo "The High price was '${$price_high}<br/>";
            }
         $price= $multiplier * $area;
         echo "The {$lvl} price is '${$price}<br/>";
         break;
     case 'Image':
     if (isset($m_high)){
            $price_high= $m_high * $img_count *$cogs;
            echo "The High price was '${$price_high}<br/>";
            } 
         $price= $multiplier * $img_count *$cogs;
         echo "The {$lvl} price is '${$price}<br/>";
         break;
     case 'Commission':
     if (isset($m_high)){
            $price_high= $m_high + $cogs;
            echo "The High price was '${$price_high}<br/>";
            } 
         $price= $multiplier + $cogs;
         echo "The {$lvl} price is '${$price}<br/>";
         break;
     case 'Flat':
     if (isset($m_high)){
            $price_high= $m_high;
            echo "The High price was '${$price_high}<br/>";
            } 
         $price= $multiplier;
         echo "The {$lvl} price is '${$price}<br/>";
         break;
     case 'Commission+Image':
     if (isset($m_high)){
            $price_high= $m_high + ($img_cost*$img_count);
            echo "The High price was '${$price_high}<br/>";
            } 
         $price= $multiplier + ($img_cost*$img_count);
         echo "The {$lvl} price is '${$price}<br/>";
         break;
     case'Price':
     if (isset($m_high)){
            $price_high= $m_high * $cogs;
            echo "The High price was '${$price_high}<br/>";
            } 
         $price= $multiplier * $cogs;
         echo "The {$lvl} price is '${$price}<br/>";
         break;
}}
/*End goal is to display the $price variable next to the product depending on if client has the High, medium, or low rate. Rate will be pulled from "client" data, m_type and multipliers will be pulled from "Product group" data, and all other variables will be found in the "product" data. 
If client is not logged in, multiplier uses high value.<br><br>

*/
?>

您在if语句中使用了单个等号。单个等号是赋值操作符。这里需要使用比较操作符==

所以在你的代码中,当你想比较变量$lvl与字符串'high'时,你实际上是将值'high'分配给$lvl。这就是为什么你总是得到高值。

你的代码应该是这样的:

foreach ($client_lvl as $lvl) {
    If ($lvl == 'High'){
        $multiplier=$p_high;
    } elseif($lvl =='Med'){
        $multiplier=$p_med;
        $m_high=$p_high;
    } else {
        $multiplier=$p_low;
        $m_high=$p_high;
    }