Example, php string splitting:
-
- $keyword="asp php,jsp";
- $keyword=str_replace(" "," ",$keyword);
- $keyword=str_replace(" ",",", $keyword);
- $keyarr=explode(',',$keyword);
- for($index=0;$index{
- $whereSql .= " And (arc. title like '%$keyarr[$index]%' Or arc.keywords like '%$keyarr[$index]%') ";
- }
- echo $whereSql;
Copy code In order to support both spaces and Commas need to be replaced with unified commas in advance, that is, first replace all spaces with commas, then split the string by commas, and then splice the sql query statement in a loop.
str_replace is the commonly used string replacement function in php.
explode is a function commonly used in PHP to split strings into arrays.
PHP implementation of multi-condition query example code
Multi-condition query is often used, and I will share an example of second-hand house query.
In this example, it is necessary to query the information required by customers through multiple conditions such as geographical location, property type, house price, house area, and information release date.
Query file (search.php)
1. Generate query statements:
-
- $conn=mysql_connect("localhost","root","");
- $db=mysql_select_db("lingyun");
- $query="select * from message where tradetype= '".$tradetype."'"; //Transaction type, such as renting, selling
- $SQL=$SQL . "wuye='" . $wuye . "'";
- if($housetype!="No limit" ){
- $query.=" && housetype='".$housetype."'"; //House type, such as two bedrooms and one living room, three bedrooms and two living rooms
- }
- if($degree!="No limit"){
- $query.=" && degree='".$degree."'"; //The degree of old and new
- }
- if($wuye!="No limit"){
- $query.=" && wuye='". $wuye."'"; //Property type such as housing, shops
- }
- if($price2!=""){
- switch($price1){
- case "greater than":
- $query.=" && price> '".$price2."'"; //Price
- break;
- case "equal to":
- $query.=" && price='".$price2."'";
- break;
- case "less than":
- $query.=" && price<'".$price2."'";
- break;
- }
- }
- if($area2!=""){
- switch($area1){
- case "greater than":
- $query.=" && area>'".$area2."'"; //area
- break;
- case "equal to":
- $query.=" && area='".$area2."'";
- break;
- case "less than":
- $query.=" && area<'".$area2."'";
- break;
- }
- }
- switch($pubdate){ //Publish date
- case "this Within the week":
- $query.=" && TO_DAYS(NOW()) - TO_DAYS(date)<=7";
- break;
- case "Within the month":
- $query.=" && TO_DAYS(NOW( )) - TO_DAYS(date)<=30";
- break;
- case "within three months":
- $query.=" && TO_DAYS(NOW()) - TO_DAYS(date)<=91";
- break;
- case "within six months":
- $query.=" && TO_DAYS(NOW()) - TO_DAYS(date)<=183";
- break;
- }
- if($address!="") {
- $query.=" && address like '%$address%'"; //Address
- }
- if(!$page){
- $page=1;
- }
- ?>
Copy code
二、输出查询结果:
-
- if ($page){
- $page_size=20;
- $result=mysql_query($query);
- #$message_count=mysql_result($result,0,"total");
- $message_count=10;
- $page_count=ceil($message_count/$page_size);
- $offset=($page-1)*$page_size;
- $query=$query." order by date desc limit $offset, $page_size";
- $result=mysql_query($query);
- if($result){
- $rows=mysql_num_rows($result);
- if($rows!=0){
- while($myrow=mysql_fetch_array($result)){
- echo "
";
- echo "
| ";
- echo "
$myrow[id] $myrow[tradetype] $myrow[address] $myrow[wuye]($myrow[housetype])[$myrow[date]]";
- echo "
| ";
- echo "
详细内容 | ";
- echo "
";
- }
- }
- else echo "
没有找到满足你条件的记录 & lt;/td> | ";
- }
- $prev_page=$page-1;
- $next_page=$page 1;
- echo "
";
- echo " 第".$page."/".$page_count."页 ";
- if ($page<=1){
- echo "|第一页|";
- }
- else{
- echo "|第一页|";
- }
- echo " ";
- if ($prev_page<1){
- echo "|上一页|";
- }
- else{
- echo "|上一页|";
- }
- echo " ";
- if ($next_page>$page_count){
- echo "|下一页|";
- }
- else{
- echo "|下一页|";
- }
- echo " ";
- if ($page>=$page_count){
- echo "|最后一页|";
- }
- else{
- echo "|最后一页|";
- }
- echo "
";
- }
- else{
- echo "
现在还没有房屋租赁信息! ";
- }
- echo "
";
- ?>
|