一个可以针对本地数据库的新闻搜索,添加新闻和单词库操作的php程序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>www.codeke.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="keywords" content="php video,javascript" />
<meta http-equiv="description" content="php php php hp php" />
<style type="text/css">
<!--
span { margin:1px 5px;}
-->
</style>
</head>
<form action="pwd.php" method="post">
<table cellpadding="0" cellspacing="0" border="1px" cols="5" width="300">
<tr><td align="center" colspan="2">列表</td><td colspan="2" align="center">名称</td></tr>
<tr><td align="center" valign="bottom" height="20px" rowspan="2" colspan="3">输入单词</td>
<td colspan="2" rowspan="2" valign="middle" height="20px" align="right">
<input size="26" type="text" name="danci" /></td></tr>
<tr></tr>
<tr id="thanks" style="display:none"><td colspan="5"><span>输入旧单词</span><span><input size="26" type="text" name="jiudanci" value="" /></span></td></tr>
<tr></tr>
<tr><td align="center" valign="bottom" height="20px" colspan="3" >选择操作类型</td>
<td valign="middle" height="20px" align="right">
<select name="select" id="opt" onchange="javascript:change();">
<option value="1" selected="selected">添加</option>
<option value="2">删除</option>
<option value="3">更改</option>
<option value="4">查询</option>
</select>
</td>
</tr>
<script type="text/javascript">
window.onload=synthesize;
function synthesize(){
innerNews();
outterNews();
change();
}
function innerNews(){
document.getElementById("newsArea").innerHTML="";
}
function outterNews(){
document.getElementById("newsArea").innerHTML="请在这里输入新闻正文:";
}
function change(){
var temp = document.getElementById("opt").value;
var temp1 = document.getElementById("new").value;
if(temp==3){
document.getElementById("thanks").style.display="table-row";
}else{
document.getElementById("thanks").style.display="none";
}
if(temp1==1){
document.getElementById("newsArea").style.display="block";
document.getElementById("title").innerHTML="新闻标题:(不能重复)";
}else {
document.getElementById("newsArea").style.display="none";
document.getElementById("title").innerHTML="输入查询关键字";
}
}
</script>
<tr><td colspan="2"><input type="reset" /></td><td align="center" colspan="2"><input type="submit" value="submit" /></td></tr>
</table>
<input type="hidden" name="hidd" value="danci" />
</form>
<div><br/><br/>
<form action="pwd.php" method="post">
<span id="title">Title: </span><input size="30" name="title" type="text" /><br />
<textarea cols="35" rows="10" name="news" id="newsArea" style="display:block" onblur="javascript:outterNews();" onfocus="javascript:innerNews()">
</textarea><br /><br />
<select name="newsel" id="new" onchange="javascript:change();">
<option value="1" selected="selected">添加新闻</option>
<option value="2">查询新闻</option>
</select>
<input type="reset" value="reset" />
<input type="submit" value="submit"/>
<input size="15" name="hidd" value="news" type="hidden" />
</form>
</div>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>www.codeke.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="5;url=access.htm" />
</head>
<?php /*
function readFiles($fileName,$filePath){
// $fileName= "1.jpg";//中文文件名必须进行转码为GB2312
//$fileName=iconv("UTF-8","gb2312",$fileName);
// $filePath=$_SERVER['DOCUMENT_ROOT']."/images/";
if(file_exists($filePath.$fileName)){
$filesize=filesize($filePath.$fileName);
// $fp=fopen($filePath.$fileName,'r');
//请求文件的类型为二进制文件
header("Content-Type:application/octet-stream");
//网页下载框显示控制,例如文件名
header("Content-Disposition:attachment;filename=".basename($filePath.$fileName));
//要下载的文件的大小
header("Accept-Length:$filesize");
$fp=fopen($filePath.$fileName,"r");
do{
echo fread($fp,$filesize);
}while(!feof($fp));
fclose($fp);
return ;
}
} */
require_once 'temp.php';
require_once 'def.class.php'; //这个文件用于数据库操作
$hidd=$_POST['hidd'];
if($hidd==="danci"){
$danci=$_POST['danci'];
$jiudanci=$_POST['jiudanci'];
$choice=$_POST['select'];
$danci=strtoupper($danci); //将单词统一转化成大写的
$jiudanci=strtoupper($jiudanci);
$sqlQue=new SqlDiy("like","admin");
$sqlQue->setDanci($danci);
$sqlQue->setJiudanci($jiudanci);
switch($choice){
case 1:$sqlQue->setOper("insert");break;
case 2:$sqlQue->setOper("delete");break;
case 3:$sqlQue->setOper("update");break;
case 4:$sqlQue->setOper("select");break;
}
if($danci==$jiudanci&&$choice==3){
echo "新单词和旧单词一样,无需更新";
}
if($danci==""||($jiudanci==""&&$choice==3)){
echo "输入为空";
return ;
}
$sqlQue->connSql();
}
if($hidd==="news"){
$title=$_POST['title'];
$news=$_POST['news'];
$choice=$_POST['newsel'];
if($choice==="1"){//添加新闻
$sqlnew=new SqlDiy("like","admin");
$sqlnew->setDanci($title);
$sqlnew->setJiudanci($news);
$sqlnew->setOper("insert");
$tempName=date("His");
$urlfile="http://localhost/6/$tempName.html";
$sqlnew->setUrl($urlfile);
$sqlnew->connNews();
$time=$sqlnew->getTime();
/*必须转化成本地形式或者用ftp_connect()函数来连接文件,http协议是只能读取文件不能
* 写文件,要不然随便哪个用户通过http协议就可以修改服务器的文件,那就危险了,如果
* 要更改文件也应该通过FTP函数来修改,还要修改一下php.ini配置文件来支持ftp,
* php.ini配置文件中要开启allow_url_fopen,设置为on*/
$urlfile="file:///C:/news/test/apache2_2/htdocs/6/$tempName.html";
setHtml($urlfile,$news,$title,$time);
}
else {// quety the news
$sqlnew=new SqlDiy("like","admin");
$sqlnew->setDanci($title);
$sqlnew->setOper("select");
$sqlnew->connNews();
}
}
?>
<?php
function setHtml($file,$news,$title,$time){
if(!file_exists($file)){
$str=file_get_contents("file://C:/news/test/apache2_2/htdocs/6/model.htm");//模板文件,必须有的
file_put_contents($file,$str);
}
$p=fopen($file,"r+");
if(!is_writeable($file)){
echo "It is not writable";
return ;
}
$string="";
$string=file_get_contents($file);
$replacements[0]="<title>$title</title>";
$replacements[1]="<p>$news</p>";
$replacements[2]="<p>$title</p>";
$replacements[3]="$time";
$patterns[0]="/<title>((.*\n)*|(.*\n)).*<\/title>/";
$patterns[1]="/<p>.*<\/p>/";
$patterns[2]="/<h2>.*<\/h2>/";
$patterns[3]="/time must change/";
ksort($patterns);
ksort($replacements);
$string=preg_replace($patterns, $replacements, $string);
file_put_contents($file,$string);
fclose($p);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>www.codeke.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
//header("content-type:text/html;charset=utf-8");
class SqlDiy{
private $time;
private $dbtime;
private $pwd;
private $danci; //symbol for the field named title and danci
private $user;
private $jiudanci; //symbol for the field named jiudanci and newsText
private $oper;
private $url;
function __construct($user,$pwd){
$this->user=$user;
$this->pwd=$pwd;
$this->timeWorkErr();
//$this->createDataBase();
}
function timeWorkErr(){
$this->time=time();
date_default_timezone_set('PRC');
}
function createDataBase(){ //建立一个数据表
$link=mysql_connect('localhost',"root","zhulike");//必须用root账号和密码登录
if(!$link){
die('Could not connect: ' . mysql_error());
}
$sql = 'DROP DATABASE discuz';
if(mysql_query($sql, $link)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
mysql_close($link);
}
function connSql(){
$conn=mysql_connect("localhost",$this->user,$this->pwd);
if(!$conn){
echo mysql_error().error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");
}
mysql_select_db("test",$conn) or error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");
mysql_query("set names utf8");
switch($this->oper){
case "select":$query="select * from `danci` where `danci`='$this->danci'";break;
case "delete":$query="delete from `danci` where danci='$this->danci'";break;
case "insert":$query="insert into `danci`(danci)values('$this->danci')";break;
case "update":$query="update `danci` set `danci`='$this->danci' where `danci`='$this->jiudanci'";break;
}
//$query="update user set name='lily' where name='lily'";
mysql_query("select * from `danci` where danci='$this->danci'",$conn);
if(mysql_affected_rows($conn)>0&&$this->oper=="insert"){
echo "<br />单词库中已经有该单词,无需添加";
return ;
}
if(mysql_affected_rows($conn)<1&&$this->oper=="select"){
echo "<br />单词库里没有该单词"; //针对查询操作
return ;
}
if(mysql_affected_rows($conn)<1&&$this->oper=="delete"){
echo "<br />无法删除一个没有的单词"; //针对删除操作
return ;
}
$res=mysql_query($query,$conn);
if(mysql_affected_rows($conn)<1){
echo "<br />连接数据库有误$this->jiudanci--$this->danci";
error_log(mysql_error().date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");
}
else {
if($this->oper=="select"){
while($rows=mysql_fetch_object($res)){
echo "<br />".$rows->id."--".$rows->danci."--".$rows->dt;
}
mysql_free_result($res);
}
if($this->oper=="insert"){
echo "<br />插入单词成功";
}
if($this->oper=="delete"){
echo "<br />删除单词成功";
}
if($this->oper=="update"){
echo "<br />更新单词成功";
}
}
mysql_close($conn);
}
function connNews(){
$conn=mysql_connect("localhost",$this->user,$this->pwd);
if(!$conn){
echo mysql_error().error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");
}
mysql_select_db("test",$conn) or error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");
mysql_query("set names utf8");
switch($this->oper){
case "select":$query="select * from `news` where title like '%$this->danci%'";break;
case "delete":$query="delete from `news` where title='$this->danci'";break;
case "insert":$query="insert into `news`(title,newsText,url)values('$this->danci','$this->jiudanci','$this->url')";break;
}
mysql_query("select title from `news` where title='$this->danci'",$conn);
if(mysql_affected_rows($conn)>0&&$this->oper=="insert"){
echo "<br />已经有该新闻,无需添加";
return ;
}
if(mysql_affected_rows($conn)<1&&$this->oper=="delete"){
echo "<br />无法删除一个没有的新闻"; //针对删除操作
return ;
}
$res=mysql_query($query,$conn);
if(mysql_affected_rows($conn)<1){
echo "<br />连接数据库有误";
error_log(mysql_error().date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");
}
else {
if($this->oper=="select"){
$tempStr="<h2>搜索结果:</h2><br /><ul><br />";
while($rows=mysql_fetch_object($res)){
$tempStr .= "<br /><li><a href='$rows->url'>$rows->title</a>发布时间:$rows->dt</li><br />$rows->newsText<br /><br />";
}
$tempStr.="</ul>";echo "$tempStr";
//setdbHtml($tempStr,"test for win32");
mysql_free_result($res);
}
else{
$query="select dt from `news` where title='$this->danci' limit 1";
$res=mysql_query($query,$conn);
$rows=mysql_fetch_object($res);
$this->dbtime=$rows->dt;
if($this->oper=="insert"){
echo "<br />添加新闻成功";
}
if($this->oper=="delete"){
echo "<br />删除新闻成功";
}
}
}
mysql_close($conn);
}
function getTime(){
return $this->dbtime;
}
function setJiudanci($i){
$this->jiudanci=$i;
}
function setOper($i){
$this->oper=$i;
}
function setDanci($i){
$this->danci=$i;
}
function setUrl($i){
$this->url=$i;
}
}
?>
其实先理清这些文件的调用过程就是所谓的框架写代码就是最简单的,不过我还是太低级了!还没有搞懂用框架去写代码,我想就像写作文一样,事先先列一个怎么写的提纲在去写作文就会好多了,把每一部分的结构每一个文件的作用弄清楚是最好不过了。
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。