Getting results from multiple values

I’m currently working on a site that uses server side javascript to send the results of various options (using Form Data and arrays as POST - for example: brand%5B%5D=Max+Warehouse+&brand%5B%5D=Superstore&timing%5B%5D=All day&flavour%5B%5D=Orange&flavour%5B%5D=Cola&sweeteners%5B%5D=Sugar&sweeteners%5B%5D=Date&sweeteners%5B%5D=none&pageNumber=1&tableLength=25&sortDirection=sortDesc&sortedBy=best_sellers) to a php script then the php will search the database for results that match options checked but I’m not sure how to do this.

I’m pretty new to php/mysql and don’t know how to run through what’s being sent if more than one value of the same option is sent.

Thanks in advance for any help

That should decode into arrays for the duplicates - %5B and %5D are the codes for the square bracket characters [ and ]. So you can use the count() function to check for multiple entries where you might expect them to be, and run through with foreach() or similar to build them into a query.

Sorry to ask but how do I do that?

The data should arrive in a $_POST array and be split into several elements within that array:

$_POST['pageNumber'] = 1
$_POST['tableLength'] = 25
$_POST['sortDirection'] = "sortDesc"

and so on. Those first elements where the characters are present should decode as something like

$_POST['brand'][0] = "Max Warehouse "
$_POST['brand'][1] = "Superstore"
... and ...
$_POST['flavour'][0] = "Orange"
$_POST['flavour'][1] = "Cola"

so if you have a look at the foreach() statement, you should be able to pick up how to iterate through those arrays to extract the various values.

If the values are not arriving in the $_POST array in that format, please show the code you have to send the data, and to extract it. And perhaps a vardump($_POST) to see what is arriving.

I used var_dump($_GET); to get what was being sent and this is what I got:

array(8) { [“brand”]=> array(2) { [0]=> string(14) "Max Warehouse " [1]=> string(10) “Superstore” } [“timing”]=> array(1) { [0]=> string(7) “All day” } [“flavour”]=> array(2) { [0]=> string(6) “Orange” [1]=> string(4) “Cola” } [“sweeteners”]=> array(3) { [0]=> string(5) “Sugar” [1]=> string(4) “Date” [2]=> string(4) “none” } [“pageNumber”]=> string(1) “1” [“tableLength”]=> string(2) “25” [“sortDirection”]=> string(8) “sortDesc” [“sortedBy”]=> string(12) “best_sellers” }

Thanks for helping with this

OK, that’s pretty much what I expected, except that you got it in $_GET rather than $_POST as you’d said you were using in the first post.

So you need to use foreach() through that where there are multiple values. http://php.net/manual/en/control-structures.foreach.php

Sorry I thought it was using $_POST but when I didn’t get anything from that thought I’d try $_GET instead.

I’ve tried to use foreach() to get the values but don’t get anything at all so am not sure what I’m doing wrong:

var_dump($_GET);
$arrayResults = $_GET;

$arr = $arrayResults;
foreach ($arr as &$value) {
    $arr = $value * 2;
}

foreach ($arr as $key => $value) {
    echo "{$key} => {$value} ";
    print_r($arr);
}

Also once I’ve done that how do I search the database with that? I want to get all results that match any value rather than only the results that match all

Thank you

Well, there’s a bit of superfluous code in there:

$arrayResults = $_GET;

$arr = $arrayResults;

It’s fine (and maybe desirable) to copy the $_GET array to another, but I can’t see why you’d then copy it to yet another array. Normally what you would see is that developers would validate the incoming data and put the validated data into other variables, and never use the incoming stuff directly.

The way I would do it would be something like

foreach ($arr['brand'] as $brand) { 
  echo $brand;
  }

and so on. But you don’t post the results of your sample code, what does that show?

Once you’ve extracted the data, then it’s a case of using those values to build a query. If you’re new to PHP, first off I would ditch the idea of the multiple search terms and, for now, just get it working with a single one. Once you can see how you can pass values in from the form to a query, then start adding support for multiple search terms.

Thank you, before I wasn’t getting anything, now I’m getting:

Max Warehouse Superstore

I’m using this to get the results but don’t know how to get specific results:

$sql_select = "SELECT * "; 
$sql_from = "from feeds,product_categories_map where feeds.brand = 'Max Warehouse' and feeds.enabled=1 and feeds.stock=1 and feeds.deleted=0 and feeds.best_seller=1 and product_id=product_feed_id and product_feed_id!=1 ";
$sql_orderby = " ORDER BY rand()";
$sql_limit = " LIMIT 20";

	$query=$sql_select . $sql_from . $sql_group . $sql_orderby . $sql_limit;
    $product_results=dbselect( $query,"dbLinkInt" );

OK, that’s what I’d expect.

I’m not sure what you mean by that. You can see in your second line of code how you hard-code the brand selection, so now you need to experiment with how to use the contents of that array as strings instead of the hard-coded value.

That’s the part I’m having trouble with, how do I use the array to get results where that matches - sorry I’m really new to PHP and am trying to pick it up as quickly as possible.

OK, so write a bit of code to run through the arrays as I showed earlier, but instead of just echoing the variables, build them into a string. You can do string concatenation because you show it in your post above. Your target is to end up with a string that looks like

"Max Warehouse" OR "Superstore"

But I still recommend the first thing to do is to just use the first value in the array for the brand selection in your query, and get that working first.

Sorry how do I get it so that each value is in quotes and there’s an OR in between each one?

You need to read up on basic PHP string handling for more detail. It’s a very basic part of the programming language, and you show it happening in your post #9 above - does that mean that code is not something you’ve written yourself?

I’d recommend having a look at a few basic PHP courses for stuff like this. I saw that you were pretty new to php/sql, but I wasn’t sure whether you had some experience in programming but in a different language.

I could show you the code, but I don’t think that would help. I don’t mean to come across as unhelpful (well, only to people I know … ) but I won’t just code it for you. It might speed your job up, but it wouldn’t help. And I might get it wrong.

1 Like

I understand what you’re saying about me not learning but I know how to replace empty spaces with ‘" OR "’ but I don’t know how to do it with a string coming from the array. I have looked at how to do it but can only find examples of replacing parts of sentence or word not putting it between each array value

I’m more of a front end developer than php developer so it’s all pretty alien to me.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.