Choose where the first character is the number

I am trying to organize my content as the first character. So far, I have done something like this:

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> 

The fact is that when you press [A], it displays headings starting with "A". What I want to do is press [#] so that it displays all messages starting with a number. So, when $ _ GET = 'nr' displays all the headers starting with a number ... I tried to do this with array (), but I failed.

Do you have any suggestions?

+4
source share
3 answers
 $sql = "SELECT id FROM table WHERE SUBSTR(title,1,1) in (1,2,3,4,5,6,7,8,9,0)" 
+7
source

A simple way is regular expressions :

 SELECT * FROM yourTable where yourField REGEXP '^[[:digit:]].*$' 

EDIT Explains RegExp

  ^ Start string [[:digit:]] digit one time . everything * zero or more times $ End Of String 

Read the comments to see what

  .*$ 

not required:)

+3
source
 <?php if ($_GET['search'] == 'nr') { $where = "REGEXP '^[[:digit:]]'"; } else { $where = "= '" . mysql_real_escape_string($_GET['search']) . "'"; } $sql = "SELECT .... FROM table WHERE $where"; 
0
source

All Articles