douba6365 2017-02-13 19:10 采纳率: 100%
浏览 59
已采纳

PHP / MySQL定价路线

I am trying to create a pricing form for a route service administration panel.

Let's say that i have got the points A, B, C and D in the db. These are collected from one table. In the next table i'd like to put a 'from', 'to' and 'price'.

In the example of four points i would need the following inputs for price:
A<->B
A<->C
A<->D
B<->C
B<->D
C<->D
(one for each possible leg for the route).

What approach should i take from that i got the results of the points to that i generate the input fields? Can i solve this with PHP only or do i need to write it in js?

It's no problem to manually output these input fields, the problem is that it would need to be automated due to the fact that the number of points may vary from at least two points to say like 15 points.

  • 写回答

1条回答 默认 最新

  • doumi0737 2017-02-13 20:18
    关注

    Here is a simple function that will take in a list of points and returns a 2d array with all combinations of the points like you requested.

    (Demo)

    <?php
    
    function calculateRoutes($points){
        //our output array
        $out = array();
    
        //reset the index of our points to ensure they are 0 to N-1
        $points = array_values($points);
    
        //loop over the points once
        for($i=0; $i<count($points) - 1; $i++){
            //loop over the points again, offset by 1
            for($j=$i+1; $j<count($points); $j++){
                //add to our output array the two points
                $out[] = array($points[$i], $points[$j]);
            }
        }
    
        //return the final result
        return $out;
    }
    
    $points = array('A', 'B', 'C', 'D');
    
    $routes = calculateRoutes($points);
    
    print_r($routes);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配