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. Link to comment https://forums.phpfreaks.com/topic/222245-if-statement-validating-character/ 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']; Link to comment https://forums.phpfreaks.com/topic/222245-if-statement-validating-character/#findComment-1149651 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 Link to comment https://forums.phpfreaks.com/topic/222245-if-statement-validating-character/#findComment-1149653 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]") Link to comment https://forums.phpfreaks.com/topic/222245-if-statement-validating-character/#findComment-1149657 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 Link to comment https://forums.phpfreaks.com/topic/222245-if-statement-validating-character/#findComment-1149662 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? Link to comment https://forums.phpfreaks.com/topic/222245-if-statement-validating-character/#findComment-1149664 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 Link to comment https://forums.phpfreaks.com/topic/222245-if-statement-validating-character/#findComment-1149665 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"; } Link to comment https://forums.phpfreaks.com/topic/222245-if-statement-validating-character/#findComment-1149666 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. } Link to comment https://forums.phpfreaks.com/topic/222245-if-statement-validating-character/#findComment-1149762 Share on other sites More sharing options...
woodplease Posted December 21, 2010 Author Share Posted December 21, 2010 thanks very much, its working now Link to comment https://forums.phpfreaks.com/topic/222245-if-statement-validating-character/#findComment-1149859 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.