Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

    How do I get a different result for the first key in a foreach() ?

    I built a form with category CB's from which I want to build the WHERE clause from my SELECT statement.
    Code:
    example
    <form>
      <label>Diodes </label><input type="checkbox" name="cat[]" value="diode" />
      <label>Resistors </label><input type="checkbox" name="cat[]" value="res" />
      <label>etc.. </label><input type="checkbox" name="cat[]" value="blah" />
    </form>
    After I capture the array from the $_POST, if I do
    PHP Code:
    foreach($catArray as $var){
        
    $where="WHERE field=$var"        

    I would get a bunch of standalone WHERE clauses instead of getting Where x=y OR x=z.


    But if I do
    PHP Code:
    foreach($catArray as $var){
        
    $where="OR field=$var"        

    I don't get the initial WHERE.


    How do I make it do something for the first key that is different from all/any others?


    ~ Mo
    Last edited by mOrloff; 03-12-2009 at 11:03 PM. Reason: small stylizing update

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can use a little trick: Add "WHERE 1" to the base query. This WHERE condition will always evaluate to "true" (1 represents true) and then you are free to add additional WHERE clauses down the line without worrying about the initial WHERE.

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Fumigator View Post
    .. use a little trick: Add "WHERE 1" to the base query...
    I'm having a little trouble visualizing it.

    Can you show an example?

    ~ Mo

  • #4
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Ohhh!
    I think I'm getting it.

    PHP Code:
    $where="WHERE 1";

    foreach(
    $catArray as $var){
        
    $where.="OR field=$var"        

    Is this what you mean?


    ~ Mo

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The "WHERE 1" would be included in the query no matter what, so you could just add it to the base query, ie SELECT * FROM tablename WHERE 1 and then you proceed to add conditions to this base query as needed.

    But on second thought, this trick is not going to work with "OR" conditions. Every row will be returned because your first condition (WHERE 1) will always be true. I've just used this trick to add "AND" conditions to a query, and looking at your application, I really think you want to be using "AND" conditions. Right? A user checks a few options and for each option you want to further filter the query?

  • #6
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    NOTE/FYI: this is being used to build a custom function

    Quote Originally Posted by Fumigator View Post
    ... I really think you want to be using "AND" ...
    ... user checks a few options, and for each option, you want to further filter the query?
    Not this time, unfortunately.

    I need the results to show all the parts in any selected categories.

    For example, all "diodes" and all "resistors".
    WHERE category="diode" OR category="resistor"

    Any thoughts?


    ~ Mo

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    In this case, I would build an "IN" condition statement.

    Code:
    SELECT * FROM table1
    WHERE category IN ('diode', 'resistor', 'etc', 'etc2')
    With this strategy you just have to build the list of values, enclosed in quotes, separated by commas. You already have an array at your disposal, so just use explode on it to add the commas, oh but before that, loop through the array to add the quotes.

    PHP Code:
    foreach ($catArray as $key => $val) {
        
    $catArray[$key] == "'".$val."'";
    }
    $inString explode(","$catArray); 

  • Users who have thanked Fumigator for this post:

    mOrloff (03-13-2009)

  • #8
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    SWEET!

    I never heard of the "IN" condition. That looks perfect.
    I'll look into it and give it a try. Thanks.

    ~ Mo


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •