传递变量数组中的变量来计算领域的PHP [英] passing variables within variable array to calculate fields PHP

查看:187
本文介绍了传递变量数组中的变量来计算领域的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我可能会作出这个有点太复杂了我自己,但我真的想摸不着头脑,而且我坚持就如何使它发挥作用。这是一个字preSS站点

所以我试图完成的过程是这样的:


  1. 确定客户端级别来确定哪个价位的客户端都会有。
    选项​​:高,低,中


  2. 根据客户的水平,选择适当的产品乘数在我的计算中使用
    选项​​:p_high,p_medium或P_low


  3. 运行,其计算根据所选择的乘数类型的最终价格的公式


  4. 如果客户端水平不高,价格会显示是/现在的价位,所以无论是p_high和p_low值将被使用。


我曾尝试创建设置一个数组中的foreach循环来计算价格1,和price2运行的函数。不过,虽然我没有测试过它,我是pretty确保这不会实现我追求的目标。

我会以为我也许能增加变量名创建一个价格1和价格2变量,但我不能完全肯定的最好办法做到这一点,或者如果它合适的解决方案。如果有人可以帮助我,那将是巨大的。

请注意:switch语句不包括为present与pviously定义关联数组$ P $变量。 echo语句present将与函数一次我想出如何正确传递变量替换。

 函数get_client_lvl()
{$ USER_ID = get_current_user_id();如果($ user_ID的== 0)
  {返回高;}
其他
  {get_user_meta($ user_ID的,WPCF-客户LVL,真正的); }
}$ client_lvl = get_client_lvl();功能display_right_price($ client_lvl,$乘数)
{如果($ client_lvl ==高)
  {回声\\ $ {$}价格1;}其他{
回声为:\\ $ {$价格1}< BR />中;
回声现在:\\ $ {$ price2};
}}$ p_high = 2; // get_post_meta($ POST_ID,$关键
$ p_med = 1.5;
$ p_low = 1.1;
$ COGS = 10;
$ img_count = 3;
$ img_cost = 10;
$面积= 40;
$ m_type ='区';
如果($ client_lvl =='配有')
{$ =价格阵列($ p_high,$ p_med);}ELSEIF($ client_lvl =='低')
{$ =价格阵列($ p_high,$ p_low);}
其他
{$ =价格阵列($ p_high);}    的foreach($价格为$乘数)
    {
开关($ m_type){     案区:
         $价格= $乘数* $区;
            回声的{$拉特}价格是\\ $ {$价}&所述峰; br />中; />中; //这将display_right_price()函数来代替一旦价格变量建立
         打破;     案图像:         $价格= $乘数* $ img_count * $齿轮;
         回声的{$拉特}价格是\\ $ {$价}&所述峰; br />中; />中; //这将display_right_price()函数来代替一旦价格变量建立
         打破;     案委员会:         $价格= $乘数+ $齿轮;
         回声的{$拉特}价格是\\ $ {$价}&所述峰; br />中; />中; //这将display_right_price()函数来代替一旦价格变量建立
         打破;     案平:         $价格= $乘法器;
         回声的{$}拉特价格是\\ $ {$价格}< BR />中; />中; //这将display_right_price被替换()一次的价格变量函数的建立
         打破;     案委员会+图像:         $价格= $乘数+($ img_cost * $ img_count);
         回声的{$拉特}价格是\\ $ {$价}&所述峰; br />中; />中; //这将display_right_price()函数来代替一旦价格变量建立
         打破;     案价格:         $价格= $乘数* $齿轮;
         回声的{$拉特}价格是\\ $ {$价}&所述峰; br />中; />中; //这将display_right_price()函数来代替一旦价格变量建立
         打破;
}}

