php - 如何在数据表中重音中和后在数据库中进行 jquery 搜索 - 不起作用

标签 php jquery mysql datatables

您好,我有带有数据表 jquery 插件的表。我在页脚的 index.php 中添加了脚本(使用开关加载页面)以中和重音,因为我想在没有变音符号的情况下进行搜索。 此脚本不适用于数据库中的字段,但是当我添加到回显新行时 - 所以在这一行中一切正常。

这是我的正文代码的一部分:

  $stmt = sqlsrv_query( $conn, $sql );
  if( $stmt === false) {
      die( print_r( sqlsrv_errors(), true) );
  }
  echo "<div class='container'>

        <h5>Heading</h5>


      <table id='tabulka_kariet' class='table table-bordered'>
        <thead>
          <tr>
            <th>Kód karty</th>
            <th>Názov karty</th>
            <th>Špec 1 </th>
            <th>Špec 2 </th>
            <th>Špec 3 </th>
            <th>Špec 4 </th>


          </tr>
        </thead>

        <tfoot>
          <tr>
            <th>Kód karty</th>
            <th>Názov karty</th>
            <th>Špec 1 </th>
            <th>Špec 2 </th>
            <th>Špec 3 </th>
            <th>Špec 4 </th>

          </tr>
        </tfoot>
      <tbody>
                  <tr>
              <td>mála</td>
              <td>očivá</td>
              <td>lólo</td>
              <td>šesť</td>
              <td>nedeľa</td>
              <td>ľščťžýáíé</td>
            </tr>";


   while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  echo "<tr>";

  echo "<td>" . $row['Code'] . "</td>";
  echo "<td><a href=\"detail.php?id=" . $row['Id'] . " \">" . $row['Name'] . "</td>";
  echo "<td>" . $row['Specification'] . "</td>";
  echo "<td>" . $row['Specification2'] . "</td>";
  echo "<td>" . $row['Specification3'] . "</td>";
  echo "<td>" . $row['Specification4'] . "</td>";

  echo "</tr>";
  }
  echo "</tbody></table>";


   sqlsrv_free_stmt( $stmt);

  ?>

这是页脚的脚本:

      <script>
      $(document).ready( function () {
      jQuery.fn.DataTable.ext.type.search.string = function ( data ) {
         return ! data ?
          '' :
          typeof data === 'string' ?
              data
                  .replace( /έ/g, 'ε' )
                  .replace( /[ύϋΰ]/g, 'υ' )
                  .replace( /ό/g, 'ο' )
                  .replace( /ώ/g, 'ω' )
                  .replace( /ά/g, 'α' )
                  .replace( /[ίϊΐ]/g, 'ι' )
                  .replace( /ή/g, 'η' )
                  .replace( /\n/g, ' ' )
                  .replace( /[áàäâ]/g, 'a' )
                  .replace( /[éÉěĚèêëÈÊË]/g, 'e')
                  .replace( /[íìïî]/g, 'i' )
                  .replace( /[óòöô]/g, 'o' )
                  .replace( /[ùüûúÚůŮ]/g, 'u' )
                  .replace( /[ľĽ]/g, 'l')
                  .replace( /[šŠ]/g, 's')
                  .replace( /[čČçÇ]/g, 'c')
                  .replace( /[řŘ]/g, 'r')
                  .replace( /[ťŤ]/g, 't')
                  .replace( /[žŽ]/g, 'z')
                  .replace( /[ýÝ]/g, 'y')
                  .replace( /[ďĎ]/g, 'd')
                  .replace( /[ňŇ]/g, 'n') :

              data;
  };
    var table = $('#tabulka_kariet').DataTable();

  } );
  </script>

2.问题是,当空字段带有空格时,该脚本将无法运行。因此,在该列中,此搜索不起作用。也许会出现问题,所以问题是,如何防止数据库出现这种情况......?对每个字段使用 if 条件?有关此问题的更多信息 here

我的同类question几天前 感谢您的回答:)

最佳答案

我回答了另一个问题,但我也在这里写下我的答案。

为了解决重音中和插件的问题,我添加了与 jQuery.fn.DataTable.ext.type.search.string 添加到 jQuery.fn.DataTable 相同的方法.ext.type.search.html.

您可以在这里看到:https://jsfiddle.net/ghsmaniotto/24n05tas/

我是按照https://github.com/DataTables/Plugins/blob/master/filtering/type-based/accent-neutralise.js做的

关于php - 如何在数据表中重音中和后在数据库中进行 jquery 搜索 - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46665426/

相关文章:

java - 结果集 "column not found"与 c3p0

php - 关键字 mysql 搜索返回不准确的结果

javascript - 通过目录在预加载页面上循环(间隔)图片

javascript - 如何使用jquery将一个元素转换为2个元素

mysql - 根据任意列插入或更新

mysql - 我无法让查询从我的产品表中提取所有记录

php - Jquery .hover 不工作

php - 包含名称和 ID,但仅考虑 ID 的 Wordpress 永久链接

javascript - 如何创建 html 表格周 View ? (日历)

jquery - 页面一部分的 html2canvas 屏幕截图