-1

Consider the case of any e-commerce website where we filter product results based on zero to 'n' number of constraining parameters such as price, availability, brand, etc where 'n' is the max possible number of allowed constraints. While doing so, we can add or remove choice of parameters as well (for e.g. removing the price constraint).

I am trying to make a small prototype for the above scenario using mysql and Php.

I understand that we will be playing around with nested queries and keep filtering selection of product ids (primary Key in this case). Final results can be displayed based on the resulting set of product ids.

I am still wondering what is the best way to get started on this. Any help will be appreciated.

Thanks!

5
  • 3
    Using the WHERE clause?! Commented Jun 3, 2013 at 10:53
  • ^ THIS with proper mapping of all possible constraints. Commented Jun 3, 2013 at 10:54
  • You'll pretty much be constructing SQL queries with varying WHERE clauses dynamically. What queries exactly and how exactly depends on your database structure. This question is too vague to have a real answer. Commented Jun 3, 2013 at 10:54
  • @AlexandreLavoie I realize that the 'where' clause has to be used. However, I am looking for ways to dynamically modify the sql query's where statement based on the user-selected constraining parameters. Commented Jun 3, 2013 at 11:02
  • You write code to do so. In its simplest form, you append strings to a query string. I'd suggest you start writing some code and ask a question if you get stuck; at the moment it sounds like you're overthinking it without actually having started. Commented Jun 3, 2013 at 11:07

2 Answers 2

1

Your answer gives the impression that you might need a query builder class in your project.

It'll help you achieve exactly what you intend and keep your code clean too.

If you're using framework such as Yii, it already provides this functionality, for standalone query builder classes, please try these:

  1. http://www.phpclasses.org/package/2813-PHP-Dynamically-build-SQL-queries.html
  2. https://code.google.com/p/php-query-builder/

Disclaimer, I have not tried or tested any of these.

Here's a code example from the second option in the list:

<?php
require_once './QueryBuilder.php';

$qb = QueryBuilder::create()
    ->select(array('column1', 'alias' => 'column2'))
    ->from('mytable as mt')
    ->join('another_table at', 'on', 'at.id = mt.fk_at_id')
    ->where('mt.status = 1')
    ->group('mt.group_field')
    ->order('mt.date_field desc')
    ->limit(15); 
// when printed, gives the following query:
// SELECT column1,column2 as alias
//  FROM mytable as mt
//  JOIN another_table as at on at.id = mt.fk_at_id
//  WHERE mt.status = 1
//  GROUP BY mt.group_field
//  ORDER BY mt.date_field as desc
//  LIMIT 15
Sign up to request clarification or add additional context in comments.

Comments

0

Transforming Selections to SQL

Basically you need a way to transform the checkbox selections into a where statement. A very basic example of this concept is described here: https://stackoverflow.com/a/14761616/1688441

Quoting (simplest example):

<?php
$location= $_GET['location'];
$language= $_GET['language'];
$name= $_GET['name'];

if($location!=""){
    $where[] = " `location` = '".mysql_real_escape_string($location)."'";
}
if($language!=""){
    $where[] = " `language` = '".mysql_real_escape_string($language)."'";
}
if($name!=""){
    $where[] = " `name` = '".mysql_real_escape_string($name)."'";
}
$where_clause = implode(' OR ', $where);
//same code for the other 2 options

$sql_json = "SELECT * FROM myTable WHERE $where_clause ORDER BY id DESC";
?>

If you want to make a more dynamic solution

You need to do two things:

  • Create an associative array where key is the field and the value is what to assign to the where statement. You could also use another data structure depending on your needs.
  • Write a function that takes the associative array and created the where statement to append to your SQL statement.

1 Comment

Also have a look at this: stackoverflow.com/a/4996633/1688441 from stackoverflow.com/questions/4996454/… . It's a good example!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.