By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Attempting to Output Multiple Values through Forms in PHP & SQL for Search Tool

P: 3
Hello, I've been working on this piece of code for quite a while. Essentially it is a Search Tool which pulls Mobile Home information from a database and displays listings based on the users input. When the results page attempts to acquire variables from the Search Tool, it only displays results based off of a single variable, not all of them, which needless to say doesn't give the correct listings. Here is an example snippet of the code I'm using for the Search Tool:

Expand|Select|Wrap|Line Numbers
  1.   include(""); 
  2.  \* Connection Code *\
  3.   $cxn = mysqli_connect($host,$user,$passwd,$dbname)   #14
  4.          or die ("couldn't connect to server");
  6.   /* Select all categories from Homes table */
  7.   $query = "SELECT * FROM Homes";    #18
  8.   $result = mysqli_query($cxn,$query)
  9.             or die ("Couldn't execute query.");        #20
  10.  /* Extract all rows from table */ 
  11.  while($row = mysqli_fetch_assoc($result)) 
  12.  {
  13.  extract($row);
  14.  echo "Variable Display: $Width\n"; //<-- This is the tester code I used to see if it actually returned all of the variables I need, and it successfully outputs all of them in sequence
  15.  }          
Here is the snippet of code I'm using for the form to activate my results page:
Expand|Select|Wrap|Line Numbers
  1. echo "<form action='homeresults.php' method='POST'>\n";
  2. echo "  <select name=\"Beds\">\n";
  3. echo "    <option selected=\"selected\" value=\"0\">Any</option>\n";
  4. echo "    <option  value=\"1\">1</option>  \n";
  5. echo "    <option  value=\"2\">2</option> \n";
  6. echo "    <option  value=\"3\">3</option> \n";
  7. echo "    <option  value=\"4\">4</option>\n";
  8. echo "        <option  value=\"5\">5</option></select>\n";  //<---I thought this code worked when I originally tested the Search Tool, but it defaults to 0, which returns all results (Same for Baths)
  9. /* This code is for Width, where I'm currently working on getting the "Any" option to work. The code works just fine for the other options, but I'm trying to get the generic "Any" option to return all results, but when I send $Width to the results page, it only returns one variable, hence only one set of results. This same rule will apply to all of the future code snippets to create multiple "Any" values.*/
  10. "&nbsp;<b><u>Width:\n";
  11. echo "</td>\n";
  12. echo "<td>\n";
  13. echo "<select name='Width'>\n";
  14. echo "    <option selected=\"selected\" value=\"$Width\">Any</option>\n";
  15. echo "    <option  value='Single-Wide'>Single-Wide</option> \n";
  16. echo "    <option  value='Double-Wide'>Double-Wide</option> \n";
  17. echo "    <option  value='Triple-Wide'>Triple-Wide</option> </select>\n";

Here is the snippet for the Results Page:
Expand|Select|Wrap|Line Numbers
  1.   /* Select Homes of the given type */
  2.   $query = "SELECT * FROM Homes 
  3.               WHERE Beds>=\"{$_POST['Beds']}\" and Baths>=\"{$_POST['Baths']}\" and Width=\"{$_POST['Width']}\""; //<---This code helps me select only the values that the user has asked for to decrease load time.
  4.   $result = mysqli_query($cxn,$query)
  5.             or die ("Couldn't execute query.");
  7.   /* Display results in a table */
  8.   $Width=array('Single-Wide','Double-Wide','Triple-Wide'); //<----This was my attempt to get the SQL Query to select all values from the table, and output all Widths to the table, which didn't work, but I'm unsure if my syntax or execution is incorrect, so I've left this in just in case it will double as a solution (Provided a fix can be offered of course.)
  10.   echo "<br>\n";
  11. /* This code just performs a loop for all of the arrays that are created as a result of my SQL Query. This code works just fine, and does it's job by generating a new table for each of my listings, but only works correctly when the variables get through, which is where my problem is originating */
  12.   while($row = mysqli_fetch_assoc($result))            
  13.   {
  14.     extract($row);
  15.     /* display row for each pet */
  16. echo "<table width=\"755px\" background=\"\">\n";
  17. echo "<tr>\n";
  18. echo "<td>\n";
  19. echo "    <table width=\"95%\">\n";
  20. echo "        <td rowspan=\"5\" align=\"center\"><a href=\"{$row['Model#']}.php\"><img border=\"1\" src=\"{$row['Model#']}Thumb.gif\"></a><br>\n";
  21. echo "        <b><u><font size=\"1\">Virtual Tour:<br></u><a href=\"{$row['TourLink']}\"><img src=\"{$row['TourPic']}\">\n";
  22. echo "    </td>\n";
  23. echo "    </tr>\n";
  24. echo "    <tr>\n";
  25. echo "        <td><font size=\"+1\"><b><u>{$row['ModelName']}</b></u><br>\n";
  26. echo "        {$row['Width']}<br>\n";
  27. echo "        {$row['Beds']} Bed / {$row['Baths']} Bath<br>\n";
  28. echo "        {$row['Area']} Sq. Ft.<br>\n";
  29. echo "        Available on Lot?: <a href=\"{$row['Model#']}.php\">{$row['OnLot']}</a><br>\n";
  30. echo "        \n";
  31. echo "    </td>\n";
  32. echo "    </tr>\n";
  33. echo "    </table>\n";
  34. echo "    \n";
  35. echo "</td>\n";
  36. echo "<td align=\"center\">\n";
  37. echo "    <table>\n";
  38. echo "    <tr>\n";
  39. echo "    <td><center><font size=\"+1\"><b><u></b></u><img src=\"{$row['Floorplan']}\"><br>Floorplan\n";
  40. echo "    </td>\n";
  41. echo "    </tr>\n";
  42. echo "    <tr>\n";
  43. echo "    <td align=\"center\"><a href=\"{$row['ModelLink']}\">-View Model in Detail-</a>\n";
  44. echo "    </td>\n";
  45. echo "    </tr>\n";
  46. echo "    </table>\n";
  47. echo "</td>\n";
  48. echo "</tr>\n";
  49.   }
