开发者

PHP Loop with JQUERY

开发者 https://www.devze.com 2023-04-12 21:00 出处:网络
I am wanting to run a开发者_Go百科 jquery.append function on some divs.Basically, I am calling information to the database, and the database knows which DIV id the content belongs to, so I want the DI

I am wanting to run a开发者_Go百科 jquery.append function on some divs. Basically, I am calling information to the database, and the database knows which DIV id the content belongs to, so I want the DIV id in the .append function to be different for each value.

I guess I want to know how I can run a loop on the Jquery portion. Everything else I am doing is working great!


Lets say your each id is stored into an array in PHP, i.e.

<?php
$all_ids = Array(
  '1' => 'ID 1 value',
  '2' => 'ID 2 value',
  '3' => 'ID 3 value'
);
?>

You can use json_encode to translate the array to a javascript object.

<script>
var iterateMe = <?=json_encode($all_ids)?>;
$.each(iterateMe,function(index, value) {
  // This is where you would run each append
  alert(index + ": " + value);
});
</script>

Then, iterate as normal!

0

精彩评论

暂无评论...
验证码 换一张
取 消