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