Jump to content

Checkboxes and Complex Array


pbrough

Recommended Posts

I am trying to make a series of 5 check boxes generate a set of values (pricing information).  This would be simple if the variables were a fixed cost but they are not.  If you select one item then the others are reduced, but only when combined.  And different combinations give different results for example 1 & 2 and 4 gives one value but 1 & 2 and 5 will give a different result.  These results don't have to be numeric and it would be easier for me to have them text based anyway.  I would be happy to have hidden div's or other solution to allow the visitor to see the effects that they select dynamically.

 

The combinations required will be:

 

1

2

3

- Result 1

 

1, 2

1, 3

2, 3

- Result 2

 

1, 2 & 4

1, 3 & 4

2, 3 & 4

- Result 3

 

1, 2 & 5

1, 3 & 5

2, 3 & 5

- Result 4

 

1, 2, 4 & 5

1, 3, 4 & 5

2, 3, 4 & 5

- Result 5

 

1, 2, 3

- Result 6

 

1, 2, 3 & 4

- Result 7

 

1, 2, 3 & 5

- Result 8

 

1, 2, 3, 4 & 5

- Result 9

 

4 & 5

- Result 10

 

I hope someone can help.

 

Phil

Link to comment
https://forums.phpfreaks.com/topic/143461-checkboxes-and-complex-array/
Share on other sites

what about something like this:

<script type="text/javascript">
  var options = {
    '1'         : 'Result 1',
    '2'         : 'Result 1',
    '3'         : 'Result 1',
    '1:2'       : 'Result 2',
    '1:3'       : 'Result 2',
    '2:3'       : 'Result 2',
    '1:2:4'     : 'Result 3',
    '1:3:4'     : 'Result 3',
    '2:3:4'     : 'Result 3',
    '1:2:5'     : 'Result 4',
    '1:3:5'     : 'Result 4',
    '2:3:5'     : 'Result 4',
    '1:2:4:5'   : 'Result 5',
    '1:3:4:5'   : 'Result 5',
    '2:3:4:5'   : 'Result 5',
    '1:2:3'     : 'Result 6',
    '1:2:3:4'   : 'Result 7',
    '1:2:3:5'   : 'Result 8',
    '1:2:3:4:5' : 'Result 9',
    '4:5'       : 'Result 10'
  };
  function findResult ( ) {
    var form = document.getElementById('myForm');
    var checked = [];
    for(var n=0;n < form['foobar'].length;n++){
      if(form['foobar'][n].checked){
        checked.push(form['foobar'][n].value);
      }
    }
    var key = checked.join(':');
    var msg = 'Nothing is checked';
    if(key && options[key]){
      msg = options[key];
    }else if(key){
      msg = 'No match found';
    }
    document.getElementById('result').innerHTML = msg;
  }

  window.onload = function(){
    findResult();
  }
</script>
<form id="myForm">
  <input type="checkbox" name="foobar" value="1" onclick="findResult();" /> 1<br />
  <input type="checkbox" name="foobar" value="2" onclick="findResult();" /> 2<br />
  <input type="checkbox" name="foobar" value="3" onclick="findResult();" /> 3<br />
  <input type="checkbox" name="foobar" value="4" onclick="findResult();" /> 4<br />
  <input type="checkbox" name="foobar" value="5" onclick="findResult();" /> 5<br />
  <span id="result"></span>
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.