php从数据库里读出的HTML代码然后执行怎么实现

php从数据库里读出的HTML代码然后执行怎么实现,第1张

<?php require_once("mysql_class.php")

require_once("sys_conf.inc")

header('Content-Type:text/htmlCharset=GBK')

$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD)

mysql_select_db($DBNAME) //选择数据库my_chat

$sql="SELECT * FROM `dx_leibie`"

$query = mysql_query($sql)

while($row=mysql_fetch_array($query)){

//print_r($row) //$con=array(array('新闻标题','新闻内容'),array('新闻标题2','新闻内容2')) $title=$row[id_lang]

$content=$row[name]

if(!is_dir("up")) //如果此文件夹不存在,则自动建立一个

{

mkdir("up")

}$up="up/"

echo $path=$up.$row[id_lang].'.htm'

$fp=fopen("tmp.htm","r")//只读打开模板

$str=fread($fp,filesize("tmp.htm"))//读取模板中内容

$str=str_replace("{title}",$title,$str)

echo $str=str_replace("{content}",$content,$str)//替换内容

fclose($fp) $handle=fopen($path,"w")//写入方式打开新闻路径

fwrite($handle,$str)//把刚才替换的内容写进生成的HTML文件

fclose($handle)

//echo "生成成功"

} // unlink($path)//删除文件

?><?php require_once("mysql_class.php")

require_once("sys_conf.inc")

header('Content-Type:text/htmlCharset=GBK')

$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD)

mysql_select_db($DBNAME) //选择数据库my_chat

$sql="SELECT * FROM `dx_leibie`"

$query = mysql_query($sql)

while($row=mysql_fetch_array($query)){

//print_r($row) //$con=array(array('新闻标题','新闻内容'),array('新闻标题2','新闻内容2')) $title=$row[id_lang]

$content=$row[name]

if(!is_dir("up")) //如果此文件夹不存在,则自动建立一个

{

mkdir("up")

}$up="up/"

echo $path=$up.$row[id_lang].'.htm'

$fp=fopen("tmp.htm","r")//只读打开模板

$str=fread($fp,filesize("tmp.htm"))//读取模板中内容

$str=str_replace("{title}",$title,$str)

echo $str=str_replace("{content}",$content,$str)//替换内容

fclose($fp) $handle=fopen($path,"w")//写入方式打开新闻路径

fwrite($handle,$str)//把刚才替换的内容写进生成的HTML文件

fclose($handle)

//echo "生成成功"

} // unlink($path)//删除文件

?><?php require_once("mysql_class.php")

require_once("sys_conf.inc")

header('Content-Type:text/htmlCharset=GBK')

$link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD)

mysql_select_db($DBNAME) //选择数据库my_chat

$sql="SELECT * FROM `dx_leibie`"

$query = mysql_query($sql)

while($row=mysql_fetch_array($query)){

//print_r($row) //$con=array(array('新闻标题','新闻内容'),array('新闻标题2','新闻内容2')) $title=$row[id_lang]

$content=$row[name]

if(!is_dir("up")) //如果此文件夹不存在,则自动建立一个

{

mkdir("up")

}$up="up/"

echo $path=$up.$row[id_lang].'.htm'

$fp=fopen("tmp.htm","r")//只读打开模板

$str=fread($fp,filesize("tmp.htm"))//读取模板中内容

$str=str_replace("{title}",$title,$str)

echo $str=str_replace("{content}",$content,$str)//替换内容

fclose($fp) $handle=fopen($path,"w")//写入方式打开新闻路径

fwrite($handle,$str)//把刚才替换的内容写进生成的HTML文件

fclose($handle)

//echo "生成成功"

} // unlink($path)//删除文件

?>用以上先生成一个HTML文件,然后再用require_once("aaa.html")引进来就OK

html只能通过ajax接口跟后台数据库接口通信,然后取出来的数据在页面上显示。

以下是一个例子:

html文件suggest.html:

<html>

<head>

<meta http-equiv="content-type" content="text/htmlcharset=UTF-8">

<script language="JavaScript" type="text/javascript" src="ajax_search.js"></script>

<style type="text/css" media="screen">

body {

font: 11px arial

}

.suggest_link {

background-color: #FFFFFF

padding: 2px 6px 2px 6px

cursor:hand

}

