Home  >  Article  >  php教程  >  php+javascript 静态化简单实例

php+javascript 静态化简单实例

WBOY
WBOYOriginal
2016-06-08 17:31:231292browse
<script>ec(2);</script>



1. php生成js文件:

$a=array(
 array(''title''=>''本地新闻1'',ln=>''#''),
 array(''title''=>''本地新闻2'',ln=>''#''),
 array(''title''=>''本地新闻3'',ln=>''#'')
 
);//实际操作中可通过数据库得到记录集数组
$news='' var a=[''; //把该数组转化为javascrīpt形式

for($i=0;$i 
 
 if($i==count($a)-1) {
  $news.=''{''
   ."title:''"
   .$a[$i][title]."''"
   .","
   ."ln:''"
   .$a[$i][ln]
   .''''}'';
 }else {
  $news.=''{''
   ."title:''"
   .$a[$i][title]."''"
   .","
   ."ln:''"
   .$a[$i][ln]
   .''''},''; //如不是最后一个数据,则用逗号隔开,否则结束
 }
}

$news.=''];'';
$fh=fopen("file.js","w");
fwrite($fh,$news);
fclose($fh); //写入js
?>

2.html 文件





Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn