Displaying title alphabetically and using $_GET

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Displaying title alphabetically and using $_GET

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You could use LIKE or SUBSTRING MySQL functions in your WHERE clause
There are 10 types of people in this world, those who understand binary and those who don't
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post 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");
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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']";
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I don't see you using LIKE in that query anywhere.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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%'");
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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 ...
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply