SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Old Town
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Searching with Check Boxes

    Lets see if I can explain this:

    Basically this is sort of a search script.

    The user is confronted with 5 check boxes and they want to find 'resources' within the database that coressponds to what they check off.

    So here are the options

    Checkbox[1] = Red
    Checkbox[2] = Blue
    Checkbox[3] = Green
    Checkbox[4] = Yellow
    Checkbox[5] = Purple

    In the database if Resource #30 has yellow than the yellow column would have a 1, if it didn't have a red than the red column would be a 0

    Example. The user wants to find all the resources in the database that have Yellow AND Green. So they would check off Yellow and Green.

    $n = 0;

    while($n < 5){
    if ($_POST[checkbox[$n]] == 1) {
    *somehow add this color to the sql select statement*
    $n++;
    }

    else { $n++; }
    }


    so you see it would cycle through and each time it found that a color was checked off it would add it to the sql statement. So that in this case the resulting statement would look like

    $sql = "SELECT * FROM table WHERE yellow = 1 && green = 1";



    and the question is: and ideas on what to put for the *somehow add to the sql statement* ??

    Nate

  2. #2
    SitePoint Addict Guimauve's Avatar
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea which programming language you are using, but try something like this:

    Code:
    $n = 0;
    $sql = "SELECT * FROM table WHERE 1=1";
    
    while($n < 5){
      if ($_POST[checkbox[$n]] == 1) {
        switch ($n) {
          case 1:
            $sql += " && Red = 1" ;
            break;
          case 2:
            $sql += " && Blue = 1" ;
            break;
          case 3:
            $sql += " && Green = 1" ;
            break;
          case 4:
            $sql += " && Yellow = 1" ;
            break;
          case 5:
            $sql += " && Purple = 1" ;
            break;
        }
        $n++;
      }
    
      else {
        $n++;
      }
    }


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
  •