实现原理很简单就是根据用户提交的数据到mysql数据表中查询是否有相同的,有就输出没有就不操作了。这是一款简单的记录搜索代码。
首先我们来创建搜索数据表
create table if not exists `search` (
`id` int(4) not null auto_increment,
`keyword` varchar(500) not null,
primary key (`id`)
) engine=myisam default charset=utf8 auto_increment=1 ;
保存数据
代码如下 |
复制代码 |
insert into `acc`.`search` (
`id` ,
`keyword`
)
values (
null , '内容搜索'
), (
null , 'php站内搜索'
); |
*/
//数据连接
代码如下 |
复制代码 |
$conn = mysql教程_connect('localhost','ac','1');//这里的ac用户名,1是密码,修改成你自己的mysql便可
$res = mysql_db_query('abc',"select * from search where keyword='内容搜索' ") or die(mysql_error());
if(mysql_num_rows($res) == 1) {
while($row = mysql_fetch_assoc($res)) {
foreach($row as $key => $val) {
echo $row['keyword'],'<br >';
}
}
}
|
/*
本站原创转载注明来源www.111cn.net
*/
这是一款开源的php代码,ExcelReader是专业地excel表进行读写操作的哦,下面我们来看一款ExcelReader读取111cn.net.xls工作表实例吧。
代码如下 |
复制代码 |
$allow_url_override = 1; // set to 0 to not allow changed via post or get
if(!$allow_url_override || !isset($file_to_include))
{
$file_to_include = "111cn.net.xls";
}
if(!$allow_url_override || !isset($max_rows))
{
$max_rows = 0; //use 0 for no max
}
if(!$allow_url_override || !isset($max_cols))
{
$max_cols = 5; //use 0 for no max
}
if(!$allow_url_override || !isset($debug))
{
$debug = 0; //1 for on 0 for off
}
if(!$allow_url_override || !isset($force_nobr))
{
$force_nobr = 1; //force the info in cells not to wrap unless stated explicitly (newline)
}
require_once 'www.111cn.net/reader.php';
$data = new spreadsheet_excel_reader();
$data->setoutputencoding('cpa25a');
$data->read($file_to_include);
error_reporting(e_all ^ e_notice);
echo "
<style>
.table_data
{
border-style:ridge;
border-width:1;
}
.tab_base
{
background:#c5d0dd;
font-weight:bold;
border-style:ridge;
border-width:1;
cursor:pointer;
}
.table_sub_heading
{
background:#cccccc;
font-weight:bold;
border-style:ridge;
border-width:1;
}
.table_body
{
background:#f0f0f0;
font-wieght:normal;
font-size:12;
font-family:sans-serif;
border-style:ridge;
border-width:1;
border-spacing: 0px;
border-collaps教程e: collapse;
}
.tab_loaded
{
background:#222222;
color:white;
font-weight:bold;
border-style:groove;
border-width:1;
cursor:pointer;
}
</style>
";
function make_alpha_from_numbers($number)
{
$numeric = "abcdefghijklmnopqrstuvwxyz";
if($number<strlen($numeric))
{
return $numeric[$number];
}
else
{
$dev_by = floor($number/strlen($numeric));
return "" . make_alpha_from_numbers($dev_by-1) . make_alpha_from_numbers($number-($dev_by*strlen($numeric)));
}
}
echo "<script language='网页特效'>
var sheet_html = array(); ";
for($sheet=0;$sheet<count($data->sheets);$sheet++)
{
$table_output[$sheet] .= "<table class='table_body'>
<tr>
<td> </td>";
for($i=0;$i<$data->sheets[$sheet]['numcols']&&($i<=$max_cols||$max_cols==0);$i++)
{
$table_output[$sheet] .= "<td class='table_sub_heading' align=center>" . make_alpha_from_numbers($i) . "</td>";
}
for($row=1;$row<=$data->sheets[$sheet]['numrows']&&($row<=$max_rows||$max_rows==0);$row++)
{
$table_output[$sheet] .= "<tr><td class='table_sub_heading'>" . $row . "</td>";
for($col=1;$col<=$data->sheets[$sheet]['numcols']&&($col<=$max_cols||$max_cols==0);$col++)
{
if($data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'] >=1 && $data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan'] >=1)
{
$this_cell_colspan = " colspan=" . $data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'];
$this_cell_rowspan = " rowspan=" . $data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan'];
for($i=1;$i<$data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'];$i++)
{
$data->sheets[$sheet]['cellsinfo'][$row][$col+$i]['dontprint']=1;
}
for($i=1;$i<$data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan'];$i++)
{
for($j=0;$j<$data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'];$j++)
{
$data->sheets[$sheet]['cellsinfo'][$row+$i][$col+$j]['dontprint']=1;
}
}
}
else if($data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'] >=1)
{
$this_cell_colspan = " colspan=" . $data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'];
$this_cell_rowspan = "";
for($i=1;$i<$data->sheets[$sheet]['cellsinfo'][$row][$col]['colspan'];$i++)
{
$data->sheets[$sheet]['cellsinfo'][$row][$col+$i]['dontprint']=1;
}
}
else if($data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan'] >=1)
{
$this_cell_colspan = "";
$this_cell_rowspan = " rowspan=" . $data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan'];
for($i=1;$i<$data->sheets[$sheet]['cellsinfo'][$row][$col]['rowspan'];$i++)
{
$data->sheets[$sheet]['cellsinfo'][$row+$i][$col]['dontprint']=1;
}
}
else
{
$this_cell_colspan = "";
$this_cell_rowspan = "";
}
if(!($data->sheets[$sheet]['cellsinfo'][$row][$col]['dontprint']))
{
$table_output[$sheet] .= "<td class='table_data' $this_cell_colspan $this_cell_rowspan> ";
if($force_nobr)
{
$table_output[$sheet] .= "<nobr>";
}
$table_output[$sheet] .= nl2br(htmlentities($data->sheets[$sheet]['cells'][$row][$col]));
if($force_nobr)
{
$table_output[$sheet] .= "</nobr>";
}
$table_output[$sheet] .= "</td>";
}
}
$table_output[$sheet] .= "</tr>";
}
$table_output[$sheet] .= "</table>";
$table_output[$sheet] = str_replace(" ","",$table_output[$sheet]);
$table_output[$sheet] = str_replace(" ","",$table_output[$sheet]);
$table_output[$sheet] = str_replace(" "," ",$table_output[$sheet]);
if($debug)
{
$debug_output = print_r($data->sheets[$sheet],true);
$debug_output = str_replace(" ","\n",$debug_output);
$debug_output = str_replace(" ","\r",$debug_output);
$table_output[$sheet] .= "<pre>$debug_output</pre>";
}
echo "sheet_html[$sheet] = "$table_output[$sheet]"; ";
}
echo "
function change_tabs(sheet)
{
//alert('sheet_tab_' + sheet);
for(i=0;i<" , count($data->sheets) , ";i++)
{
document.getelementbyid('sheet_tab_' + i).classname = 'tab_base';
}
document.getelementbyid('table_loader_div').innerhtml=sheet_html[sheet];
document.getelementbyid('sheet_tab_' + sheet).classname = 'tab_loaded';
}
</script>";
echo "
<table class='table_body' name='tab_table'>
<tr>";
for($sheet=0;$sheet<count($data->sheets);$sheet++)
{
echo "<td class='tab_base' id='sheet_tab_$sheet' align=center
onmousedown="change_tabs($sheet);">", $data->boundsheets[$sheet]['name'] , "</td>";
}
echo
"<tr>";
echo "</table>
<div id=table_loader_div>www.111cn.net</div>
<script language='网页特效'>
change_tabs(0);
</script>";
//echo "<iframe name=table_loader_iframe src='about:blank' width=100 height=100></iframe>";
/*
echo "<pre>";
print_r($data);
echo "</pre>";
*/
|
// excelreader这个类你可以到官网去下载哦。
php搜索csv中的数据原理是打开csv文件,然后一行行搜索指定的内容是否包含在我们读取这代码中,如果是返回true。
//搜索csv中指定内容
代码如下 |
复制代码 |
$fh = @fopen("csv_file_name", "r");
if($fh) {
if(flock($fh, lock_ex)) {
while(!feof($fh)) {
$line = fgets($fh);
if(strstr($line, $target_email_address) !== false) {
$data = split(",", $line); // $data *is* an array
}
}
flock($fh, lock_un);
}
fclose($fh);
}
|
代码如下 |
复制代码 |
$cn = mysql_connect('localhost','root','root');
if( $cn )
{
die('数据库连接成功');
}
else
{
die('连接失败');
} |
这是一款比较适合php初学者学的教程哦,我们利用一个简单的实例来对数据库添加、修改、删除哦,这样更系统的让各位知道php mysql数据库操作的要点。
require_once('common.php');
$action = $_get['action'];
?>
代码如下 |
复制代码 |
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>人才列表</title>
<link href="style.css教程" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="wrap">
<div id="main">
<?php
if($action=='add'){
?>
<form action="?action=save" method="post" name="form1">
<table width="300" border="0" cellspacing="0" cellpadding="0" class="post">
<tr>
<td colspan="2">添加人员</td></tr>
<tr><td width="78">登陆账号</td>
<td width="220"><input name="login" type="text" id="login" /></td>
</tr>
<tr>
<td>登陆密码</td>
<td><input name="pws" type="text" id="pws" /></td>
</tr>
<tr>
<td>问题</td>
<td><input name="question" type="text" id="question" /></td>
</tr>
<tr>
<td>答案</td>
<td><input name="answer" type="text" id="answer" /></td>
</tr>
<tr>
<td colspan="2"><input name="button" type="submit" id="button" value=" 添加 " /></td>
</tr>
</table>
</form>
<?php
}
elseif($action=='save'){
$login = isset($_post['login']) ? $_post['login'] : '';
$pws = isset($_post['pws']) ? $_post['pws'] : '';
$question = isset($_post['question']) ? $_post['question'] : '';
$answer = isset($_post['answer']) ? $_post['answer'] : '';
$sql = "insert into person (login,pws,question,answer)
values('$login','$pws','$question','$answer')";
$db->query($sql);
forward('发布成功','href','personlist.php');
}
elseif($action=='del'){
$person_id=$_get['person_id'];
$page=$_get['page'];
$sql="delete from person where person_id='$person_id'";
$db->query($sql);
forward('删除成功','href','personlist.php?page='.$page);
}
elseif($action=='editsave'){
$person_id = isset($_post['person_id']) ? $_post['person_id'] : '';
$page = isset($_post['page']) ? $_post['page'] : '';
$login = isset($_post['login']) ? $_post['login'] : '';
$pws = isset($_post['pws']) ? $_post['pws'] : '';
$question = isset($_post['question']) ? $_post['question'] : '';
$answer = isset($_post['answer']) ? $_post['answer'] : '';
$sql="update person set login='$login',pws='$pws',question='$question',answer='$answer' where person_id='$person_id'";
$db->query($sql);
forward('修改成功','href','personlist.php?page='.$page);
}
elseif($action=='edit'){
$person_id=$_get['person_id'];
$page=$_get['page'];
$sql="select * from person where person_id='$person_id'";
$query = $db->query($sql);
$row = $db->fetch_array($query);
$login=$row['login'];
$pws=$row['pws'];
$question=$row['question'];
$answer=$row['answer'];
?>
<form action="?action=editsave" method="post" name="form1">
<table width="300" border="0" cellspacing="0" cellpadding="0" class="post">
<tr>
<input name="page" type="hidden" value="<?php echo $page?>"/>
<input name="person_id" type="hidden" value="<?php echo $person_id?>"/>
<td colspan="2">修改人员</td></tr>
<tr><td width="78">登陆账号</td>
<td width="220"><input name="login" type="text" id="login" value="<?php echo $login?>"/></td>
</tr>
<tr>
<td>登陆密码</td>
<td><input name="pws" type="text" id="pws" value="<?php echo $pws?>"/></td>
</tr>
<tr>
<td>问题</td>
<td><input name="question" type="text" id="question" value="<?php echo $question?>"/></td>
</tr>
<tr>
<td>答案</td>
<td><input name="answer" type="text" id="answer" value="<?php echo $answer?>"/></td>
</tr>
<tr>
<td colspan="2"><input name="button" type="submit" id="button" value=" 修改 " /></td>
</tr>
</table>
</form>
<?php
}
else{
$page = isset($_get['page']) ?intval($_get['page']) : 1;
$num = 5;
$sql="select * from person";
$query = $db->query($sql);
$totalnum = $db->num_rows($query);//记录总数
$pagenum = ceil($totalnum/$num); //总页数
$offset = ($page-1) * $num;
$sql=$sql." limit $offset,$num ";
$query = $db->query($sql);//取得记录
?>
<table width="639" border="0" cellspacing="0" cellpadding="0" class="post">
<tr>
<td colspan="6">记录总数:<?php echo $totalnum;?>————<a href="personlist.php?action=add">添加人员</a></td></tr>
<tr><td width="126">登陆账号</td>
<td width="98">登陆密码</td>
<td width="115">问题</td>
<td width="66">答案</td>
<td width="138">加入时间</td>
<td width="94">操作</td>
</tr>
<?php
while ($row = $db->fetch_array($query)) {
?>
<tr>
<td><?php echo $row['login'];?></td>
<td><?php echo $row['pws'];?></td>
<td><?php echo $row['question'];?></td>
<td><?php echo $row['answer'];?></td>
<td><?php echo $row['addtime'];?></td>
<td><a href="?action=del&person_id=<?php echo $row['person_id'] ?>&page=<?php echo $page ?>">删除</a>/
<a href="?action=edit&person_id=<?php echo $row['person_id'] ?>&page=<?php echo $page ?>">修改</a></td>
</tr>
<?php
}
?>
</table>
</div>
<div id="pages_btns">
<div class="pages"><?php showpage($page, $num, $pagenum, $totalnum)?></div>
</div>
<?php
}
?>
</div>
</body>
</html>
|