Jump to content

LOOKBOOK's Search Function


Tranceflux

Recommended Posts

Hi everyone :D, consider the case below:

 

http://lookbook.nu/search

 

I want to build a search function like that for my ecommerce website but I've been getting lots of Search Engine tutorials on the web instead (eg. using a query string) while I had in mind displaying results based on categories.

 

http://lookbook.nu/search?gender=girls&material=cotton&colors[]=000000

 

I know lookbook uses $_GET to query their database, but how do they put the different options into 1 query string?

 

(eg. how does the script recognize it should be mysql_query("SELECT * FROM clothes WHERE gender = girls && material = cotton && colors = 000000 SORT BY date DESC")?

 

And another thing that puzzles me as well: Once you have selected on an option (eg. Gender: Girl), the link knows that it is selected and clicking it again will deselect it

 

(eg. http://lookbook.nu/search?gender=girls&material=cotton&colors[]=000000

becomes

http://lookbook.nu/search?material=cotton&colors[]=000000)

 

Any advise? Thanks ;D

Link to comment
Share on other sites

to create a link you would/could do something like this for the following url:

 

url: http://lookbook.nu/search?gender=girls&pattern=argyle

 

$query = ltrim($_SERVER['QUERY_STRING'], '?');
$query = ltrim($str, '?');

echo '<a href="/search.php?newVal=myval&'.$query.'">Link</a>';

 

Then for your query you could do something like this:

 

 

$query = ltrim($_SERVER['QUERY_STRING'], '?');
$query = ltrim($str, '?');
$where = '';
foreach(explode('&', $query) as $pairs){
list($key, $value) = explode('=', $pairs);
$key = mysql_real_escape_string($key);
$value = mysql_real_escape_string($value);
$where .= "$key = '$value',";
}
$where = rtrim($where, ",");
mysql_query("SELECT * FROM tbl WHERE $where");

Link to comment
Share on other sites

Hi The Little Guy,

 

Thanks for your reply! I believe it opened up a dead end for me.

 

I understood most of your awesome solution but I have some point of doubts:

 

1) $str

 

$query = ltrim($str, '?');

 

In this code, where do you get the $str from?

 

2) The link

 

echo '<a href="/search.php?newVal=myval&'.$query.'">Link</a>';

 

In this code, how does the link detects that its option has been selected and will change to "deselect" mode?

 

(eg. Current url on browser is http://lookbook.nu/search?gender=girls&pattern=argyle

Hovering on 'Agryle' gives you <a href="/search.php?gender=girls">Agryle</a> instead of <a href=?/search.php?gender=girls&pattern=agryle)

 

PS: Will credit you on my website!

Link to comment
Share on other sites

1) Oops sorry, in both of my codes line 2 should be deleted.

 

2) to remove/change a value in the string, you could do something like this:

 

to change:

<?php
$str = 'gender=girls&color=red&pattern=pattern1';
$info = array(
'gender' => array('guys', 'girls'),
'color' => array('red', 'white', 'blue'),
'pattern' => array('pattern1', 'pattern2', 'pattern3')
);

$arr = array();
parse_str($str, $arr);

$opt = '';
echo "<p>$str</p>";
foreach($info as $key => $val){
foreach($val as $item){
	$newStr = $str;
	if(preg_match("~$key~", $newStr)){
		if(in_array($item, $arr)){
			$newStr = preg_replace("~$key=.+?(&|$)~", "", $newStr);
		}else{
			$newStr = preg_replace("~$key=.+?(&|$)~", "$key=$item&", $newStr);
		}
	}
	echo '<div>';
	echo "<span style='display:block;float:left;width:100px;'>item $item:</span> ".trim($newStr, '&');
	echo '</div>';
}
}
?>

Link to comment
Share on other sites

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.