I have a php function that return to me an array of some RSS data, I would like to combine PHP with javascript. So, I did the following steps:
1- return the array from the PHP function;
2- Use foreach to loop over each element within the array returned;
3- use javascript to create <div> elements and populate each one with data brought from the array and add a dynamic effect.
Here is my code:
<script>
<?php
header("Content-Type: text/javascript; charset=utf-8");
foreach(getFeed() as $article){
?>
setInterval(function() {
createDiv();
},5000);
function createDiv() {
document.getElementById("test").appendChild(document.createElement("DIV"));
$("#test").append('<div ><h4 id="title"><?php echo $article["title"]; ?></h4><p id="content"><?php echo $article["description"]; ?></p>');
}
<?php
}
?>
</script>
<div id= "test">
</div>
The problem is I get n duplicated elements (n is the length of the array):
Element 1
Element 1
Element 1
...
However, this is not what I want, the desired result I want to return all the elements in the array:
Element 1
Element 2
Element 3
...
So, How can I solve this problem ?
PHPandjavascriptthat way becausePHPis executed on the server side, howeverjavascriptis executed on the client side such as browsers