SELECT/FORM Question

Discussion in 'PHP' started by dweb77, Jul 24, 2007.

  1. #1
    I have a form with 3 fields that could have a value or be an 'All' selection.

    Is there an expression that I can put in the form as a value for the 'All' choice on each, such that when I process the form and include the string in the SELECT query it will respond accordingly.

    I currently have the following as an example for the SELECT query with a ???? for where I want to be able to put an expression for the 'All'. As stated above, each of the fields could be an 'All' choice:

    SELECT FROM locations WHERE city = '$city' AND state = '????' AND zip = $zip

    The version of php is: 4.3.11

    Thanks in advance for any thoughts.
     
    dweb77, Jul 24, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could use "like" instead of =, which can be wild carded with a % sign if "ALL" is chosen...

    if ($_GET['all_city']){
    $city='%'
    }
    if ($_GET['all_state']){
    $state='%'
    }
    if ($_GET['all_zip']){
    $zip='%'
    }

    and then change your select clause to

    SELECT * FROM locations Where city like '$city' and state like '$state' and zip like '$zip'
     
    ecentricNick, Jul 24, 2007 IP
  3. bloodredxxx

    bloodredxxx Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's just a matter of creating the right SQL statement using PHP.

    $sql = "SELECT FROM locations WHERE city = '$city'";
    if ($state != "All") $sql .= " AND state = '$state'";
    $sql .= " AND zip = $zip";
    Code (markup):
    then call mysql_query($sql) or something.
     
    bloodredxxx, Jul 25, 2007 IP