php - 如何使用文件名获得多个搜索键

标签 php html mysql

我有一个数据库,其中文件名为 abcd100.00b、abcd101.00b、...

我需要一个代码,当用户单独输入 abcd 时,应显示 100 到 110 个名称为 abcd 且在 100 到 110 范围内的所有文件。

这些是 rinex 文件,其命名约定如 iisc001.10h。有多个文件,范围从 iisc001.10hiisc100.30h 等等。我希望能够搜索 iisc (前 4 个字符),然后搜索 001 到 100 - 此范围内的所有文件都需要以表格形式显示。

下面的代码只能显示前四个字符,我该如何实现?

    <?php

//capture search term and remove spaces at its both ends if the is any

     $searchTerm = trim($_GET['keyname']) ;

//check whether the name parsed is empty

     if($searchTerm == "rinex_file")
     {
       echo "Enter name you are searching for.";
       exit();
      }
      if($searchTerm == "rinex_file")
     {
      echo "Enter name you are searching for.";
      exit();
      }

//database connection info

    $host = "localhost"; //server
    $db = "rinex"; //database name
    $user = "m"; //dabases user name
    $pwd = "c"; //password

//connecting to server and creating link to database

    $link = mysqli_connect($host, $user, $pwd, $db);

//MYSQL search statement

    $query = "SELECT * FROM rinexo WHERE rinex_file LIKE '%$searchTerm%'";

     $results = mysqli_query($link, $query)  ;

/* check whethere there were matching records in the table
by counting the number of results returned */

     if(mysqli_num_rows($results) >= 1){
     echo '<table border="1">
     <tr>
     <th>rinex version</th>
     <th>program</th>
      <th>date</th>
     <th>maker name</th>
     <th>maker number</th>
      <th>observer</th>
      <th>agency</th>
     <th>position_X_Y_Z</th>
      </tr>';
     while($row = mysqli_fetch_array($results)){
     echo '<tr>
     <td>'.$row['rinex_version'].'</td>
     <td>'.$row['pgm'].'</td>
      <td>'.$row['date'].'</td>
      <td>'.$row['marker_name'].'</td>
        <td>'.$row['marker_no'].'</td>
         <td>'.$row['observer'].'</td>
        <td>'.$row['agency'].'</td>
      <td>'.$row['position_X_Y_Z'].'</td>

     </tr>';

       }
     echo '</table>';

      }else{

       echo "There was no matching record for the name " . $searchTerm;


        }

最佳答案

您可以尝试使用正则表达式:

SELECT * 
FROM rinexo 
WHERE rinex_file regexp '^$searchTerm([0][0-9]{2}|100)\.' 

如果 $searchTerm='iisc' 那么此代码将获取从“iisc001”开始的记录。最高可达“iisc100”。您需要引用$searchTerm(php函数preg_quote),这样任何特殊字符都不会进入查询。

查看有关 mysql 中正则表达式的更多信息 http://dev.mysql.com/doc/refman/5.1/en/regexp.html

但是正则表达式的工作速度不会那么快,如果可能的话,创建附加列,在其中放置您需要的整数值,例如:

1 for iisc001.10h
2 for iisc002.10h
100 for iisc100.10h

搜索词(在数字之前,如“iisc”)也有意义移动到单独的列并为它们创建索引,并且搜索将仅在它们上进行。

还有一种方法:

select rinex_file, 
       substr(rinex_file, 1, 4) as prefix,
       substr(rinex_file, 5, locate('.', rinex_file)-5) as digits,
       substr(rinex_file, -3, 2) as suffix,
       substr(rinex_file, -1) as postfix
from rinexo
where substr(rinex_file, 1, 4) like 'iis%'
  and substr(rinex_file, 5, locate('.', rinex_file)-5) like '0%'

关于php - 如何使用文件名获得多个搜索键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19809794/

相关文章:

php - 如何将一个变量的值分配给之前的另一个变量?

php - 在一个页面上组合多个 Controller ,而无需从 Controller 加载 Controller

php - 如何用两种不同的语言建立TCP/IP客户端服务器通信?

html - 无法定位 "back to top"按钮

html - Flexbox 布局在 Internet Explorer 10 中不起作用

mysql - 如何将 MaxMind GeoIP Free Country CSV 文件导入 MySQL 并节省磁盘空间

python - 在 Django 中使用 sql concat() 连接两列

php - 在 WooCommerce 管理订单列表的自定义列中显示每个订单项目的库存数量

php - MySQL 查询两个值之间的限制

html - 我的问题是将字体上传到网站