-1

I have the following:

for(var i = 0; i < triggerCount; i++) {

<?php $me = i; ?>

console.log(<?php echo $me; ?>);

jQuery('[data-key="field_5ee8665e2cd7f"] .acf-input .acf-actions [data-event="add-row"]').trigger('click');

jQuery('#ItineraryTab .acf-row input').eq(i).val("<?php print_r($array[$me]); ?>");
}

Which loops through a triggerCount variable and sets the input to each value in the array. However, if I use print_r($array[0]); then it shows uses this value from the array and places it into every iteration. How can I loop through this $array so similar to the count it places each value in order into each input in order?

8
  • You are mixing PHP and javascript. Where is the php $me defined? how about $iid? Commented Jul 7, 2020 at 0:41
  • Have updated my question - but I am trying to get the $me in the printer array - so shouldn't it return 0 or 1 or 2 etc? Commented Jul 7, 2020 at 0:52
  • Just do var array = <?= json_encode($array) ?>; once and don't mix PHP with JS after it. Commented Jul 7, 2020 at 0:59
  • You can not assign value of i (js) directly to $me (php). PHP executes on server side. Browser will execute javascript after he received all the content from the server meaning meaning php execution has finished already. Commented Jul 7, 2020 at 1:01
  • 1
    Thanks @CarlosCarucce - so how would I need to rewrite this so it can also loop through a counter (for the print_r) Commented Jul 7, 2020 at 1:05

1 Answer 1

-1

I think you should to use array_push() as:

for(var i = 0; i < triggerCount; i++) {

<?php
    $me = i;
    $array = []; 
    array_push($a, $me);
?>

console.log(<?php echo $me; ?>);

jQuery('[data-key="field_5ee8665e2cd7f"] .acf-input .acf-actions [data-event="add-row"]').trigger('click');

jQuery('#ItineraryTab .acf-row input').eq(i).val("<?php print_r($array[$me]); ?>");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.