473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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("misc.inc"); 
  2.  \* Connection Code *\
  3.   $cxn = mysqli_connect($host,$user,$passwd,$dbname)   #14
  4.          or die ("couldn't connect to server");
  5.  
  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.");
  6.  
  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.)
  9.  
  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=\"http://bytes.com/images/sebg.gif\">\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=\"http://bytes.com/images/models/{$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.   }
  50.  
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
5 2261
code green
1,726 Expert 1GB
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"; 
  2.  
Try
Expand|Select|Wrap|Line Numbers
  1. echo '<option  value="4">4</option>'."\n"; 
  2.  
Why ."\n" by the way. Is the output not in a browser?
Mar 17 '10 #2
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
1,726 Expert 1GB
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'];
  5.  
  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,"};
  11.  
  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
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
1,726 Expert 1GB
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

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

Similar topics

2
by: Eric Schultz | last post by:
Good afternoon... I'm new to struts so I hope I'm not asking an obvious question, but so far I haven't found my answer in any book, web site, or google search. I need to develop an application...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
2
by: RBohannon | last post by:
I have a report with most fields populated by a query. However, some of the fields are variable in such a way that their values cannot be queried from a table. At present the values for these...
4
by: Mountain Bikn' Guy | last post by:
I am having serious problems with the following IDE bug: Could not write to output file 'x.dll' -- 'The process cannot access the file because it is being used by another process. ' and BUG:...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
6
by: Bez | last post by:
I have created a querie from a Form with differents search criteria on the form when i search for a comopany begining with "a" for example it will bring up all the companies begining with "a" but...
9
by: lightning | last post by:
Hi all, I'm not very conversant with the vocabulary of Access, so please ask for clarification if necessary... I am trying to build a search form that allows combinations of search terms. For...
1
by: Kevin | last post by:
Hello, I'm doing a school project and I'm supposed to be able to track campgrounds and their amenities. Here is some of the information I'm supposed to store: CAMPGROUND Number of Sites ...
5
by: programmerboy | last post by:
I never had this kind of issue before and it is completely surprising. I have a usercontrol where I need 2 forms to make 1 form. When I have only 1 form it submits the page to itself. I have spent...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.