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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts

    question on coding

    I have a search form where some of the info does not have to be selected and I am a bit puzzled as to how to code my second page when those options are not used.

    Can I set up separate $query's based on the blank input?

    PHP Code:
    if (empty($search1))
      
    $query search

    else if (empty($search2))
      
    $query search2

    else
      
    $query search3

    if ($result mysql_query($query)) 
    or do I need to do the full code in each if statement?

    Any suggestions would be greatly appreciated!!
    Last edited by harkly; 06-30-2010 at 06:10 PM.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Of course you can. Think about it. What are you attempting to do in the if/else tree? Assign a string to a variable. There's nothing to restrict you from doing that.

    I'll bet you could have found the answer through testing in the time it took to write the post and get an answer.
    Are you a Help Vampire?

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Actually I did!

    But since I am new I want to make sure that I am not just pulling some of the logic out of thin air!

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Excellent! I'm surprised at the number of people who actually don't try, but wait for spoon-feeding.

    So, here's a bonus. You'll see code like this in some apps:
    PHP Code:
    // ...
    $query "SELECT `foo`, `bar` FROM `baz` WHERE";
    if (!empty(
    $search1))
    {
      
    $query .= " `foo`='$search1'";
    }
    elseif (!empty(
    $search2))
    {
      
    $query .= " `bar`='$search2'";
    }
    else
    {
      
    $query .= " `qux`='$search3'";
    }
    if (
    $result mysql_query($query))
    {
    // ... 
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    harkly (06-30-2010)


  •  

    Posting Permissions

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