Jump to content

[PHP] Coding Help


Travis1128

Recommended Posts

Hello guys it's me again. I have not posted a question in awhile so I am glad to be on PHPFreaks again. Anyways here is my question,

 

I have simple "Filter By" or "Refine By" script I am developing. It's foundation is primarily database (MySQL) and does not use XML or other table files. This script simply add's filtered array ("objects") to the url as the customer filters his/her search. An example,

 

$attributeCodes("processor_type","color","memory"); // These arrays are defined and built from the database on the time of the request. So they are automatically updated.

 

The above array is used in an if/and/or statement to look for these attribute codes. If it finds it and it has a value it then refines the MySQL query to look for products with the said attribute code.

 

Now that we have the basics out there of how it works I am at the part where it confuses me.

 

I give the user the ability to filter his/her search, however I want the user to be able to remove these filters separately if wanted.

 

So for example the customer filters,

 

Domain.com/search/productsearch.php?catId=112&processor_type=AMD&memory=2gb

 

The customer does not need the memory filter and wishes to remove that specific item or vice-verse he/she does not want to search by processor_type. How is it that I can remove specifically one attribute and its value from the URL? So the url will then look like,

 

Domain.com/search/productsearch.php?catID=112&processor_type=AMD or

Domain.com/search/productsearch.php?catID=112&memory=2gb

 

Any help would be great. Or if you have any suggestions on a different method to filter by attributes products. Suggestions and criticism are welcome. Please do not be shy or modest when it comes to giving me advice as I am trying to learn efficiently.

 

- Travis

Link to comment
Share on other sites

Thank you for your reply.

 

My setup for this example looks like this,

 

<?php

error_reporting(0);

// Filter Arrays
$filter = Array("demo","demo2","demo3");

 

As you can see I am trying to attempt to find if the filter values in the array above are being used. Example,

 

www.Domain.com/search/productsearch.php?catId=112&demo=test

 

The customer then selects another filter and the url looks like this now,

 

www.Domain.com/search/productsearch.php?catId=1123&demo=test&demo2=test

 

I could not really get your method to apply to my script, and I was curious if you knew anything about how to implement it. Basically I am trying to attempt this,

 

Filtered Search

- Filter the products by filtering attributes.

- Allow the customer to remove/add filters based on their needs.

 

I was thinking as well, perhaps cookies or sessions would work with filtering data? I am not sure.

 

- Travis

Link to comment
Share on other sites

An example of how you can remove a filter -

<?php
// if the filter is present in the query-string, make a 'remove' link that will request the current page without that choice.

$filter = Array("demo","demo2","demo3");

foreach($filter as $choice){
if(isset($_GET[$choice])){
	$get = $_GET; // make a copy of the GET keys/values
	unset($get[$choice]); // remove the choice from the get keys/values
	$remove_links[] = "<a href='?".http_build_query($get, '', '&')."'>$choice</a>"; // make a link w/o the choice
}
}

