b0byan
asked on
Sorting parameter arrays and filtering mysql query
Hi,
I have an mysql query which need to be filtered by the values of an url parameter that looks like this: <url>?featuresId=18, 2, 50
I have the array below to sort my values:
------------------
$temp=explode(',', $HTTP_GET_VARS['featuresId ']);
foreach($temp as $index=>$value)
{
if(is_int($value))
{
$temp[$index]=(int)$value;
}
else
{
unset($temp[$index]);
}
}
asort($temp);
$get_features_query = "select * from features where feature_name like '%|" . implode("|%|", $temp) . "|%'";
-------------------------- ---------- -
The thing is i need the query to be filtered by feature_name like this:
$get_features_query = "select * from features where feature_name like %|2|%|18|%|50|%;
Any ideas how can i get to the bottom of this?
Thanks
I have an mysql query which need to be filtered by the values of an url parameter that looks like this: <url>?featuresId=18, 2, 50
I have the array below to sort my values:
------------------
$temp=explode(',', $HTTP_GET_VARS['featuresId
foreach($temp as $index=>$value)
{
if(is_int($value))
{
$temp[$index]=(int)$value;
}
else
{
unset($temp[$index]);
}
}
asort($temp);
$get_features_query = "select * from features where feature_name like '%|" . implode("|%|", $temp) . "|%'";
--------------------------
The thing is i need the query to be filtered by feature_name like this:
$get_features_query = "select * from features where feature_name like %|2|%|18|%|50|%;
Any ideas how can i get to the bottom of this?
Thanks
You would want to start with something like this:
http://www.laprbass.com/RAY_temp_b0byan.php
http://www.laprbass.com/RAY_temp_b0byan.php
<?php // RAY_temp_b0byan.php
error_reporting(E_ALL);
/**
* ?featuresId=18, 2, 50
* SEE http://www.experts-exchange.com/Database/MySQL/Q_28293954.html
*/
// IF THERE IS ANYTHING IN THE REQUEST
if (!empty($_GET['featuresId']))
{
var_dump($_GET);
}
// CREATE THE FORM FOR THE INPUT VALUES
$form = <<<ENDFORM
<form>
<input name="featuresId" />
<input type="submit" />
</form>
ENDFORM;
echo $form;
This code will enable you to see the relationship between the URL variables and the data that arrives in the PHP script via the $_GET array.
Next, we would move on to something like this, which filters the input. Note that it uses is_numeric() and does not use is_int(). The reason for this choice is clear when you read the online man page for is_int().
<?php // RAY_temp_b0byan.php
error_reporting(E_ALL);
/**
* ?featuresId=18, 2, 50
* SEE http://www.experts-exchange.com/Database/MySQL/Q_28293954.html
*/
// IF THERE IS ANYTHING IN THE REQUEST
if (!empty($_GET['featuresId']))
{
// ACTIVATE THIS LINE TO SEE THE REQUEST VARIABLES
// var_dump($_GET);
// BREAK THE REQUEST VARIABLES INTO AN ARRAY, USING THE COMMA
$arr = explode(',', $_GET['featuresId']);
// ELIMINATE ANYTHING THAT IS NOT A NUMBER
foreach ($arr as $key => $val)
{
$arr[$key] = trim($val);
if (is_numeric($arr[$key])) continue;
unset($arr[$key]);
}
// IF THERE ARE ANY NUMBERS LEFT
if (!empty($arr)) var_dump($arr);
}
// CREATE THE FORM FOR THE INPUT VALUES
$form = <<<ENDFORM
<form>
<input name="featuresId" value="18, 2, 50" />
<input type="submit" />
</form>
ENDFORM;
echo $form;
ASKER
Hi,
I don't know if you understood me right. Maybe my english is not too good and i'm sorry for this.
To make it more clearer why i need the query filtered like this is because my table column looks something like this:
products_id featuresId
10107 |1|2|3|5|6|22|27|29|53|56| 86|96|100| 103|104|11 7|13...
16536 |3|6|9|13|14|21|22|39|40|5 1|53|56|57 |58|65|66| 136|...
121169 |3|6|7|9|11|12|15|17|18|21 |32|34|39| 41|42|50|5 1|56...
If you look at the featuresId column you'll understand why i need those things in the begining. I don't see any other solution to find and narrow my results searching i nthis column but this
Thanks
Hope you help me solve this
I don't know if you understood me right. Maybe my english is not too good and i'm sorry for this.
To make it more clearer why i need the query filtered like this is because my table column looks something like this:
products_id featuresId
10107 |1|2|3|5|6|22|27|29|53|56|
16536 |3|6|9|13|14|21|22|39|40|5
121169 |3|6|7|9|11|12|15|17|18|21
If you look at the featuresId column you'll understand why i need those things in the begining. I don't see any other solution to find and narrow my results searching i nthis column but this
Thanks
Hope you help me solve this
The next step would be to construct the query string. You would never want to use SELECT * (antipractice #26) but instead your query would name the columns that you wanted to select.
Without seeing the CREATE TABLE statement, we are guessing about the nature of the feature_name column. I am going to guess that it is not a "name" but is really a numeric id, probably of type INT. This next step would enable us to create the WHERE clause for the query.
Without seeing the CREATE TABLE statement, we are guessing about the nature of the feature_name column. I am going to guess that it is not a "name" but is really a numeric id, probably of type INT. This next step would enable us to create the WHERE clause for the query.
<?php // RAY_temp_b0byan.php
error_reporting(E_ALL);
/**
* ?featuresId=18, 2, 50
* SEE http://www.experts-exchange.com/Database/MySQL/Q_28293954.html
*/
// IF THERE IS ANYTHING IN THE REQUEST
if (!empty($_GET['featuresId']))
{
// ACTIVATE THIS LINE TO SEE THE REQUEST VARIABLES
// var_dump($_GET);
// BREAK THE REQUEST VARIABLES INTO AN ARRAY, USING THE COMMA
$arr = explode(',', $_GET['featuresId']);
// ELIMINATE ANYTHING THAT IS NOT A NUMBER
foreach ($arr as $key => $val)
{
$arr[$key] = trim($val);
if (is_numeric($arr[$key])) continue;
unset($arr[$key]);
}
// IF THERE ARE ANY NUMBERS LEFT
if (!empty($arr))
{
$where = 'WHERE feature_name = ' . implode(' OR feature_name = ', $arr);
echo $where;
}
}
// CREATE THE FORM FOR THE INPUT VALUES
$form = <<<ENDFORM
<form>
<input name="featuresId" value="18, 2, 50" />
<input type="submit" />
</form>
ENDFORM;
echo $form;
products_id featuresIdOh, NO! Your data base is misdesigned. Please make a Google search for the exact terms "Should I Normalize My Database" and read the very thoughtful discussion on the issue. You should never have more than one atomic value in any column of a data base table. A correct design would link the products_id to the featuresId via a junction table. This would establish the relationship that is inherent in relational data base.
10107 |1|2|3|5|6|22|27|29|53|56|86|96|100| 103|104|11 7|13...
16536 |3|6|9|13|14|21|22|39|40|51|53|56|57 |58|65|66| 136|...
121169 |3|6|7|9|11|12|15|17|18|21|32|34|39| 41|42|50|5 1|56...
ASKER
Yeaa sorry for the misspeling. I've modified the query for this example only and missed some stuff. In fact feature_name is featuresId
If you're stuck with the existing design (and I sincerely hope you're not) you may be able to construct the WHERE clause with something like this.
<?php // RAY_temp_b0byan.php
error_reporting(E_ALL);
/**
* ?featuresId=18, 2, 50
* SEE http://www.experts-exchange.com/Database/MySQL/Q_28293954.html
*/
// IF THERE IS ANYTHING IN THE REQUEST
if (!empty($_GET['featuresId']))
{
// ACTIVATE THIS LINE TO SEE THE REQUEST VARIABLES
// var_dump($_GET);
// BREAK THE REQUEST VARIABLES INTO AN ARRAY, USING THE COMMA
$arr = explode(',', $_GET['featuresId']);
// ELIMINATE ANYTHING THAT IS NOT A NUMBER
foreach ($arr as $key => $val)
{
$arr[$key] = trim($val);
if (is_numeric($arr[$key])) continue;
unset($arr[$key]);
}
// IF THERE ARE ANY NUMBERS LEFT
if (!empty($arr))
{
$where = "WHERE featuresId LIKE '%|" . implode("|%' OR featuresId LIKE '%|", $arr) . "|%'";
echo $where;
}
}
// CREATE THE FORM FOR THE INPUT VALUES
$form = <<<ENDFORM
<form>
<input name="featuresId" value="18, 2, 50" />
<input type="submit" />
</form>
ENDFORM;
echo $form;
Edited to use "featuresId" instead of "feature_name"
ASKER
Yea i know the table is not a good design. In fact is very troublesome. But this is how i received my data from my employers and can't argue with that.
I have to go around some problems. And this is the only solution that i found to filter and narrow products by facility.
I cannot modify the table.
My question is can you help me in this case?
The way i constructed the url and the filter worked so far for me. The only issue that i have in the query below is i have to have the parameter values ordered ascending in order to display the results correctly.
This is what i need. To order the values in an array and then put them in the query in this form '%|2|%|18|%|50|%'
$get_features_query = "select * from features where featuresId like '%|" . str_replace(",", "|%|", $HTTP_GET_VARS['featuresId ']) . "|%'
Thanks
I have to go around some problems. And this is the only solution that i found to filter and narrow products by facility.
I cannot modify the table.
My question is can you help me in this case?
The way i constructed the url and the filter worked so far for me. The only issue that i have in the query below is i have to have the parameter values ordered ascending in order to display the results correctly.
This is what i need. To order the values in an array and then put them in the query in this form '%|2|%|18|%|50|%'
$get_features_query = "select * from features where featuresId like '%|" . str_replace(",", "|%|", $HTTP_GET_VARS['featuresId
Thanks
Please see this link.
https://www.experts-exchange.com/questions/28293954/Sorting-parameter-arrays-and-filtering-mysql-query.html?anchorAnswerId=39647930#a39647930
You can use PHP sort() to change the order of array elements.
When you post a question here at EE, you will get quick and accurate help if you prepare the SSCCE. In the case of database questions this would include the CREATE TABLE statement.
Do not use HTTP_GET_VARS -- it is deprecated.
http://php.net/manual/en/reserved.variables.get.php
https://www.experts-exchange.com/questions/28293954/Sorting-parameter-arrays-and-filtering-mysql-query.html?anchorAnswerId=39647930#a39647930
You can use PHP sort() to change the order of array elements.
When you post a question here at EE, you will get quick and accurate help if you prepare the SSCCE. In the case of database questions this would include the CREATE TABLE statement.
Do not use HTTP_GET_VARS -- it is deprecated.
http://php.net/manual/en/reserved.variables.get.php
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help. I'll go from here on my own
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
I'll look at the problem specifics in a moment. ~Ray