Help With SQL Statement From URL Parameters
Posted: Fri Oct 19, 2012 2:04 am
I think that I need some help with the way I am piecing together a SQL statement from URL parameters. I'm using this statement to sort products by colors, styles, sizes, etc. based on whatever a user clicks. Say a user clicks on the color "berry" and then a style option of "short sleeve" and "sleeveless" - I need a sql statement that searches for a match in the color "berry" and "short sleeve" or the color "berry" and "sleeveless". The following code is what I've written and I have a feeling that it's a bad way to go.
This code does exactly what I need it to but it does throw out undefined variable notices. Does anyone know of a better way that I can achieve what I with this code or point me in the direction of starting new code that's more efficient? Are the nested foreach's a bad way to go?
Code: Select all
if (isset($_GET['color']))
$color_filter = $_GET['color'];
$color = explode(',', $color_filter);
if (isset($_GET['fit']))
$fit_filter = $_GET['fit'];
$fit = explode(',', $fit_filter);
if (isset($_GET['style']))
$style_filter = $_GET['style'];
$style = explode(',', $style_filter);
if (isset($_GET['fabric']))
$fabric_filter = $_GET['fabric'];
$fabric = explode(',', $fabric_filter);
if (isset($_GET['chamois']))
$chamois_filter = $_GET['chamois'];
$chamois = explode(',', $chamois_filter);
if (isset($_GET['ride']))
$ride_filter = $_GET['ride'];
$ride = explode(',', $ride_filter);
$conditions = '';
if (!empty($color_filter) || !empty($fit_filter) || !empty($style_filter) || !empty($fabric_filter) || !empty($chamois_filter) || !empty($ride_filter)) {
$i = 0;
foreach ($color as $color_value) {
foreach ($fit as $fit_value) {
foreach ($style as $style_value) {
foreach ($fabric as $fabric_value) {
foreach ($chamois as $chamois_value) {
foreach ($ride as $ride_value) {
if ($i == 0)
$conditions .= 'subcategory = "' . $sub . '"';
if ($i !== 0)
$conditions .= ' OR subcategory = "' . $sub . '"';
if (!empty($fabric_filter) && $fabric_value == 'solid' && $sub == 'tops')
$conditions .= ' AND name LIKE "%' . $fabric_value . '%" AND active = 1';
if (!empty($fabric_filter) && $fabric_value == 'print' && $sub == 'tops')
$conditions .= ' AND name NOT LIKE "%solid%" AND active = 1';
if (!empty($color_filter))
$conditions .= ' AND colors LIKE "%' . $color_value . '%" AND active = 1';
if (!empty($fit_filter))
$conditions .= ' AND fit = "' . $fit_value . '" AND active = 1';
if (!empty($style_filter))
$conditions .= ' AND style LIKE "%' . $style_value . '%" AND active = 1';
if (!empty($chamois_filter))
$conditions .= ' AND specs LIKE "%' . $chamois_value . '%" AND active = 1';
if (!empty($ride_filter))
$conditions .= ' AND ride LIKE "%' . $ride_value . '%" AND active = 1';
$i++;
}
}
}
}
}
}
$filter = $conditions;
}