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
https://forums.phpfreaks.com/topic/187002-lookbooks-search-function/
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");

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!

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>';
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.