filter results by using search box and drop down

Discussion in 'PHP' started by patrioticcow, Sep 17, 2010.

  1. #1
    hello.
    i have a search box that will display some results. I also have a drop down list with values "1" and "2".
    the values for each results are stored into database in a "req" field.

    <select name="req" id="req">
               <option value="1">yes</option>
               <option value="2">no</option>
    </select>
    $sql = 'SELECT * FROM topic WHERE (Title LIKE '."'%$search1%') AND req='1'
    PHP:
    what am i missing.
    thanks
     
    patrioticcow, Sep 17, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Post the full code, as I can indentify a few syntax errors with your $sql already, but posting more code would make me understand your structure better.
     
    danx10, Sep 18, 2010 IP
  3. seoforall

    seoforall Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i'm not sure what you wanna say with your sql query, but it doesn't look normal to me .. especially the punctuation before the $search1

    not much to understand from your short post, but i'll give it a shot .. did you want to write the following ?

    $sql = "SELECT * FROM topic WHERE Title LIKE '%$search1%' AND req='1' ";

    I've surrounded the wholel line with double quotes, removed the unknown punctuation before the wildcarded variable and removed the () since you don't need them in this particular case.

    You might also want to surround the field name "Title" with `....` (the thing just under your ESC button on keyboard) .. so it will look like `Title`.
     
    seoforall, Sep 18, 2010 IP
  4. patrioticcow

    patrioticcow Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i've got a little problem integrating the code into my code

    
    if($search1 == '')
       {
          $smarty->assign('Error','Please enter any destination!');
          $z=1;
       }
       elseif($type == 'like' && $status='1')
         {
    $sql = "SELECT * FROM topic WHERE (Title LIKE '%$search1%') AND req= $req";
    }
    
    Code (markup):
    my html is
    
    <select name="req" id="req">
               <option value="1">yes</option>
               <option value="2">no</option>
    </select>
    
    Code (markup):
    i am already putting the values into the database using this:
    
    $sql = 'INSERT INTO topic (req) VALUES '." ('$_SESSION[EmpId]', $req)";
    
    Code (markup):
    let me see if i got the right idea on how I should implement the code
    
    if($search1 == '')
       {
          $smarty->assign('Error','Please enter any destination!');
          $z=1;
       }
    if( $_POST['req'] == 1 || $_POST['req'] == 2 ) {
         $req = (int) $_POST['req'];
    }
       elseif($type == 'like' && $status='1')
         {
    $sql = "SELECT * FROM topic WHERE (Title LIKE '%$search1%') AND req= $req";
    $req = 1;
    }
    
    Code (markup):
    what u think?
     
    patrioticcow, Sep 20, 2010 IP
  5. almedajohnson

    almedajohnson Peon

    Messages:
    140
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    are you getting any error or wrong result??

    You haven't specified what problem you are facing>> If it runs well then you are missing nothing
    But if it is not working then you are missing to write your question here for now
     
    almedajohnson, Sep 21, 2010 IP
  6. patrioticcow

    patrioticcow Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    it just doesn't work..
    im trying to do it like here http://www.insiderpages.com/store_finder/find/home_depot
    where u search by something and then by state, just that i will put just 2 options in the state drop box
    thanks
     
    patrioticcow, Sep 22, 2010 IP
  7. axelay

    axelay Peon

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This is clearly wrong:
    if($search1 == '')
       {
          $smarty->assign('Error','Please enter any destination!');
          $z=1;
       }
    if( $_POST['req'] == 1 || $_POST['req'] == 2 ) {
         $req = (int) $_POST['req'];
    }
       elseif($type == 'like' && $status='1')
         {
    $sql = "SELECT * FROM topic WHERE (Title LIKE '%$search1%') AND req= $req";
    $req = 1;
    }
    Code (markup):
    the if statment is wrong... I would do something like:
       if($search1 == '')
       {
          $smarty->assign('Error','Please enter any destination!');
          $z=1;
       }
    
       if($type == 'like' && $status='1')
       {
          $req = intval ($_POST['req']);
          $sql = "SELECT * FROM topic WHERE (Title LIKE '%$search1%')";
          if ($req > 0 && $req < 3) $sql .= " AND req= $req";
          //...
       }
    Code (markup):
    As far as I understand it you want to narrow down the results IF the req has been chosen. Could be wrong though lol

    aXe
     
    axelay, Sep 22, 2010 IP
  8. patrioticcow

    patrioticcow Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i tryed this
    
     $req = intval ($_GET['req']);
    if($type == 'like' && $status='1' && $req > 0 && $req < 3)
      	{
    $sql = "SELECT * FROM topic WHERE (Title LIKE '%$search1%') AND req= $req"";
    }
    it worked. thanks a lotttttttttt
    Code (markup):
     
    Last edited: Sep 22, 2010
    patrioticcow, Sep 22, 2010 IP