Avatar of b0byan
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
MySQL ServerPHP

Avatar of undefined
Last Comment
b0byan

8/22/2022 - Mon
Ray Paseur

You need to get something of a foundation in how to use PHP.
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
Ray Paseur

You would want to start with something like this:
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;

Open in new window

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.
Ray Paseur

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;

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
b0byan

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|117|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|51|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
Ray Paseur

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.

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

Open in new window

Ray Paseur

products_id featuresId
10107 |1|2|3|5|6|22|27|29|53|56|86|96|100|103|104|117|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|51|56...
Oh, 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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
b0byan

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
Ray Paseur

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;

Open in new window

Edited to use "featuresId" instead of "feature_name"
b0byan

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
Your help has saved me hundreds of hours of internet surfing.
fblack61
Ray Paseur

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
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
b0byan

ASKER
Thanks for your help. I'll go from here on my own