I hope this is enough information and that my comments have helped explain some of the problems I'm currently experiencing with this. I've exhausted all of my resources, and am desperately seeking an answer to this problem. Any help, even an idea or small working function would be greatly appreciated. Thank you very much for your time.
Mar 17 '10 #1
Share this Question
Share on Google+
5 Replies

code green
Expert 100+
P: 1,726
it only displays results based off of a single variable, not all of them
I can't wade through all your code but surely this is because you are using <select> rather than a list box?

Also a tip to avoid the ugly escapes. Instead of
Expand|Select|Wrap|Line Numbers
  1. echo "    <option  value=\"4\">4</option>\n"; 
Expand|Select|Wrap|Line Numbers
  1. echo '<option  value="4">4</option>'."\n"; 
Why ."\n" by the way. Is the output not in a browser?
Mar 17 '10 #2

P: 3
The \n; and all of that is residual of an HTML to PHP converter I used online to save some time in the conversion process. I'm using a <select> box only for the drop down menu. For all other options, this works just great, it sends the variable it's supposed to, and gives me correct results. I'm just trying to work it out so the "Any" option will send all of the values so everything of a certain category is returned, i.e Any (Single-Wide, Double-Wide, Triple-Wide) vs. only Single, only Double etc.
Mar 17 '10 #3

code green
Expert 100+
P: 1,726
I can't see the declaration of $width so not sure what is meant to do.
But I would give <option> 'any' an empty value and insert some processing of the POST values.
Then build the query string dynamically
Expand|Select|Wrap|Line Numbers
  1. //Need extra form cleansing to avoid injection
  2. $beds = $_POST['Beds'];
  3. $baths = $_POST['Baths'];
  4. $width = $_POST['Width'];
  6. //String concat idea
  7. $query = 'SELECT * FROM Homes '; 
  8. $where = '';
  9. if(!empty($beds))  {$where .= "Beds >= $beds,"}; #note comma and space
  10. if(!empty($baths)) {$where .= "Baths >= $baths,"};
  12. $where = rtrim($where,', '); #get rid of trailng comma + space
  13. if(!empty($where))  {$query .= 'WHERE '.$where;}
Then the WHERE condition only will filter where specified, if not specified will get all
Mar 17 '10 #4

P: 3
Alright. I've taken the code snippet and inserted it into my page. As it stands, I have to leave all options as Any and it successfully outputs all homes, but when
I select any values it defaults to an error message. Are there any specific changes I need to make to any of my existing code in addition to amending these conditions, or is there perhaps a certain place it needs to be inserted in the code to have it work?
Mar 18 '10 #5

code green
Expert 100+
P: 1,726
I have not deliberately ignored you, my firewall intermittedly blocks this site.
Any suggestions moderators?
I select any values it defaults to an error message
What is the error message?
Mar 30 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.