简体   繁体   中英

Format jQuery function with PHP foreach loop

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>

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>

(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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM