woodplease Posted December 20, 2010 Share Posted December 20, 2010 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. Quote Link to comment Share on other sites More sharing options...
Rifts Posted December 20, 2010 Share Posted December 20, 2010 $sort=$_GET[orderby]; should be $sort=$_GET['orderby']; Quote Link to comment Share on other sites More sharing options...
woodplease Posted December 20, 2010 Author Share Posted December 20, 2010 sorry, typing error on my part, it already is $sort=$_GET['orderby']; so that isn't the problem Quote Link to comment Share on other sites More sharing options...
Rifts Posted December 20, 2010 Share Posted December 20, 2010 I don't believe this is a real statement ($sort=="[A-Z]") Quote Link to comment Share on other sites More sharing options...
woodplease Posted December 20, 2010 Author Share Posted December 20, 2010 well i'm not sure if it is or not, i do know that its similar to a regular expression, but i'm not quite sure how to use them Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 20, 2010 Share Posted December 20, 2010 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? Quote Link to comment Share on other sites More sharing options...
woodplease Posted December 20, 2010 Author Share Posted December 20, 2010 the $_Get['orderby'] returns a single letter, so i want to return all the values that begin with that letter Quote Link to comment Share on other sites More sharing options...
Rifts Posted December 20, 2010 Share Posted December 20, 2010 maybe try something like $regexp = "[A-z]"; if (preg_match($regexp, $sort)) { echo "is a valid letter"; } else { echo "NOT A LETTER"; } Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 21, 2010 Share Posted December 21, 2010 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. } Quote Link to comment Share on other sites More sharing options...
woodplease Posted December 21, 2010 Author Share Posted December 21, 2010 thanks very much, its working now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.