Forum Moderators: open

Message Too Old, No Replies

Displaying title alphabetically

using $_GET

         

kkonline

1:48 pm on Sep 14, 2007 (gmt 0)

10+ Year Member



I am using the following code to display the list of articles.
[php]$sql = mysql_query("SELECT * FROM articles WHERE `trusted` = 0 AND `catid` = $catid ORDER BY `title` ASC LIMIT $from, $max_results");
[/php]

It now displays a maximum of $max_results on one page as per the script and ascending order of title.
Now i want to display only those articles whose title start from 'a' or 'A'
Also to make the query dependent on the $_GET . means if i pass example.com/pages.php?alphabet=d then all the articles having title starting with d should be displayed.

How to write the query?

Discovery

10:13 am on Sep 15, 2007 (gmt 0)

10+ Year Member



its should be like,

[php]
$ArticleContain = $_GET['alphabet'];

$sql = mysql_query("SELECT * FROM articles WHERE (`trusted` = 0 AND `catid` = $catid) And title LIKE '".$ArticleContain."%' ORDER BY `title` ASC LIMIT $from, $max_results");
[/php]

The LIKE and NOT LIKE have two search helper symobls. The underscore _ character that looks for one character and the percentage % character that looks for zero or more characters.

dreamcatcher

2:09 pm on Sep 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use SUBSTRING. And no need to check for 'a' and 'A', just convert both input and query to lowercase beforehand:

$q = strtolower [php.net](strip_tags($_GET['alphabet']));

SELECT * FROM table WHERE SUBSTRING [dev.mysql.com](LOWER [dev.mysql.com](title),1,1)=='{$q}'...

dc