Page 1 of 1

Displaying title alphabetically and using $_GET

Posted: Fri Sep 14, 2007 8:46 am
by kkonline
I am using the following code to display the list of articles.

Code: Select all

$sql = mysql_query("SELECT * FROM articles WHERE `trusted` = 0 AND `catid` = $catid ORDER BY `title` ASC LIMIT $from, $max_results");
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?

Posted: Fri Sep 14, 2007 8:50 am
by VladSun
You could use LIKE or SUBSTRING MySQL functions in your WHERE clause

Posted: Fri Sep 14, 2007 9:19 am
by kkonline
VladSun wrote:You could use LIKE or SUBSTRING MySQL functions in your WHERE clause
i used the following code but gives me parse errors?

Code: Select all

$sql = mysql_query("SELECT * FROM wow WHERE `trusted` = 0 AND `catid` = $catid AND left($row['title'],1) = `A` ORDER BY `title` ASC LIMIT $from, $max_results");

Posted: Fri Sep 14, 2007 9:35 am
by VladSun
When concatenating array variables to string you should use:

Code: Select all

"Bla-bla ".$array_var['key'];
or

Code: Select all

"Bla-bla {$array_var['key']}";
and not:

Code: Select all

"Bla-bla $array_var['key']";

Posted: Fri Sep 14, 2007 3:03 pm
by pickle
I don't see you using LIKE in that query anywhere.

Posted: Fri Sep 14, 2007 3:08 pm
by s.dot
When doing this before, I've just used it like this..

Code: Select all

if (!empty($_GET['letter']) && in_array($_GET['letter'], range('a', 'z')))
{
    $letter = $_GET['letter'];
} else
{
    $letter = false;
}

if ($letter)
{
    $result = mysql_query("SELECT `field` FROM `table` WHERE `title` LIKE '$letter%'");
}

Posted: Fri Sep 14, 2007 3:14 pm
by VladSun
Gosh ... now I realize what you have posted ...

Code: Select all

$sql = mysql_query("SELECT * FROM wow WHERE `trusted` = 0 AND `catid` = $catid AND left(`title`,1) = `A` ORDER BY `title` ASC LIMIT $from, $max_results"); 
This $row['title'] in the SQL query is very wrong ...