.suggest_link_over {

background-color: #E8F2FE

padding: 2px 6px 2px 6px

}

#search_suggest {

position:relative

background-color: #FFFFFF

text-align: left

border: 1px solid #000000

left: -12px

top: 1px

height: 74px

width: 170px

display:none

}

</style>

</head>

<body background="bg.gif">

<center>

<h3>实现搜索提示</h3>

<div style="width: 500px">

<input type="text" id="txtSearch" name="txtSearch" onKeyUp="searchSuggest()" autocomplete="off" />

<input type="submit" id="cmdSearch" name="cmdSearch" value="搜索"/><br />

<div id="search_suggest"></div>

</div>

</center>

</body>

</html>

ajax_search.js:

var searchReq = createAjaxObj()

function createAjaxObj()

{

var httprequest=false

if (window.XMLHttpRequest) //为非IE浏览器生成XmlHttpRequest对象

{

httprequest=new XMLHttpRequest()

if (httprequest.overrideMimeType) httprequest.overrideMimeType('text/xml')

}

else if (window.ActiveXObject) //为IE浏览器生成XmlHttpRequest对象

{

try

{

httprequest=new ActiveXObject("Msxml2.XMLHTTP")

}

catch (e)

{

try

{

httprequest=new ActiveXObject("Microsoft.XMLHTTP")

}

catch (e){}

}

}

return httprequest

}

function searchSuggest()

{

if (searchReq.readyState == 4 || searchReq.readyState == 0)

{

var str = escape(document.getElementById('txtSearch').value)

searchReq.open("GET", 'search?search=' + str, true)//向服务器端发送请求

searchReq.onreadystatechange = handleSearchSuggest//设置回调函数

searchReq.send(null)

}

}

function handleSearchSuggest()

{ //这是回调函数,处理从服务器端返回的信息

if (searchReq.readyState == 4)

{

var ss = document.getElementById('search_suggest')

ss.innerHTML = ''

var str = searchReq.responseText.split("\n")

for(i=0i <str.length - 1i++)

{

var suggest = '<div onmouseover="javascript:suggestOver(this)" '

suggest += 'onmouseout="javascript:suggestOut(this)" '

suggest += 'onclick="javascript:setSearch(this.innerHTML)" '

suggest += 'class="suggest_link">' + str[i] + '</div>'

ss.innerHTML += suggest

ss.style.display="block"

}

}

}

function suggestOver(div_value)

{

div_value.className = 'suggest_link_over'

}

function suggestOut(div_value)

{

div_value.className = 'suggest_link'

}

function setSearch(value)

{

document.getElementById('txtSearch').value = value

document.getElementById('search_suggest').innerHTML = ''

document.getElementById('search_suggest').style.display="none"

}

servlet文件SearchSuggest.java:

package com

import java.sql.Connection

import java.sql.DriverManager

import java.sql.ResultSet

import java.sql.SQLException

import java.sql.Statement

import java.util.ArrayList

import javax.servlet.ServletException

import javax.servlet.http.HttpServlet

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

public class SearchSuggest extends HttpServlet

{

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException

{

response.setCharacterEncoding("UTF-8")

String search = request.getParameter("search")//接受参数值

String sql = "select GoodsName from Goods where GoodsName like '"+search+"%' order by GoodsName"//设置查询语句

Connection conn = null

Statement stmt = null

ResultSet rs = null

ArrayList vData = new ArrayList()

java.io.PrintWriter out = response.getWriter()

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")//注册数据库连接驱动

String MdbPath = "F:/sneaker/Tomcat/webapps/search/database/#costume.mdb"

String url= "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)}DBQ=" + MdbPath

conn = DriverManager.getConnection(url)

stmt = conn.createStatement()

rs = stmt.executeQuery(sql)//执行查询

while (rs.next())

{

vData.add(rs.getString("GoodsName"))

}

StringBuilder buf = new StringBuilder()

for (int i=0i<vData.size()i++)

{

String keyword = (String)vData.get(i)

buf.append(keyword+"\n")

}

out.print(buf.toString())//向客户端输出信息

} catch (Exception e) {

e.printStackTrace()

} finally {

try {

if (stmt != null)

stmt.close()

if (conn != null)

conn.close()

} catch (SQLException sqle) {

}

}

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, java.io.IOException

{

doPost(request, response)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10027451.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存