Jump to content

How can I include white spaces in search query?


CStrauss
 Share

Recommended Posts

okay I have a little problem with trying to get correct information out of my database. I have a field called tags which has keywords basicly for example one row my have this in the tags field.Aiko 3,Victoria,Victoria 3,A3,V3,Clothing,TexturesNow values are passed via links used to query the database and display all the information based on what keywords or (tags) it finds. Here is my code im using.

		// Display All Items For Selected Item	$search_array = array($_GET['item']);		// Query Database	$sql = mysql_query("SELECT * FROM poser_items WHERE itemType=$itemType");if (!sql) {	echo "Error Performing MySQL Query: " . mysql_error();} else { 	while($row=mysql_fetch_assoc($sql)){		foreach($row as $item){			foreach($search_array as $search){				if(stripos($item,$search) !== false){					stripslashes(extract($row));					echo "<p>$item_name</p>";				}			}		}			}}

Now for the most part it works as far as pulling the data out of the database except that for example it treats "Victoria" the same as "Victoria 3".Now what I can conclude is that when its doing when it does the search and its evaluating Victoria 3 it stops after the a then goes on to the next word in this case its 3 and 3 is not a match. Get what im saying.So my question is how can I fix this to include the white space in a word like Victoria 3. Now an easy solution might be to go change all my data and remove the white space. But this is something interesting I might want to learn about for future uses.So what are my best options here to make my search work?

Link to comment
Share on other sites

This is what you are using to check:if(stripos($item,$search) !== false){stripos (and strpos) check if a substring is inside another string. Since "Victoria" is in "Victoria 3", the check succeeds. But that would make sense, if you are searching for the word "Victoria", then you would want "Victoria 3" to show up as well as "Victoria". If you only want to check for a specific word, then just do that explicitly:if ($item == $search)

Link to comment
Share on other sites

Hmmmm I changed my code as you suggested and that didnt seem to work, your explination makes perfect sence to me but I changed my code replacing:if(stripos($item,$search) !== false){With:if($item==$search){And now I get no results when before I got something regardless they were incorrect. lol neither is acceptable but I rather see something then nothing when putting something together if you know what i mean,Any how here is my database scheme with two rows of sample data maybe it will help me get help.poser_item

item_id				  item_name				   item_type	 vendor		  tags-------			  ------------					-----------	 ---------		 -----1					 Victoria Cloths					 1		   nobody		 Victoria,V1,V2,Clothing,Texture2					 Victoria 3 Cloths				  1		   somebody	 Victoria 3,V3,Clothing,Texture3					 Nice Cloths						1			nobody		  Aiko 3,Victoria 3,V3,A3,Clothing,Texture

Thats the basics of my table. Now for my full code:

<?phpinclude $_SERVER['DOCUMENT_ROOT'].'/inc/database.php';$req = (!isset($_REQUEST['req'])) ? 'default' : $_REQUEST['req'];switch($req){	// 1=Figures 2=propscase "1":	$sql = mysql_query("SELECT * FROM sub_cat_table WHERE itemType=$req ORDER BY sub_catName ");	if(!$sql){		echo "Error Performing MySQL Query:".mysql_error();	}else{		while($row=mysql_fetch_assoc($sql)){			stripslashes(extract($row));			echo '<div style="display:inline;"><a href="inventory.php?req=display&itemType='.$itemType.'&item='.$_GET['item'].'&cat='.$sub_catName.'">'.$sub_catName.'</a> </div>';		}	}		// Display All Items For Selected Item	$search_array = array($_GET['item']);	echo $search_array[0];	//echo $itemType;	// Query Database	$sql = mysql_query("SELECT * FROM poser_items WHERE itemType=$itemType");if (!sql) {	echo "Error Performing MySQL Query: " . mysql_error();} else { 	while($row=mysql_fetch_assoc($sql)){		foreach($row as $item){			foreach($search_array as $search){				//if(stripos($item,$search) !== false){				  if($item ==$search){					stripslashes(extract($row));					echo "<p>$item_name</p>";				}			}		}			}}		break;case "2":			break;case "display":break;default:echo "<div style>";	// Generate Inventory Links$sql = mysql_query("SELECT * FROM category_table ORDER BY cat_name");while($row=mysql_fetch_assoc($sql)){	stripslashes(extract($row));	echo '<a href="inventory.php?req='.$itemType.'&item='.$cat_name.'" style="display:inline;">'.$cat_name.'</a> ';}	// Display All Items In Inventoryecho "</div>";	break;} // End Switch

As you can see the values of the tags(keywords) its searching upon are comming via links. Maybe there is a better way to set up my database to make this easier or im just over thinking or someting. but changing the statment to if($item==$search) gave me zero results. Basicly what im trying to do is create a inventory system of some files i have and use often. so when it first loads its shows a serious of links such as Victoria Victoria 3 Aiko 3 and so on....Then when you click a link it passes some variables in this case the item type and the item name so for example it will pass 1 for the $itemType and Victoria 3 for the $itemThen more links display like this from the case 1:Clothing, Characters, Props ect as links then those values will be passed but i havent gotten that far yet this is where im at in my code where on this page it displays all items that relate to the $item.lol Follow me so far.So do i need to completely modify my code to use the if($item==$search){?Or do you see a better way to achieve what i tried regular expressions and it came up with the same problem the space between the a the 3 in "Victoria 3"

Link to comment
Share on other sites

Stick quotes around what my variables? they are already of type string when they are passed via the link i thought?What im trying to do if click on the link Victoria 3 it passes a varaible $item as Victoria 3 and itemType = 1 then performs a query and searches through the tags field for all items with Victoria 3. then display the item_name of those items.so im not sure what you mean by stick quotes around.

Link to comment
Share on other sites

Yes, they are already strings, but that doesn't matter for this because we are only comparing the values, not the types. This is a pretty straight-forward piece of code, so I doubt there is anything subtle that would be causing this. I'm thinking it's probably a case-sensitivity issue. stripos is case-insensitive, but with $item==$search, Victoria != victoria. So, try converting both of them to lowercase.if(strtolower($item) == strtolower($search)){

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...