簡體   English   中英

PHP / MySQL定價路線

[英]PHP/MySQL Pricing routes

我正在嘗試為路線服務管理面板創建價格表。

假設我在數據庫中得到了A,B,C和D點。 這些是從一張表中收集的。 在下一張表格中,我想輸入一個“從”,“到”和“價格”。

在四點示例中,我需要以下輸入價格:
A <-> B
A <-> C
A <-> D
B <-> C
B <-> D
C→D
(該路線的每條可能的航段一條)。

從得到點的結果到生成輸入字段的方式我應該采取什么方法? 我可以僅使用PHP解決此問題,還是需要用JS編寫?

手動輸出這些輸入字段沒有問題,問題在於,由於點數可能從至少兩個點變化為15個點,因此需要將其自動化。

這是一個簡單的函數,它將接收點列表,並返回一個二維數組,其中包含所有點的組合,如您所要求的。

演示

<?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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM