paymentstv Posted July 13, 2011 Share Posted July 13, 2011 Hello All, I want to compare a variable ($var) that I have to a database entry ($list) that is comma separated. If any of the entries in $list does not match $var, then I want to carry out some other operations. At the moment, my database entry is a single entry (not comma separated) so I just use the following to find out if it match my variable. if($list!="$var"){} Can you please help me figure out how to go about this if the database entry is a comma separated value? Thanks for any help . Quote Link to comment https://forums.phpfreaks.com/topic/241918-how-can-i-compare-a-variable-to-a-comma-separated-database-entry/ Share on other sites More sharing options...
teynon Posted July 13, 2011 Share Posted July 13, 2011 $vars=explode(",", $list); if (in_array($var, $vars)) { // Var is in array } Quote Link to comment https://forums.phpfreaks.com/topic/241918-how-can-i-compare-a-variable-to-a-comma-separated-database-entry/#findComment-1242362 Share on other sites More sharing options...
AyKay47 Posted July 13, 2011 Share Posted July 13, 2011 you will want to use explode after you extract the db data.. $subject = "this,is,a,test,string" $sub_strings = explode(",",$subject); if(in_array($var,$subject)){ //do something }else{ print "$var is not in the array"; } Edit: well, he beat me to it Quote Link to comment https://forums.phpfreaks.com/topic/241918-how-can-i-compare-a-variable-to-a-comma-separated-database-entry/#findComment-1242363 Share on other sites More sharing options...
PFMaBiSmAd Posted July 13, 2011 Share Posted July 13, 2011 Or you could just do this in your query (find if a value is in a list)- http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_find-in-set Quote Link to comment https://forums.phpfreaks.com/topic/241918-how-can-i-compare-a-variable-to-a-comma-separated-database-entry/#findComment-1242364 Share on other sites More sharing options...
paymentstv Posted July 13, 2011 Author Share Posted July 13, 2011 Thanks for all the help and I got the general idea of how to go about this. However, while trying to test it I face another obstacle. At the moment I can not enter a comma seperated value into my form because I have the following code allowing me only letters, numbers, "-",".", else if( privateurl.search("[^A-Za-z0-9\-.:/ ]") >= 0) { alert("Only characters and numbers allowed for URL!.") ; document.getElementById( "privateurl" ).focus() ; return false ; privateurl is the variable I hope to store in my database as a comma separated value. Does any one know how can modify above code so that I can allow a comma? Quote Link to comment https://forums.phpfreaks.com/topic/241918-how-can-i-compare-a-variable-to-a-comma-separated-database-entry/#findComment-1242372 Share on other sites More sharing options...
paymentstv Posted July 13, 2011 Author Share Posted July 13, 2011 else if( privateurl.search("[^A-Za-z0-9\,-.:/ ]") >= 0) solved that issue..testing the solutions now. Quote Link to comment https://forums.phpfreaks.com/topic/241918-how-can-i-compare-a-variable-to-a-comma-separated-database-entry/#findComment-1242373 Share on other sites More sharing options...
paymentstv Posted July 13, 2011 Author Share Posted July 13, 2011 Hell again, It is working when I use the following code, but there is a small issue. If an entry has a space after the comma it will not identify that entry against my $var. i.e in a list of X, Y,Z when I search for Y it returns 0 because there is a space before Y. Is there a method where I can remove any space in the $list? $vars=explode(",", $list); if(in_array($var, $vars))==0{Do this} Thanks for all the help so far, I can use it as it is but would be nice if I don't have to tell my visitors not to enter a space..etc b/c they never listen to me! Quote Link to comment https://forums.phpfreaks.com/topic/241918-how-can-i-compare-a-variable-to-a-comma-separated-database-entry/#findComment-1242383 Share on other sites More sharing options...
AyKay47 Posted July 13, 2011 Share Posted July 13, 2011 you will want to use trim here $vars=explode(",", $list); foreach($vars as $value){ trim($value); } if(in_array($var, $vars)){//do something} this should give you the desired results Quote Link to comment https://forums.phpfreaks.com/topic/241918-how-can-i-compare-a-variable-to-a-comma-separated-database-entry/#findComment-1242391 Share on other sites More sharing options...
paymentstv Posted July 13, 2011 Author Share Posted July 13, 2011 Thanks a lot this worked! you will want to use trim here $vars=explode(",", $list); foreach($vars as $value){ trim($value); } if(in_array($var, $vars)){//do something} this should give you the desired results Quote Link to comment https://forums.phpfreaks.com/topic/241918-how-can-i-compare-a-variable-to-a-comma-separated-database-entry/#findComment-1242400 Share on other sites More sharing options...
AyKay47 Posted July 13, 2011 Share Posted July 13, 2011 excellent, please mark this as solved....lower left hand of the thread.. Quote Link to comment https://forums.phpfreaks.com/topic/241918-how-can-i-compare-a-variable-to-a-comma-separated-database-entry/#findComment-1242407 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.