I run into a little problem that needs your help. I am coding in a Joomla module, but this is not really a problem to all those who are not Joomla-friendly.
In a Joomla’s module, there will be a folder named tmpl (template for short I think) that contains a little file default.php, which is where the view will be presented to the browser. Here is roughly a part of the file I am dealing with
!DOCTYPE HTML
<?php
// perform some initialization
?>
<script language="javascript">
$(document).ready(function()
{
var obj=["o1","o2","o3","o4",...,"o_n"];
//do something with range obj
});
</script>
<div>
<div class="content">
<?php
if(is_set($array))
{
foreach($array as $key=>$val):
?>
//GET $val->id
<?php
//display $val->$content
}
?>
</div>
</div>
In the foreach loop I would like to get all the integral ids inside $val which will be fed back into the above javascript’s obj variable as strings. I would need this to be done automatically; when the page loaded, the obj in javascript is initialized and when I added a new $val item into the database, this must too be done automatically (I don’t have to open this default page to edit the obj).
Do you understand the problem I am having right now, please ask and reply. Thank you
One solution would be to build the array you want to pass to JS in PHP, then
json_encodeit and pass it as is to JS. It would be easier than to build it in JS, then afterwards extend it via generated JS code. See this for example:Another way would be to use AJAX calls to fill this array, but that’s not always the best approach, so I’ll leave it up to you to decide.