nik nik - 2 months ago 8
PHP Question

Multiple Relations between two or more id's?

i have minimum of two select box in html form to insert through POST but user can add more select boxes.Now i have to show relations between each n every inserted id.

like we have inserted 1,2,3,4 id..so i want it inserted in a table in two column as

(1-2) (1-3) (1-4) (2-3) (2-4) (3-4)


plzz reply to this and tell some idea about this
any help??

enter image description here

enter image description here

Answer

Simply iterate your array and make couples of next elements in same array:

function calculate() {
  var nums = document.getElementById('nums');

  nums = nums
    .value
    .split(',')
    .sort(function(a, b) {
      return parseInt(a) - parseInt(b);
    });

  for (var i = 0; i < nums.length; i++) {
    for (var j = i + 1; j < nums.length; j++) {
      console.log('(' + nums[i] + ' - ' + nums[j] + ')');
    }
  }
}
<input type="text" placeholder="1,2,5,8" id="nums" />
<button onClick="calculate()">Calculate</button>

Comments