Jump to content

Using multiple variables to assemble a query string, what is best method?


Vigilante
 Share

Recommended Posts

What is the best way to do query strings when you need multiple variables?

`$pages->find("template=foo, $a, $b, $c");`

My problem is multi-fold.

If I set the variable to just the value, like $a = 10, and I do `template=foo, field=$a` I find that I get different results if $a happens to be empty. $a might be 10, but it might be an empty string. I found that "field=$a" would return 199 results while not having field in there at all would return 200 (the correct number). Why would field="" return one less entry?

So what I started doing is putting the whole thing in the string like `$a = "field=10"` so that I could just drop it in `template=foo, $a` and this worked. The problem is the commas! If $a is just empty then now I have `template=foo,` with a comma for no reason.

Compound that by the fact I want multiple variable query strings and then, where do I put the commas? I can do it in the variable like `$a = ", field=10` but then I have to have the variable smashed up against previous arguments like `template=foo{$a}{$b}{$c}` and it just doesn't look as nice.

Another thing is that I'd rather have the raw value in my variables. For example I'd rather $a = 10 than $a = ", field=10" because then I can't reuse $a anywhere to reference 10. So where do I store "field="? It seems to me PW doesn't like having arguments exist with empty strings since I get different results that way.

Ideally I want raw values in the variable, $a=10, $b=bar. And then I want a nice and pretty query string that returns accurate results whether those variables contain a value, or are empty strings or even uninitialized or null or whatever other conditions the logic might have.

Anyway, all that to say, what is the best way to use many possible variables in a query string which might have values and might not? Which looks nice, and is most useful in code.

Link to comment
Share on other sites

One of my specific problems is a page field array (multi select).

There seems to be no way do this: `$pages->find("template=foo, categories=$var");`

If $var contains an ID, it works. But if I'm not trying to filter categories and $var is empty, the result is inconsistent and wrong. I've tried setting $var to blank, empty string, null, false, 0. No matter what, it doesn't work.

In my case, if $var is empty and I'm not trying to select categories, then I want it to select ALL categories. In other words, doesn't matter, as if it didn't exist. Just select everything. But that's not the case, I can't find any way to set $var so that categories= is basically ignored.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...