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 9 of 9
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to tie in a multiple choice search form to my PHP code?

    I'm trying to make a search form with multiple choices to be searched from. I want it to still return the same results from the same tables, just have someone be able to choose which criteria they want to search by. So, my question is, how do I tie in the multiple choices in the search form with the PHP code? I assume it would be put somewhere in the WHERE clause, but I'm not completely sure.

    This is my search form code:

    Code:
    <h1>Search Records</h1>
    		<form method="post" action="searchjoin3.php">
    			<table width=90% align=center>
    				<tr><td><select name="selectName" size="3" multiple="multiple">
    							<option selected="selected" value="one">Account #</option>
    							<option selected="selected" value="two">Name</option>
    							<option selected="selected" value="three">Legal</option>
    						</select></td><td><input type=text name='search' size=60 maxlength=255></td></tr>
    				<td></td>
    				<td><input type=submit value="Search Records"></td>
    And my PHP search query:

    PHP Code:
    "SELECT asmnt_parcel.Account, asmnt_parcel.OwnersName, asmnt_parcel.ParcelID, asmnt_parcel.Township, asmnt_parcel.Range, asmnt_parcel.Section, asmnt_parcel.LotSize, asmnt_parcel.LotSizeType, asmnt_parcel.TaxAreaCode, asmnt_parcel.TotalValue, asmnt_legal.Legal, cmn_name.Address2, cmn_name.City, cmn_name.State, cmn_name.ZipCode, asmnt_situs.Situs, appr_resident.TotalArea, appr_resident.YearBuilt, appr_miscimpr.Description, appr_miscimpr.Year, appr_miscimpr.Size, appr_miscimpr.Value, sale_parcel.SaleDate
                  FROM asmnt_parcel
                  INNER JOIN asmnt_legal 
                  ON asmnt_parcel.Account=asmnt_legal.Account
                  INNER JOIN cmn_name
                  ON asmnt_parcel.OwnersName=cmn_name.OwnersName
                  INNER JOIN asmnt_situs
                  ON asmnt_parcel.Account=asmnt_situs.Account
                  INNER JOIN appr_resident
                  ON asmnt_parcel.Account=appr_resident.Account
                  INNER JOIN appr_miscimpr
                  ON asmnt_parcel.Account=appr_miscimpr.Account
                  INNER JOIN sale_parcel
                  ON asmnt_parcel.Account=sale_parcel.Account
                  WHERE asmnt_parcel.Account LIKE '{$search}'
                 ORDER BY asmnt_parcel.Account ASC"
    ;
       
    $result mysql_query($query$con) or die(mysql_error().": $query"); 
    As you can see right now I just have the one search option in the WHERE clause. But, I need to change that obviously.

    Any tips and ideas on how to do it would be great!!

    Thank you!
    Qadoshyah

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    do you mean something like

    WHERE condition1 AND conditon2

    or

    WHERE condition1 OR condition2

    ?

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    do you mean something like

    WHERE condition1 AND conditon2

    or

    WHERE condition1 OR condition2

    ?
    Umm, I'm not totally sure. That's what I'm asking. How would I tie in the multiple choices in the search form to the WHERE to search clause?

    I'm fairly new to PHP, which is why I'm asking!

    Thanks ,
    Qadoshyah

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    if you could rephrase the problem, it might get clearer to me.

  • #5
    Regular Coder cyrus709's Avatar
    Join Date
    Dec 2008
    Posts
    102
    Thanks
    22
    Thanked 5 Times in 5 Posts
    The issue your having is that when you $_POST (receive) the information, the name of what you are posting is the same, which is "selectName". so it looks like this
    Code:
    ?selectName=one&selectName=two&selectName=three
    but it should only insert the last one selected.

    The WHERE clause is not what you need, I believe you need to use an array to get them.

    It would also be helpful if you gave more of the php code, or at least defined any varaibles
    Last edited by cyrus709; 02-05-2010 at 10:38 PM.
    Last night I lay in bed looking up at the stars in the sky and I thought to myself, where the heck is the ceiling.

  • #6
    New Coder
    Join Date
    Feb 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cyrus709 View Post
    The issue your having is that when you $_POST (receive) the information, the name of what you are posting is the same, which is "selectName". so it looks like this
    Code:
    ?selectName=one&selectName=two&selectName=three
    but it should only insert the last one selected.

    The WHERE clause is not what you need, I believe you need to use an array to get them.

    It would also be helpful if you gave more of the php code, or at least defined any varaibles
    Yes, I believe I need an array. I am kind of lost in exactly how to do that. I think I also need some kind of foreach statement. But . . . ah, how to do it !!!

    Here is my full code:

    Code:
    <?php
       
       $dbHost = 
       $dbUser = 
       $dbPass = 
       $dbDatabase = 
      
    
    $search = $_POST['selectName'];
    
    if ($search) // perform search only if a string was entered.
    {
       $con = mysql_connect($dbHost, $dbUser, $dbPass) or die("Failed to connect to MySQL Server. Error: " . mysql_error());
       mysql_select_db($dbDatabase) or die("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
       
       
       $query = "SELECT asmnt_parcel.Account, asmnt_parcel.OwnersName, asmnt_parcel.ParcelID, asmnt_parcel.Township, asmnt_parcel.Range, asmnt_parcel.Section, asmnt_parcel.LotSize, asmnt_parcel.LotSizeType, asmnt_parcel.TaxAreaCode, asmnt_parcel.TotalValue, asmnt_legal.Legal, cmn_name.Address2, cmn_name.City, cmn_name.State, cmn_name.ZipCode, asmnt_situs.Situs, appr_resident.TotalArea, appr_resident.YearBuilt, appr_miscimpr.Description, appr_miscimpr.Year, appr_miscimpr.Size, appr_miscimpr.Value, sale_parcel.SaleDate
     			 FROM asmnt_parcel
     			 INNER JOIN asmnt_legal 
     			 ON asmnt_parcel.Account=asmnt_legal.Account
     			 INNER JOIN cmn_name
     			 ON asmnt_parcel.OwnersName=cmn_name.OwnersName
     			 INNER JOIN asmnt_situs
     			 ON asmnt_parcel.Account=asmnt_situs.Account
     			 INNER JOIN appr_resident
     			 ON asmnt_parcel.Account=appr_resident.Account
     			 INNER JOIN appr_miscimpr
     			 ON asmnt_parcel.Account=appr_miscimpr.Account
     			 INNER JOIN sale_parcel
     			 ON asmnt_parcel.Account=sale_parcel.Account
     			 WHERE asmnt_parcel.Account LIKE '{$search}'
                 ORDER BY asmnt_parcel.Account ASC";
       $result = mysql_query($query, $con) or die(mysql_error().": $query");
    
       if ($result)
       {
          echo "Results:<br><br>";
          echo "<table width=90% align=center border=1><tr>
          <td align=center bgcolor=#4A6B3F>Account</td>
          <td align=center bgcolor=#4A6B3F>Owners Name</td>
          <td align=center bgcolor=#4A6B3F>Address</td>
          <td align=center bgcolor=#4A6B3F>City</td>
          <td align=center bgcolor=#4A6B3F>State</td>
          <td align=center bgcolor=#4A6B3F>Zip Code</td>
          <td align=center bgcolor=#4A6B3F>Legal</td>
          <td align=center bgcolor=#4A6B3F>Parcel ID</td>
          <td align=center bgcolor=#4A6B3F>Property Size</td>
          <td align=center bgcolor=#4A6B3F>Type</td>
          <td align=center bgcolor=#4A6B3F>Total Sq. Ft</td>
          <td align=center bgcolor=#4A6B3F>Est. Year Built</td>
          <td align=center bgcolor=#4A6B3F>Total Value</td>
          <td align=center bgcolor=#4A6B3F>Impr. Description</td>
          <td align=center bgcolor=#4A6B3F>Impr. Year</td>
          <td align=center bgcolor=#4A6B3F>Impr. Size</td>
          <td align=center bgcolor=#4A6B3F>Impr. Value</td>
          <td align=center bgcolor=#4A6B3F>School District</td>
          <td align=center bgcolor=#4A6B3F>Situs</td>
          <td align=center bgcolor=#4A6B3F>Township</td>
          <td align=center bgcolor=#4A6B3F>Range</td>
          <td align=center bgcolor=#4A6B3F>Section</td>
          <td align=center bgcolor=#4A6B3F>Sale Date</td>
          </tr>";
       
          while ($r = mysql_fetch_array($result))
          { // Begin while
             $act = $r["Account"]; 
             $nme = $r["OwnersName"];
             $add = $r["Address2"];  
             $city = $r["City"];  
             $ste = $r["State"];  
             $zip = $r["ZipCode"]; 
             $legal = $r["Legal"];   
             $pid = $r["ParcelID"]; 
             $size = $r["LotSize"];  
             $type = $r["LotSizeType"]; 
             $sqft = $r["TotalArea"];
             $built = $r["YearBuilt"];  
             $valu = $r["TotalValue"];
             $impr = $r["Description"];
             $iyr = $r["Year"];
             $isze = $r["Size"];
             $ivlu = $r["Value"];
             $sch = $r["TaxAreaCode"];
             $sit = $r["Situs"];
             $tship = $r["Township"]; 
             $rng = $r["Range"];
             $sctn = $r["Section"];   
             $date = $r["SaleDate"]; 
             echo "<tr>
                <td>$act</td>
                <td>$nme</td>
                <td>$add</td>
                <td>$city</td>
                <td>$ste</td>
                <td>$zip</td>
                <td>$legal</td>
                <td>$pid</td>
                <td>$size</td>
                <td>$type</td>
                <td>$sqft</td>
                <td>$built</td>
                <td>$valu</td>
                <td>$impr</td>
                <td>$iyr</td>
                <td>$isze</td>
                <td>$ivlu</td>
                <td>$sch</td>
                <td>$sit</td>
                <td>$tship</td>
                <td>$rng</td>
                <td>$sctn</td>
                <td>$date</td>
                </tr>";
          } // end while
          
          echo "</table>";
       }
       else
       {
          echo "Sorry, please try your search again.";
       }
    }
    else
    {
       echo "Start your search";
    }
    ?>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head><title>Searching Another Test</title>
    </head>
    <body bgcolor="#bba86d">
    		<div align="center">
    			<p></p>
    		</div>
    		<h1>Search Records</h1>
    		<form method="post" action="searchjoin3.php">
    			<table width=90% align=center>
    				<tr><td><select name="selectName[]" size="3" multiple="multiple">
                         <option value="one">Account #</option>
                         <option value="two">Name</option>
                         <option value="three">Legal</option>
                      </select></td><td><input type=text name='search' size=60 maxlength=255></td></tr>
    				<td><input type=submit value="Search Records"></td>
    				</tr>
    			</table>
    	   
    </form>
    			<p></p>
    		<p></p>
    	</body>
    </html>
    Thank you!

  • #7
    New Coder
    Join Date
    Feb 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    if you could rephrase the problem, it might get clearer to me.
    What I'm having trouble with is tying in the 3 different choices to search by - account #, name & legal. You can see what I'm referring to here:

    They are in my search form, but how do I tie my PHP code to make it so that when someone selects "name" they can search by Smith, John. Or when someone selects "Account #", they can search by the proper account number.

    I have a while loop and I have the $Post as $_Post (selectName - which refers to my search form). You can see the while loop and everything in my code in the post above this.

    Thanks for any help!
    Qadoshyah
    Last edited by WA; 07-14-2010 at 08:22 PM.

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    so you want them to search bei either account #, name or legal? in this case your search form is wrong. you have one field, that contains the search string ("search") but a multiple select. so I’d rather propose a single select. in the end the select is responsible for the field name in WHERE and the input field for its search value. (that is, you either have a SQL for each select value predefined, or you create the SQL each time according to the passed selectName.)

    on the other hand side, if you want the search string to be looked up in the account, name and legal fields if your table, that would be a multiple WHERE clause, but – I suppose – it doesn’t make sense to look for a name in the account field, etc.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    New Coder
    Join Date
    Aug 2013
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    my issue

    I have an additional question.

    I have code to search a database using a keyword search in which a user can enter more than one keyword.

    I have code for some to search a different database field by category using a dropdown.

    What I would like to do is have a user (from the same page) search either by keyword entry or by category dropdown and return results.

    BUT

    also

    have a user do a filtered query where they can search by category + keyword + a third search criteria (for example, the number of people required to do something)...so two to three different search criteria..each searching for a separate value in a separate dB field.


  •  

    Posting Permissions

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