Javascript 通过从PHP传入数组,在java脚本中填充项目数组及其值
如果您的PHP文件中有JavaScript,只需回显PHP变量即可。例如,如果您的PHP数组是$array:Javascript 通过从PHP传入数组,在java脚本中填充项目数组及其值,javascript,php,html,Javascript,Php,Html,如果您的PHP文件中有JavaScript,只需回显PHP变量即可。例如,如果您的PHP数组是$array: for ($i = 0; $i < sizeof($vodAssetArray); $i++) { populate each of the items in the java script like { 'title' : $vodAssetArray[i]->title,
for ($i = 0; $i < sizeof($vodAssetArray); $i++)
{
populate each of the items in the java script like
{
'title' : $vodAssetArray[i]->title,
'description' : $vodAssetArray[i]->description,
'thumbnail' : [$vodAssetArray[i]->imgSrc],
'large' : [$vodAssetArray[i]->imgSrc],
'button_list' :
[
{ 'title':'Demo', 'url' : 'http://mypd.front.net/', 'new_window' : true }
],
'tags' : [$vodAssetArray[i]->genre]
},
}
?>
“标题”:“,
示例
'title' : "<?php echo $array['title']; ?>",
$(函数(){
$(“#演示”).grid({
“流派”:“全部”,
“项目”:
[
]
});
});
这里有很多方法如果这已经出现在PHP页面上,为什么不在它们的给定位置呼出您需要的适当PHP?您是指&array[i]->title吗?这只是一个通用示例。如果您将PHP数组代码添加到您的问题中,我可以专门针对您的情况告诉您。是否可以使用PHP对整个脚本部分进行回显。因为这个代码部分将在@sen中,是的,但它可能看起来很混乱
。有人能帮我用for循环回显这个javascript来创建项目数组吗?
'title' : "<?php echo $array['title']; ?>",
<?php
$vodAssetArray = array(
array(
'title' =>'title 1',
'description' =>'description 1',
'thumbnail' =>'thumbnail imgSrc 1',
'large' =>'imgSrc 1',
'genre' =>'Tags 1',
),
array(
'title' =>'title 2',
'description' =>'description 2',
'thumbnail' =>'thumbnail imgSrc 2',
'large' =>'imgSrc 2',
'genre' =>'Tags 2',
)
);
$javascript= array();
$count = sizeof($vodAssetArray);
for($i = 0; $i < $count; $i++)
{
$javascript[]= "
{
'title' : ".$vodAssetArray[$i]['title'].",
'description' : ".$vodAssetArray[$i]['description'].",
'thumbnail' : [".$vodAssetArray[$i]['thumbnail']."],
'large' : [".$vodAssetArray[$i]['large']."],
'button_list' :
[
{ 'title':'Demo', 'url' : 'http://mypd.front.net/', 'new_window' : true }
],
'tags' : [".$vodAssetArray[$i]['genre']."]
}
";
}
?>
<script type="text/javascript">
$(function(){
$("#demo").grid({
'genre' : 'All',
'items' :
[
<?php echo implode(',', $javascript); ?>
]
});
});
</script>