Php+javascript A simple example of static

Source: Internet
Author: User

1. PHP Generated JS file:

<?php
A=array (
Array (' title ' => ' local news 1′,ln=> ' # '),
Array (' title ' => ' local news 2′,ln=> ' # '),
Array (' title ' => ' local news 3′,ln=> ' # ')

//actual operation can get the recordset array through the database
News= ' var a=[';//convert the array into a javascrīpt form

For (I=0;i<count (a); 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]
.’ \'},’; If not the last data, separated by commas, or end
}
}

News.= ']; ';
Fh=fopen ("File.js", "w");
Fwrite (fh,news);
Fclose (FH); Write JS
?>

2.html file

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; Charset=gb2312″/>
<title> Untitled Document </title>



<body>
<div id= "News" style= "Display:none" >//Definition display format, first hidden by display:none
<table border= "1″bordercolor=" #FF0000 ″>
<tr>
<td><a href= "link" >title</a></td>
</tr>
</table>
</div>
<scrīpt language= "javascrīpt" src= "file.js" >//reference JS

</scrīpt>
<scrīpt language= "Javascrīpt" >

News_obj=document.getelementbyid (' News ');
e=news_obj.innerhtml;
re=/\title/g; Regular, matching title
re2=/\link/g; Match link
for (i=0;i<a.length;i++) {
Tmp=e.replace (Re,a[i].title); Use A[i].title instead of removed title
Tmp=tmp.replace (RE2,A[I].LN);
document.write (TMP); Display text
}
</scrīpt>
<table width= "200″border=" 1″>

</table>
</body>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.