dongpo2002 2015-03-20 17:08
浏览 185
已采纳

想要通过开头的字母搜索结果,但显示包含该字母的所有内容

I coded my very first search filter by reading source codes and tutorials and now I have some kind of my own search filter. The code works, but the problem is that I can't find a way to make my code more precise: is how I want to use it.

These are the important parts of the code (I removed the echo codes and connection parts, because everything is OK with that):

<p><a href="?by=A">A</a> | <a href="?by=B">B</a> | <a href="?by=C">C</a> | <a href="?by=D">D</a> | <a href="?by=E">E</a> | <a href="?by=F">F</a> | <a href="?by=G">G</a> | <a href="?by=H">H</a> | <a href="?by=I">I</a> | <a href="?by=J">J</a> | <a href="?by=K">K</a> | <a href="?by=L">L</a> | <a href="?by=M">M</a> | <a href="?by=N">N</a> | <a href="?by=O">O</a> | <a href="?by=P">P</a> | <a href="?by=Q">Q</a> | <a href="?by=R">R</a> | <a href="?by=S">S</a> | <a href="?by=T">T</a> | <a href="?by=U">U</a> | <a href="?by=V">V</a> | <a href="?by=W">W</a> | <a href="?by=X">X</a> | <a href="?by=Y">Y</a> | <a href="?by=Z">Z</a></p>
<?php

if(isset($_GET['by'])){
    $letter=$_GET['by'];

    //-query the database table
    $sql="SELECT Name, Land FROM interpret WHERE Name LIKE '%" . $letter . "%'";

    //-run the query against the mysql query function
    $result=mysql_query($sql); 

    //ergebnisse zählen
    $numrows=mysql_num_rows($result);

    echo "<p>" .$numrows . " results found for " . $letter . "</p>"; 

    //loopy \(^_^)/
    while($row=mysql_fetch_array($result)){
        $Name =$row['Name'];
        $Land=$row['Land'];
    }
}

?>

I removed the connect to database code lines and the echo result lines because I doubt they matter here.

The best end result would be to have all results with A (and just with an A at the beginning) when pressing the button A in the search bar.

  • 写回答

3条回答 默认 最新

  • doutang1873 2015-03-20 17:10
    关注

    Extra % removed.

    $sql="SELECT Name, Land FROM interpret WHERE Name LIKE '" . $letter . "%'";
    

    It allowed any characters before, as well as after $letter. Now it'll only match any characters after it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?