我正在尝试按第一个字符整理我的内容。到现在为止,我做了类似的事情:
PHP:
$first_char = $_GET['search'];
$sql = "SELECT id
FROM table
WHERE SUBSTR(title,1,1) = '".$first_char."'"
....
HTML:
<a href="?search=a">[A]</a>
<a href="?search=b">[B]</a>
...
<a href="?search=z">[Z]</a>
<a href="?search=nr">[#]</a>
当点击[A]时,它会显示以“A”开头的标题。 我想要做的是当点击[#]时,它应该显示所有以数字开头的帖子。所以,当 $ _ GET ='nr'时,它显示所有以数字开头的标题......我试图用array()来做,但是我失败了。
你有什么建议吗?
答案 0 :(得分:7)
$sql = "SELECT id
FROM table
WHERE SUBSTR(title,1,1) in (1,2,3,4,5,6,7,8,9,0)"
答案 1 :(得分:3)
简单方法是使用regular expressions:
SELECT * FROM yourTable
where yourField REGEXP '^[[:digit:]].*$'
EDIT解释RegExp
^ Start string
[[:digit:]] digit one time
. everything
* zero or more times
$ End Of String
阅读评论以查看
.*$
没有必要:)
答案 2 :(得分:0)
<?php
if ($_GET['search'] == 'nr') {
$where = "REGEXP '^[[:digit:]]'";
} else {
$where = "= '" . mysql_real_escape_string($_GET['search']) . "'";
}
$sql = "SELECT .... FROM table WHERE $where";