编辑:我想它(现在只需要弄清楚如何从两个不同类型的后拉动态变量:

 函数get_client_lvl()
{$ USER_ID = get_current_user_id();
如果($ user_ID的== 0)
{返回'高';}
其他
{get_user_meta($ user_ID的,WPCF-客户LVL,真正的); }
}// $ client_lvl = get_client_lvl();
$ =帖子ID get_the_ID();
$ client_lvl =高;功能display_right_price($ client_lvl)
{
$ p_high = 3.2; // get_post_meta($ POST_ID,$键)
$ p_med = 1.3;
$ p_low = 1.1;
$ COGS = 11.53;
$ img_count = 3;
$ img_cost = 10;
$面积= 40;
$ m_type ='价格';
$价格=阵列();如果($ client_lvl =='配有')
{$ =价格阵列(高=> $ p_high,配有'=> $ p_med);}ELSEIF($ client_lvl =='低')
{$ =价格阵列(高=> $ p_high,低= GT; $ p_low);}
其他
{$ =价格阵列(高=> $ p_high);}    的foreach($价格为$乘数)
    {
开关($ m_type){     案区:
         $总[] = $乘数* $区;
                       打破;     案图像:         $总[] = $乘数* $ img_count * $齿轮;         打破;     案委员会:         $总[] = $乘数+ $齿轮;         打破;     案平:         $总[] = $乘法器;         打破;     案委员会+图像:         $总[] = $乘数+($ img_cost * $ img_count);         打破;     案价格:         $总[] = $乘数* $齿轮;         打破;
}}
$ P1 = number_format($总[0],2,...,',''。');
如果($ client_lvl ==高)
{回声\\ $ {$ P1};
}
其他{
$ P2 = number_format($总[1],2,...,',''。');
回声为:\\ $ {$ P1}< BR />中;
回声NOW:\\ $ {$ P2};
}}display_right_price($ client_lvl);


解决方案

我认为你需要考虑什么是的变量范围。使用全球允许之外的功能设置变量是内可见。例如:

 函数display_right_price($ client_lvl,$乘数){    //允许变量$ p_high,$ p_med,$ p_low是在函数内部可见
    全球$ p_high,$ p_med,$ p_low;    // ...}

但也许,把事情简单化,你会通过$ client_lvl和产品ID您display_right_price()函数,然后执行所有的计算,并从函数内部打印的价格呢?

Alright, I may be making this a bit too complicated for myself, but I am really trying to figure this out, and I am stuck on how to make it work. This is for a wordpress site

So the process I am trying to accomplish is this:

  1. Determine client level to determine which price point the client will have. Options: High, low, medium

  2. based on client level, select the appropriate product multiplier to use in my calculations options: p_high, p_medium, or P_low

  3. Run an equation that calculates the final price depending on the multiplier type selected.

  4. If client level is not high, the price will display a was/ now price point, so both the p_high, and p_low values will have to be used.

I have tried creating a function that sets an array to run in a foreach loop to calculate the price1, and price2. But although I haven't tested it yet, I am pretty sure this is not going accomplish the goal I am seeking.

I'm going assume that I may be able to increment the variable name to create a price1 and price 2 variable, but I'm not entirely sure the best way to do that, or if its the right solution. If anyone could help me out, that would be great.

Note: The switch statement does include variables that were present with a previously defined associative array. The echo statement present will be replaced with the function once i figure out how to pass the variables properly.

function get_client_lvl ()
{$user_id = get_current_user_id();

If ($user_id == 0)
  {return "high";}
Else
  {get_user_meta($user_id, wpcf-client-lvl, true); }
}

$client_lvl=get_client_lvl();

function display_right_price($client_lvl, $multiplier)
{If ($client_lvl == "high")
  {echo "\${$price1}";}

else {
echo "WAS:\${$price1}<br/>";
echo "Now: \${$price2}";
}}

$p_high= 2; //get_post_meta($post_id,$key
$p_med= 1.5;
$p_low =1.1;
$cogs= 10;
$img_count =3;
$img_cost= 10;
$area= 40;
$m_type=  'Area';


if ($client_lvl == 'med')
{$prices=array($p_high, $p_med);}

elseif ($client_lvl == 'low')
{$prices=array($p_high, $p_low);}
else
{$prices=array($p_high);}

    Foreach($prices as $multiplier)
    {
switch ($m_type) {

     case 'Area':
         $price= $multiplier * $area;
            echo "The {$lvl} price is \${$price}<br/>";/>";  //This will be replaced with display_right_price() function once the price variables are established
         break;

     case 'Image':

         $price= $multiplier * $img_count *$cogs;
         echo "The {$lvl} price is \${$price}<br/>";/>";  //This will be replaced with display_right_price() function once the price variables are established
         break;

     case 'Commission':

         $price= $multiplier + $cogs;
         echo "The {$lvl} price is \${$price}<br/>";/>";  //This will be replaced with display_right_price() function once the price variables are established
         break;

     case 'Flat':

         $price= $multiplier;
         echo "The {$lvl} price is \${$price}<br/>"; />";  //This will be replaced with display_right_price() function once the price variables are established
         break;

     case 'Commission+Image':

         $price= $multiplier + ($img_cost*$img_count);
         echo "The {$lvl} price is \${$price}<br/>";/>";  //This will be replaced with display_right_price() function once the price variables are established
         break;

     case 'Price':

         $price= $multiplier * $cogs;
         echo "The {$lvl} price is \${$price}<br/>";/>";  //This will be replaced with display_right_price() function once the price variables are established
         break;
}}

EDIT: I figured it out (Now just need to figure out how to pull the variables dynamically from two different post types:

function get_client_lvl ()
{$user_id = get_current_user_id();
If ($user_id == 0)
{return 'high';}
Else
{get_user_meta($user_id, wpcf-client-lvl, true); }
}

//$client_lvl=get_client_lvl();


$postid = get_the_ID();
$client_lvl='high';





function display_right_price($client_lvl)
{
$p_high= 3.2; //get_post_meta($post_id,$key)
$p_med= 1.3;
$p_low =1.1;
$cogs= 11.53;
$img_count =3;
$img_cost= 10;
$area= 40;
$m_type= 'Price';
$price=array();

if ($client_lvl == 'med')
{$prices=array('high'=>$p_high,'med'=> $p_med);}

elseif ($client_lvl == 'low')
{$prices=array('high'=>$p_high,'low'=> $p_low);}
else
{$prices=array('high'=>$p_high);}

    Foreach($prices as $multiplier)
    {
switch ($m_type) {

     case 'Area':
         $total[]= $multiplier * $area;
                       break;

     case 'Image':

         $total[]= $multiplier * $img_count *$cogs;

         break;

     case 'Commission':

         $total[]= $multiplier + $cogs;

         break;

     case 'Flat':

         $total[]= $multiplier;

         break;

     case 'Commission+Image':

         $total[]= $multiplier + ($img_cost*$img_count);

         break;

     case 'Price':

         $total[]= $multiplier * $cogs;

         break;
}}
$p1=number_format($total[0],2,'.',',');
If ($client_lvl == "high")
{

echo "\${$p1}";
}
else {
$p2=number_format($total[1],2,'.',',');
echo "WAS: \${$p1}<br/>";
echo "NOW: \${$p2}";
}}

display_right_price($client_lvl);

解决方案

I think what you need to look into is variable scope. Using "global" allows variables set outside the function to be visible within. For example:

function display_right_price($client_lvl, $multiplier) {

    // allow variables $p_high, $p_med, $p_low to be visible inside the function
    global $p_high, $p_med, $p_low;

    // ...

}

But maybe, to simplify things, you would pass $client_lvl and the product ID to your display_right_price() function, then perform all the calculations and print the prices from inside the function?

这篇关于传递变量数组中的变量来计算领域的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