if(!empty($remove_links)){ // build the 'remove' navigation
$remove_nav = "Remove Filter: " . implode(' | ',$remove_links);
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>Filter example</title>
<style type="text/css">
</style>
</head>
<body>
<div>
<?php echo isset($remove_nav) ? $remove_nav : ''; ?>
</div>
<div>
<p>Other content on your page...</p>
</div>
</body>
</html>

 

Link to comment
Share on other sites

Thank you for your code. I am having a bit of an issue with another piece of code in relation to this.

 

private function displayOptions($atID){
	$this->mysqlConnect();
	$sql = "SELECT *
			FROM pcs_attribute pa, pcs_attribute_option pao
			WHERE pao.attribute_id = '$atID'
			AND pa.attribute_id = '$atID'";
	$query = $this->mysqlQuery($sql);
	print('<div class="attribute_items">');
	while($fetch = $this->mysqlFetch($query)){

                       // Code Input

	}
	print('</div>');
}

 

Basically I am trying to get this script to post a link containing the data of the example below,

 

www.domain.com/category.php?catID=1 - This is the default

 

But when the user clicks on "Processor Type -> AMD" I want it to take them too

 

www.domain.com/category.php?catID=1&processor_type=amd

 

Sounds simple right? This is where it gets tricky for me. I also am trying to create a no duplication protection from it which I am having the most trouble on. So in the example below you wont get this,

 

www.domain.com/category.php?catID=1&processor_type=amd&processor_type=amd

 

if the user can click on "processor type" still. I also am trying to make the "filters" stackable when clicked. Example,

 

www.domain.com/category.php?catID=1&processor_type=amd

 

then the user clicks a link and it changes

 

www.domain.com/category.php?catID=1&processor_type=amd&memory_size=3gb

 

If you or anyone else could help that would be great. If you have any more questions please do not hesitate to ask. I will continue to work on it and experiment and post back if I do find the solution. Thank you.

Link to comment
Share on other sites

Here is a hint - You would use code similar to the remove filter example, but compliment the logic. If the filter is not present in the current query-string, you would make a menu section for each choice by setting the get key/value, rather than un-setting it.

 

<?php
// create filter links -
// if the filter is not present in the query-string, make a menu section with 'add' links for each possible choice

$filters['processor_type'][] = 'amd'; // dummy data for demo purposes
$filters['processor_type'][] = 'intel';
$filters['memory_size'][] = '2gb';
$filters['memory_size'][] = '4gb';
$filters['memory_size'][] = '6gb';

$add_nav = ''; // build 'add' navigation
foreach($filters as $key => $array){
if(!isset($_GET[$key])){
	// the key is not present in the current query-string, list the choices
	$add_nav .= "<h4>".ucwords(str_replace('_',' ',$key))."</h4>\n<ul>\n";
	foreach($array as $choice){
		$get = $_GET; // make a copy of the GET keys/values
		$get[$key] = $choice; // add the choice to the get keys/values
		$add_link = "<a href='?".http_build_query($get, '', '&')."'>$choice</a>"; // make a link with the choice
		$add_nav .= "<li>$add_link</li>\n";	
	}
	$add_nav .= "</ul>\n";
}
}

Link to comment
Share on other sites

Thanks your for reply. Your script worked however this is how I am trying to implement it.

 

private function displayOptions($atID){
	$this->mysqlConnect();
	$sql = "SELECT *
			FROM pcs_attribute pa, pcs_attribute_option pao
			WHERE pao.attribute_id = '$atID'
			AND pa.attribute_id = '$atID'";
	$query = $this->mysqlQuery($sql);
	print('<div class="attribute_items">');
	while($fetch = $this->mysqlFetch($query)){
		// This is where the attribute options are retrieved and displayed

	}
	print('</div>');
}

private function displayAttributes($setID){
	$this->mysqlConnect();
	$sql = "SELECT *
			FROM pcs_attribute
			WHERE attribute_set_id = '$setID'
			OR attribute_is_global = '1'
			ORDER BY attribute_set_id ASC";
	$query = $this->mysqlQuery($sql);
	print('<div class="filter_by">Filter By</div>');
	while($fetch = $this->mysqlFetch($query)){
		// This is where the attributes display

		// This is the options
		$this->displayOptions($fetch['attribute_id']);
	}
}

 

So function displayAttributes(); is the function that displays the attributes within the database example,

 

- Processor Type

- Memory Size

- Color

 

And displayOptions(); is the function that displays the attributes within the attribute_options database. Example,

 

- AMD

- Intel

- 3Gb

- 4GB

- 5GB

- Red

- Blue

 

Each table also is assigned to the attribute above ("processor_type", "memory_size", "color") through the attribute_id. This is a numerical entity (i.e. 1,2,3) and really has no relation but felt I should mention it.

 

There is a final function called displayFilters();

 

public function displayFilters(){
	// Contsruct HTML
	$this->buildHtml();
	// Connect the MySQLa
	$this->mysqlConnect();
	// Check the Category to verify it is correct.
	if($this->checkCategory($this->mysqlEscape($_GET['catID']))){
		// The result is returned true.
		// Now lets begin to filter and find products within he attributes database.
		if($this->checkCategorySet($this->mysqlEscape($_GET['catID']))){
			// The result is returned true.
			$this->displayAttributes($this->getCategoryInfo($this->mysqlEscape($_GET['catID']), 'attribute_set_id'));
		} else {
			// The result is returned false.
			// This means there is no set assigned to this category
			print($this->message('1011'));
		}
	} else {
		// The result is returned false.
		print($this->message('1010'));
	}
}

 

This is where it displays the final build of everything.

 

If you have any questions please ask. I am working hard on this as well and I appreciate your help.

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.