Jump to content

Recommended Posts

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 :).

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  :P

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?

 

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!  :)

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

Thanks a lot this worked!  :D

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

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.