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 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测