【问题标题】:Sorting a sql database alphabetically with links/buttons使用链接/按钮按字母顺序对 sql 数据库进行排序
【发布时间】:2017-04-05 11:59:47
【问题描述】:

脚本朋友们好,

我正在做一个项目,我需要按字母顺序对一行进行排序。在搜索了一些解决方案后,我没有找到我想要的解决方案。假设我只需要以字母“F”开头的产品。我想要如下图:https://gyazo.com/488ccc8fd2beaadbc96d687938e5a6ff

我需要能够点击 F 并让它只显示以 F 开头的产品。

我拥有的代码可以解决问题,但我需要更改代码才能获得另一个字母。这是我到目前为止的代码:

<?php
require_once 'config.php';

try {
    $sQuery = "SELECT receptnummer, receptnaam, receptingredient, receptbereidingstijd, receptbereidingswijze, receptcategorie, receptbenodigdheden, receptserveertips, klantnummer
     FROM recept
     WHERE receptnaam LIKE '%f%' ORDER BY receptnaam";

    $oStmt = $db->prepare($sQuery);

    $oStmt->execute();

    if ($oStmt->rowCount() > 0) {
        echo '<table border="2">';
        echo '<thead>';
        echo '<td>receptnummer</td>';
        echo '<td>receptnaam</td>';
        echo '<td>receptingredient</td>';
        echo '<td>receptbereidingstijd</td>';
        echo '<td>receptbereidingswijze</td>';
        echo '<td>receptcategorie</td>';
        echo '<td>receptbenodigdheden</td>';
        echo '<td>receptserveertips</td>';
        echo '<td>klantnummer</td>';

        while ($aRow = $oStmt->fetch(PDO::FETCH_ASSOC)) {

            echo '<tr>';
            echo '<td>' . $aRow['receptnummer'] . '</td>';
            echo '<td>' . $aRow['receptnaam'] . '</td>';
            echo '<td>' . $aRow['receptingredient'] . '</td>';
            echo '<td>' . $aRow['receptbereidingstijd'] . '</td>';
            echo '<td>' . $aRow['receptbereidingswijze'] . '</td>';
            echo '<td>' . $aRow['receptcategorie'] . '</td>';
            echo '<td>' . $aRow['receptbenodigdheden'] . '</td>';
            echo '<td>' . $aRow['receptserveertips'] . '</td>';
            echo '<td>' . $aRow['klantnummer'] . '</td>';
            echo '</tr>';
        }
        echo '</table>';
    } else {
        echo 'helaas, geen gegevens gevonden';
    }

} catch (PDOException $e) {
    $sMsg = '<p>
    Regelnummer: ' . $e->getLine() . '<br/>
    Bestand: ' . $e->getFile() . '<br/>
    Foutmelding: ' . $e->getMessage() . '
</p>';

    trigger_error($sMsg);
}
$db = null;
?>

有人可以帮我解决这个问题吗???

提前致谢,

数字牙医

【问题讨论】:

  • 如果某些内容以“F”开头,则删除'%f%'中的第一个百分号
  • 是的,fred 是对的 %f% 表示前面的任何内容,然后是 'f',如果使用 like 命令,则表示后面的任何内容
  • 在点击一个字母之前,所有的记录都已经显示在客户端了吗?如果是这样,似乎过滤客户端会更好,因为不需要再次访问服务器。
  • 但是如何将其他变量链接到具有该特定字母的链接或按钮?

标签: php html sql alphabetical


【解决方案1】:

应用此查询

 $sQuery="SELECT receptnummer, receptnaam, receptingredient, receptbereidingstijd, receptbereidingswijze, receptcategorie, receptbenodigdheden, receptserveertips, klantnummer
 FROM recept
 WHERE receptnaam LIKE 'f%' ORDER BY receptnaam";

【讨论】:

    【解决方案2】:

    整个技巧都在这个查询中:

    $sQuery="SELECT receptnummer, receptnaam, receptingredient, receptbereidingstijd, receptbereidingswijze, receptcategorie, receptbenodigdheden, receptserveertips, klantnummer
     FROM recept
     WHERE receptnaam LIKE '%f%' ORDER BY receptnaam";
    

    您需要做的就是将LIKE '%f%' 中的f 替换为一个变量,该变量可以从POST/GET 请求中获取。

    编辑:正如其他人所提到的,您可能应该将您的 %f% 替换为 f%,除非您正在寻找仅包含字母 f 的单词,而不仅仅是以字母 f 开头。

    【讨论】:

    • 但是有没有办法让字母“A”用 A 显示所有内容,而“B”用 B 显示所有内容等等。就像在这样的菜单中:“A | B | C | D | ...”?
    • @DigitalDentist 我不确定我是否理解正确。是否需要按字母顺序显示整个表格的内容?如果是这样,您将不得不运行多个查询。创建一个字符串$alphabet = 'abcdefghijklmnopqrstuvwxyz',然后将代码放入该字符串中每个字符的 FOR 循环中。不要忘记在每次查询后做mysqli_next_result($database);,这样你就可以发送一个新的
    • 我自己已经弄明白了 ;) 无论如何感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多