SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    forms, checkboxes, ands, ors, oh my!

    I'd really appreciate any/all help with this problem:

    I have built a form with several text inputs as well as checkboxes. All of the checkboxes are named "media[]", with different values. The problem is that the query is connected by ANDS everywhere except in the media checkboxes, where the user is *supposed* to be able to select more than one media type. I've tried several different solutions and can't make any of them work properly.

    foreach ($media as $val) {
    $query . = " media like '$val''';
    }

    ....leads to a failed query when more than one checkbox is clicked (??!!!??). I have the same problem when I use:

    $query . = " media IN ('$val')''; combined with a count



    This code searches ok with only one checked box, but claims no results if more than one is clicked:

    $count = count($media);
    if($count > 0) {
    $medias = implode( " OR media LIKE ", $media);
    if ($query_mod !="0"){$query="$query &&";}
    $query .= "( media LIKE '$medias')";
    $query_mod="1";
    }



    The following code allows for more than one checkbox to be filled, but because of the OR , it interferes with the ANDs of the rest of the query and returns too many results:

    if(isset($media) && (count($media) > 0)) {
    if ($query_mod !="0"){$query="$query &&";}
    $query .= " media LIKE '$media[0]'";
    for($m=1; $m<count($media); $m++)
    $query .= " OR media LIKE '$media[$m]'";
    $query_mod="1";
    }

    I tried inserting parentheses to force it to separate the OR from the ANDs, but am not sure if this is even possible over several lines.
    What's the best method to get around these problems??
    Last edited by eep; Mar 29, 2003 at 20:06.

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eep
    What's the best method to get around these problems??
    1) Paper and a pencil
    2) Draw a picture of the logical function you want
    3) Translate into and and or


    Seriously, that's the best way I know to simplify complex logical functions...

    Parentheses - yes, they can span several lines


    About the checkbox problem:
    Have you tried imploding the array into a comma separated string and then use the IN() operator in the query?

  3. #3
    SitePoint Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for suggestions.
    I tried implode. When I use the following code, the result is always "no records found" if more than one checkbox is clicked (works ok for one box):

    $medias = implode(", ", $media);
    if ($medias !=''){
    if ($query_mod !="0"){$query="$query &&";}
    $query .= " media IN ('$medias')";
    $query_mod="1";
    }

    And I tried parentheses. The following code worked for query or two then completely choked up:

    if(isset($media) && (count($media) > 0)) {
    if ($query_mod !="0"){$query="$query &&";}
    $query .= "( media LIKE '$media[0]'";
    for($m=1; $m<count($media); $m++)
    $query .= " OR media LIKE '$media[$m]')";
    $query_mod="1";
    }

    As I understand php (which apparently I *don't*), both of these should work ... what am I doing wrong??

  4. #4
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the data type for media?
    Try removing the quotes in
    $query .= " media IN ('$medias')";
    I.e. change to
    $query .= " media IN ($medias)";
    Should work if media is a number

    If media is varchar, add quotes to each value
    I.e. $medias should look like "'abc', 'def', 'ghi'"
    (You could try this "trick"; set the values for the html checkboxes = _abc_, _def_, _ghi_, then implode the array of checked checkboxes in php, and replace _ with ' )

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jofa
    (You could try this "trick"; set the values for the html checkboxes = _abc_, _def_, _ghi_, then implode the array of checked checkboxes in php, and replace _ with ' )
    to do that, just use the ' in implode():

    PHP Code:
    <?php

    $medias 
    implode("', '"$media);

    ?>
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  6. #6
    SitePoint Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for all suggestions!!!

    As media is varchar, once I put the quotation marks in the implode statement, all my problems were solved.

  7. #7
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DR_LaRRY_PEpPeR
    to do that, just use the ' in implode():

    PHP Code:
    <?php

    $medias 
    implode("', '"$media);

    ?>
    Yes, that's true
    I wonder why I didn't think of that...

    But don't you have to add this too?
    $medias = "'" .implode("', '", $media) ."'";

  8. #8
    SitePoint Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jofa
    Yes, that's true

    But don't you have to add this too?
    $medias = "'" .implode("', '", $media) ."'";

    As I have it now (which seems to work fine):

    $medias = implode("','", $media);
    //some code
    $query .= " media IN ('$medias')";

    Wouldn't the quotes in the second statement ( '$medias') achieve the same purpose??
    Last edited by eep; Apr 2, 2003 at 14:31.

  9. #9
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, would be equal


Bookmarks

Posting Permissions

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