Jump to content

if statement (validating character)


woodplease

Recommended Posts

i'm trying to an if statement so that a piece of code is only executed if a returned variable equals a letter in the alphabet(any letter)

the variable is from a $_GET, and all i want to check is if the variable contains a letter.

This is what i have so far, but it doesnt work

 

$sort=$_GET[orderby];
if ($sort=="[A-Z]") {
$read=mysql_query("SELECT * FROM films WHERE title LIKE '".$sort."%'") or die(mysql_error());
$result=mysql_num_rows($read);
}
else { 
$read=mysql_query("SELECT * FROM films ORDER BY title") or die("query failed");
$result=mysql_num_rows($read);
}

Any help would be great.

Link to comment
Share on other sites

the current logic is:

 

if the value passed to the script, $_GET['orderby'] is equal to "[A-Z]", then execute this SQL: SELECT * FROM `films` WHERE title LIKE '[A-Z]%'

 

which won't return any records except where titles start with [A-Z], probably zero.

 

I don't think that will get you what you want. Do you mean to take the value of $_GET['orderby'], and if it is equal to "[A-Z]", then return all titles that begin with a letter? or do you mean to return all results ordered alphabetically by title? or something else?

Link to comment
Share on other sites

Still not so sure about the logic you're using, but according to the specs you gave, this should be what you want. This will only validate if $GET['orderby'] contains a single alphabetic character.

 

if( ctype_alpha($_GET['orderby']) && strlen($_GET['orderby']) === 1 ) {
     // meets the criteria.
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.