1

I wrote a PHP code for prepare jQuery function, which can select multipal values from the html multi select box.

there are different campaigns and it have separate question sets, the one campain has many question sets. below code returns the output as

$('#q9 select').val('2'); 
$('#q10 select').val('2'); 
$('#q10 select').val('1');

but i need this output as

$('#q9 select').val(['2']); 
$('#q10 select').val(['2','1']);

to select multipal items, can anyone help please, thank you...

<script>
$(function(){
 <?php 

 foreach ($selectdefaults as $s):
   ?>   
   $('#q<?php echo $s['campsetjoin']['campid'];?> select').val('<?php echo $s['campsetjoin']['setid'];?>');
   <?php  
 endforeach;
 ?> 
});
</script>

3 Answers 3

2

I think you should before change the format of your array:

now you have it like this:

array(
   array('campid'=>9, 'setid' => 2),
   array('campid'=>10, 'setid' => 1),
   array('campid'=>10, 'setid' => 2)
)

You should instead prepare your array like:

array(
   array('campid'=>9, 'setid' => array(2)),
   array('campid'=>10, 'setid' => array(1,2))
)

At that point you just can write:

<script>
$(function(){
 <?php 

foreach ($selectdefaults as $s):
?>   
$('#q<?php echo $s['campsetjoin']['campid'];?> select').val([<?php echo implode("','",$s['campsetjoin']['setid']); ?>]);
<?php  
 endforeach;
 ?> 
});
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

thanks fr the answer and the array is comes from database query, i can't change that, so have any solution within the foreach loop :)
Sure! You can add 1 loop before and put in a new array the data as i told you by grouping entries of database provided array.
1

i agree with the above two answers that either modifying your data structure or using a pure JS solution would be cleaner. however, if neither of them are an option, you could do something like this:

<script>
$(function(){
 <?php 

 // make a hash with the campid as the key
 // and the value be an array of all of the setids
 $campids = array();
 foreach ($selectdefaults as $s):
   $quoted_setid = "'"+$s['campsetjoin']['setid']+"'";
   if ($campids[$s['campsetjoin']['campid']) {
     $campids[$s['campsetjoin']['campid']][] = $quoted_sid;
   }else {
     $campids[$s['campsetjoin']['campid']] = array($quoted_sid);
   }
 endforeach;

 foreach ($campids as $c=>$setids):
   ?>   
   $('#q<?php echo $c;?> select').val('[<?php echo implode(",", $setids);?>]');
   <?php  
 endforeach;
 ?> 
});
</script>

1 Comment

i edited jQuery part for work correctly, here is code $('#q<?php echo $c;?> select').val([ <?php echo "'" . implode("','", $setids) . "'" ?>]);
1

(Link to code/demo at bottom of answer)

I recommend a preparation step between your raw data and your usage thereof. Arrange the data such that each ID represents a data set:

array(2) {
  [9]=>
  array(1) {
    [0]=>
    int(2)
  }
  [10]=>
  array(2) {
    [0]=>
    int(2)
    [1]=>
    int(1)
  }

Then, rather than mix PHP and JS (which leads to a lot of confusing constructs, quoting, etc), JSON serialize your data and inject it into the script.

(function (injected) {
    /*
      pure JS here, using `injected` var instead of PHP data
    */
}(
    <?php echo json_encode($data); ?>

))

Code/Demo: http://codepad.org/BfzOOlPT

2 Comments

i am using cakephp, its difficult to handle with json for this situation
Without meaning to be argumentative, I don't understand how cake makes this less viable. If it's due to the TPL constraints, just assign a TPL variable with the results of the json_encode, and echo that var where I am currently calling json_encode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.