【问题标题】:Output array object value if multiple values from different objects of the same group of array are selected如果选择了来自同一组数组的不同对象的多个值,则输出数组对象值
【发布时间】:2017-03-28 02:18:05
【问题描述】:

我正在为要求 3 个输入的表单创建 AJAX 功能 - 床、浴室、频率。我希望输出是价格。所以用户在输入的时候,只会输出我创建的Array对应的价格。

PHP 代码片段(数组):

 $pricing = array (
    array ('frequency' => "one", 'beds' => 1 , 'baths' => 1 , 'price' => 90),
    array ('frequency' => "one", 'beds' => 1 , 'baths' => 1.5 , 'price' => 113),
    array ('frequency' => "one", 'beds' => 1 , 'baths' => 2 , 'price' => 113),
    array ('frequency' => "one", 'beds' => 2 , 'baths' => 2.5 , 'price' => 135),
    array ('frequency' => "weekly", 'beds' => 3 , 'baths' => 3 , 'price' => 135),
    array ('frequency' => "weekly", 'beds' => 3 , 'baths' => 3.5 , 'price' => 158),
    array ('frequency' => "biweekly", 'beds' => 4 , 'baths' => 4 , 'price' => 158),
    array ('frequency' => "biweekly", 'beds' => 4 , 'baths' => 4.5 , 'price' => 180),
    array ('frequency' => "monthly", 'beds' => 5 , 'baths' => 5 , 'price' => 180),
    array ('frequency' => "monthly", 'beds' => 5 , 'baths' => 5.5 , 'price' => 203),
    array ('frequency' => "monthly", 'beds' => 6 , 'baths' => 6 , 'price' => 203)
);

我设法传递了输入的值,但我的函数不起作用。显然我可以回显(回显 $selected_frequency_id;)从我的表单中选择的值,但我不能回显价格。我的功能不正确吗?我找不到 foreach 循环有什么问题。请看下面的代码:

PHP 代码继续:

function ajax_update_price() {

$selected_bed_id = $_POST['bedID'];
$selected_bath_id = $_POST['bathID'];
$selected_frequency_id = $_POST['frequencyID'];

//echo $selected_frequency_id;

    foreach( $pricing as $element ) {
        if( $element['frequency'] == $selected_frequency_id && $element['beds'] == $selected_bed_id && $element['baths'] == $selected_bath_id) {
             echo $element['price'];
             break;
    }
 } 

    wp_die(); 
}

【问题讨论】:

  • 这和 JavaScript 有什么关系?

标签: php arrays ajax wordpress


【解决方案1】:
function ajax_update_price() {
  global $_POST;


$selected_bed_id = $_POST['bedID'];
$selected_bath_id = $_POST['bathID'];
$selected_frequency_id = $_POST['frequencyID'];


foreach( $pricing as $element ) {
    if( ($element['frequency'] == $selected_frequency_id) && ($element['beds'] == $selected_bed_id) && ($element['baths'] == $selected_bath_id)) {
         echo $element['price'];
         break;
   }
  } 

wp_die(); 
}

但我有更漂亮的解决方案:

 $pricing["one"]["1"]["1"] = 90;
 $pricing["one"]["1"]["1.5"] = 113;
 $pricing["one"]["1"]["2"] = 113;
 $pricing["one"]["2"]["2.5"] = 135;
 $pricing["one"]["2"]["2.5"] = 135;
 $pricing["weekly"]["3"]["3"] = 135;
 $pricing["weekly"]["3"]["3.5"] = 158;
// and so on, i think you understand how to continue

新功能将是:

function ajax_update_price() {
  global $_POST;


$selected_bed_id = $_POST['bedID'];
$selected_bath_id = $_POST['bathID'];
$selected_frequency_id = $_POST['frequencyID'];

if ($pricing[$selected_frequency_id][$selected_bed_id][$selected_bath_id])
    print $pricing[$selected_frequency_id][$selected_bed_id][$selected_bath_id]

  wp_die(); 
}

【讨论】:

  • 谢谢!会试试这个!
  • 对不起,新手。我完全复制了您的第一个建议,但它仍然不起作用。我还复制了您的第二个建议并删除了我的原始数组,但仍然无法正常工作。
猜你喜欢
  • 2023-01-12
  • 2023-02-03
  • 2021-11-28
  • 2020-01-10
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多