2017-04-10 29 views
1

我目前得到了接收表单数据的页面时,我的后两条信息 -通过数组循环基于POST变量 - PHP

$country_name = $_POST[“country_name”]; 
$price_point = $_POST[“price_point”]; 

我现在有一个电子表格,在列的信息

Information Format

我需要能够运行使用后的变量,并发现该数组中的相应值的函数。

我还没有创建一个数组,因为我不知道格式化这个数组的最佳方式。这是我奋斗的地方..

示例输出是;

国家的邮政价值是美国。 价格点为2. 该值为1.5。

对任何能够帮助我下面的人的主要爱!

感谢

+0

阅读PHPexcel阅读和写入Excel表。否则,您将需要使用像mysql这样的数据库(并将phpmyadmin作为MySQL的gui)并查询它是最佳方式。使用phpexcel,您可以将电子表格导入到数组或对象中,使用php进行修改并将其导出。 – Nitin

+0

您需要使用数组的索引进行最快搜索$ arr = array('US'=> [...],'UK'=> [...]);或根据搜索参数动态重新排列阵列 – Nitin

回答

1

我会创建这样的

$dataPoints = array(
    array(
     'point' => 1, 
     'price' => 1.5, 
     'country' => 'UK' 
    ), 
    array(
     'point' => 1, 
     'price' => 1.3, 
     'country' => 'US' 
    ), 
    array(
     'point' => 1, 
     'price' => .8, 
     'country' => 'SWEDEN' 
    ), 
    ... 
) 

阵列而像

function getPrice($pricePoint, $country) { 
    // Search in array for good price 
    foreach ($dataPoints as $dataPoint) { 
     if ($dataPoint['point'] == $pricePoint && $dataPoint['country'] == $country) { 
      return $dataPoint['price']; 
     } 
    } 
    return null; 
} 

一个功能,与你的讯息参数称它为打印

$pp = filter_input(INPUT_POST, 'price_point'); 
$country = filter_input(INPUT_POST, 'country_name'); 
$price = getPrice($pp,$country); 

echo 'Country is ', $country, '. The price point is ', $pp, '. The price is ', $price; 
+1

Camille,这看起来像我之后的解决方案,但是在我将它们传递给getPrice后,如何根据这些点搜索数组? – DIM3NSION

+0

我添加了搜索功能_getPrice_ – Camille

0

alternativel Ÿ我只是把这些值放在一个多维数组中,如下面的示例

一旦这个电子表格变得更大,我会建议从数据库中生成数组。

<?php 
    $country_name = 'UK'; 
    $price_point = 4; 

    $arr = array(
     'UK' => array(
      1 => 1.5, 
      2 => 1.6, 
      3 => 1.9, 
      4 => 12 
     ), 
     'US' => array(
      1 => 1.3, 
      2 => 1.5, 
      3 => 1.6, 
      4 => 1.7 
     ), 
     'SWEDEN' => array(
      1 => 0.8, 
      2 => 0.7, 
      3 => 0.5, 
      4 => 0.3 
     ) 
    ); 

    $value = $arr[$country_name][$price_point]; 
    echo $value; 
    // echoes 12 
?> 
+0

这并不完全回答这个问题。正如问题中所述_“我需要能够运行使用后变量的函数”_您应该编辑答案以包含此类函数 – Shogunivar