php实现搜索
‘壹’ php中怎么实现关键字搜索
PHP要实现关键字查搜索,需要用到like关键字来组合查询条件
like具体实现方法如下:
例一:
1$userForm=M('user');
1$where['name']=array('like','phpernote%');
2$userForm->where($where)->select();
这里的like查询即为:name like 'phpernote%'
例二:
1$where['name']=array('like',array('%phpernote%','%.com'),'OR');
这里的like查询即为:name like '%phpernote%' or name like '%.com'
例三:
1$where['name']=array(array('like','%a%'),array('like','%b%'),array('like','%c%'),'phpernote','or');
这里的like查询即为:(`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'phpernote')
例四:
1$where['_string']='(namelike"%phpernote%")OR(titlelike"%phpernote")'
这里的like查询即为:name like '%phpernote%' or title like '%phpernote'
‘贰’ PHP搜索怎么做
php中要查询mysql数据库中的内容必须先连接mysql数据库,然后再利用sql语句进行查询
SELECT 语句用于从数据库中选取所要搜索的数据。
具体用法为:
语句1:SELECT*FROMtable_name
解说:意思就是读取整个表table_name里面的数据显示出来
语句1:SELECT*FROMtable_nameWherex=1
解说:意思就是读取表table_name里面键名为:x值为:1的数据显示出来
Select 查询语句的例子
下面的例子选取存储在 "Persons" 表中的所有数据(* 字符选取表中所有数据):
<?php
$con=mysql_connect("localhost","peter","abc123");
if(!$con)
{
die('Couldnotconnect:'.mysql_error());
}
mysql_select_db("my_db",$con);
$result=mysql_query("SELECT*FROMPersons");
while($row=mysql_fetch_array($result))
{
echo$row['FirstName']."".$row['LastName'];
echo"<br/>";
}
mysql_close($con);
?>
上面这个例子在 $result 变量中存放由 mysql_query() 函数返回的数据。接下来,使用
mysql_fetch_array() 函数以数组的形式从记录集返回第一行。每个随后对 mysql_fetch_array()
函数的调用都会返回记录集中的下一行。 while loop 语句会循环记录集中的所有记录。为了输出每行的值,使用了 PHP 的 $row
变量 ($row['FirstName'] 和 $row['LastName'])。
以上代码的输出:
PeterGriffin
GlennQuagmire
‘叁’ PHP如何实现模糊搜索
mysql支持自然语言的全文搜索
对于字段的要求:
只能是CHAR, VARCHAR, 或 TEXT 类型的字段
表类型是MyISAM在表建好,并导入数据后,建立一个fulltext index(索引)
用法:
select 字段1,字段2,字段3, MATCH(要匹配的字段名) AGAINST('keyword') as score from table having score>0命中的每一行都会有个分数,分数越大表示结果越接近keyword,分数越低的就是越模糊的结果
‘肆’ 怎么用php代码写一个小搜索框,输入关键字后点击搜索下面会返回输入的关键字
php是后端的程序,搜索框只是html文件,如果你不想查询数据库,只是想显示输入的关键字,直接使用javascript就可以实现了。
点击搜索后,取输入框的值,使用js显示就可以。
‘伍’ php实现站内商品搜索
首先,把商品的各个属性也要存到商品中去,然后,前端发送要查询的条件给后台,后台接收到后,组装参数和sql,从库里查询出来,再返给前端显示.
大致就是这个流程,如果不清楚,请继续问.
‘陆’ 关于 php 站内搜索 的实现~~
搜索结果id和标题都是数组,就要先传递到模板,在模板里用foreach循环显示出来
比如搜索结果是
id、title,
$result=$rows;
$smarty->assign('result',$result)传递到模板
然后模板foreach循环显示
{foreach
from=$result
item=foo}
<a
href="shop.php?id={$foo.id}">{$foo.title}</a>
{/foreach}
大概是这样
‘柒’ php 如何实现文章内容搜索
简单的方法是
SELECT * FROM 文章表 WHERE 内容 like '%新闻%';
下面另外二种方法给你参考
另外业务层方面的方法是设置标签,然后关联表里面记录对应的文章ID
另外技术层方面的方法是使用搜索引挚,如sphinx等。这是高级部分
‘捌’ 如何用php实现搜索功能
这是SQL语句实现的
‘玖’ php怎么实现根据图片搜索图片功能
php爱好者 们很对php开发的追求是永不止步的,今天偶然想起来了 发下
php怎么实现相似图片的搜索呢?
其中的原理来解释下
1、缩小尺寸。将图片缩小到8×8的尺寸,总共64个像素。这一步的作用是去除图片的细节,只保留结构、明暗等基本信息,摒弃不同尺寸、比例带来的图片差异。
2、简化色彩。将缩小后的图片,转为64级灰度。也就是说,所有像素点总共只有64种颜色。
3、计算平均值。计算所有64个像素的灰度平均值。
4、比较像素的灰度。将每个像素的灰度,与平均值进行比较。大于或等于平均值,记为1;小于平均值,记为0。
5、计算哈希值。将上一步的比较结果,组合在一起,就构成了一个64位的整数,这就是这张图片的指纹。组合的次序并不重要,只要保证所有图片都采用同样次序就行了。得到指纹以后,就可以对比不同的图片,看看64位中有多少位是不一样的。
使用代码
hash($f);
}
return $isString ? $result[0] : $result;
}
public function checkIsSimilarImg($imgHash, $otherImgHash){
if (file_exists($imgHash) && file_exists($otherImgHash)){
$imgHash = $this->run($imgHash);
$otherImgHash = $this->run($otherImgHash);
}
if (strlen($imgHash) !== strlen($otherImgHash)) return false;
$count = 0;
$len = strlen($imgHash);
for($i=0;$i<$len;$i++){
if ($imgHash{$i} !== $otherImgHash{$i}){
$count++;
}
}
return $count <= (5 * $rate * $rate) ? true : false;
}
public function hash($file){
if (!file_exists($file)){
return false;
}
$height = 8 * $this->rate;
$width = 8 * $this->rate;
$img = imagecreatetruecolor($width, $height);
list($w, $h) = getimagesize($file);
$source = $this->createImg($file);
imageresampled($img, $source, 0, 0, 0, 0, $width, $height, $w, $h);
$value = $this->getHashValue($img);
imagedestroy($img);
return $value;
}
public function getHashValue($img){
$width = imagesx($img);
$height = imagesy($img);
$total = 0;
$array = array();
for ($y=0;$y<$height;$y++){
for ($x=0;$x<$width;$x++){
$gray = ( imagecolorat($img, $x, $y) >> 8 ) & 0xFF;
if (!is_array($array[$y])){
$array[$y] = array();
}
$array[$y][$x] = $gray;
$total += $gray;
}
}
$average = intval($total / (64 * $this->rate * $this->rate));
$result = '';
for ($y=0;$y<$height;$y++){
for ($x=0;$x<$width;$x++){
if ($array[$y][$x] >= $average){
$result .= '1';
}else{
$result .= '0';
}
}
}
return $result;
}
public function createImg($file){
$ext = $this->getFileExt($file);
if ($ext === 'jpeg') $ext = 'jpg';
$img = null;
switch ($ext){
case 'png' : $img = imagecreatefrompng($file);break;
case 'jpg' : $img = imagecreatefromjpeg($file);break;
case 'gif' : $img = imagecreatefromgif($file);
}
return $img;
}
public function getFileExt($file){
$infos = explode('.', $file);
$ext = strtolower($infos[count($infos) - 1]);
return $ext;
}
}
调用方式如下:
require_once "Imghash.class.php";
$instance = ImgHash::getInstance();
$result = $instance->checkIsSimilarImg('chenyin/IMG_3214.png', 'chenyin/IMG_3212.JPG');
如果$result值为true, 则表明2个图片相似,否则不相似。
‘拾’ php如何实现下拉框选中搜索功能
这种功能主要属于前端范筹,和php关联不大。
思路是用js实现,绑定选择框的change事件,然后ajax获取第二个选择框的列表。
当第二个选择框触发change事件,就ajax加载列表,把相关的参数传到后端,获取数据后再输出到页面。
推荐使用jQuery库,它的html处理,和ajax都很方便新人上手。
关键点:后端需要配合输出json格式数据,方便解析.
如:
$subcates=[
['id'=>1,'name'=>'aaa'],
['id'=>2,'name'=>'bbb'],
];
echo json_encode([ 'code'=>1,'result'=>$subcate ],JSON_UNESCAPED_UNICODE);
前端jQuery获取数据
$.ajax({
url:'getsubcate.php?bigcate=1',
dataType:'JSON',
success:function(json){
console.log(json.result);
//此处将result数组拼接成html放入第二个选择框
}
});
获取列表的操作类似。
另外,如果对这种动态页面感兴趣的话,可以学习下Vue或React 这些前端框架。只需专注数据,不需要处理html.