473,386 Members | 1,715 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,386 software developers and data experts.

getting data from database according to the user selection

Hello,

i am new to php.

I am trying to do a search. but not getting how exactly i can achieve this.

Here is my html form

Expand|Select|Wrap|Line Numbers
  1. <form method="post" action="http://bytes.com/search.php" name="form1">
  2. Select a color <select> <option value="red">Red</option> <option value="blue">Blue</option> <option value="white">White</option> </select> <br /><br />
  3.  
  4. Select cloth <select> <option value="silk">Silk</option> <option value="cotton">Cotton</option> </select> <br /><br />
  5.  
  6. Select brand <select> <option value="levis">Levis</option> <option value="ethnica">Ethnica</option> <option value="biba">Biba</option> </select> <br /><br />
  7.  
  8. Select type <select> <option value="shirt">Shirt</option> <option value="skirt">Skirt</option> <option value="salwar">Salwar</option> <option value="kurtis">Kurtis</option> </select> <input type="submit" value="go"> </form>

Here i want to display the result from database according to the selection.

if nothing is selected, then i will display all the data.
if only color and brand or only color selected, or brand selected it should display only that.

I am not getting which condition to use.

if i use if- else condition, then my code become repetitive and too lengthy. If i use switch, again i have to use if-else inside it.

please somebody help me. I really need your help
Apr 25 '13 #1
3 7033
Rabbit
12,516 Expert Mod 8TB
if i use if- else condition, then my code become repetitive and too lengthy.
Why would that be? You only have four fields so you would only need four if conditions.
Apr 25 '13 #2
Now i have corrected my form and php code as well

Here is my form

Expand|Select|Wrap|Line Numbers
  1. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form1">
  2. Select a color <select name="color">
  3. <option value=""></option>
  4. <option value="red">Red</option>
  5. <option value="blue">Blue</option>
  6. <option value="white">White</option>
  7. </select>
  8. <br /><br />
  9.  
  10. Select cloth <select name="cloth">
  11. <option value=""></option>
  12. <option value="silk">Silk</option>
  13. <option value="cotton">Cotton</option>
  14. </select>
  15. <br /><br />
  16.  
  17. Select brand <select name="brand">
  18. <option value=""></option>
  19. <option value="levis">Levis</option>
  20. <option value="ethnica">Ethnica</option>
  21. <option value="biba">Biba</option>
  22. </select>
  23. <br /><br />
  24.  
  25. Select type <select name="type">
  26. <option value=""></option>
  27. <option value="shirt">Shirt</option>
  28. <option value="skirt">Skirt</option>
  29. <option value="salwar">Salwar</option>
  30. <option value="kurtis">Kurtis</option>
  31. </select>
  32. <input type="submit" value="go" name="go" />
  33. </form>
my php code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. if(isset($_POST['go']))
  4. {
  5.  
  6. $colors = mysql_real_escape_string($_POST['color']);
  7. $cloths = mysql_real_escape_string($_POST['cloth']);
  8. $brands = mysql_real_escape_string($_POST['brand']);
  9. $type = mysql_real_escape_string($_POST['type']);
  10.  
  11. include('connect.php');
  12. $sql = "select * from clothing where color = '$colors' OR cloth = '$cloths' OR brand = '$brands' OR type = '$type'";
  13.  
  14. $result = mysql_query($sql) or die (mysql_error());
  15.  
  16. while($row = mysql_fetch_array($result))
  17. {
  18. //$cloth = $row['cloth'];
  19. //$type = $row['type'];
  20. //$brand = $row['brand'];
  21. //$color = $row['color'];
  22.  echo "<table width='65%' cellpadding='4' border='0'>" ;
  23.               echo "<tr><td>  Cloth:   </td> <td>Type</td> <td>Brand</td><td> Color</td></tr> ";
  24.                echo "<tr><td> " . $row['cloth'] . "</td> <td>" . $row['type'] . "</td><td> " . $row['brand'] . "</td><td> " . $row['color'] . "</td> </tr> </table>";
  25.  
  26. }
  27.  
  28.  
  29. }
  30.  
  31. ?>
now it displayes the result which satisfies at least one condition. but i want to display the result when all the selected data matches. if i select only color and type, it should display the data which has both the features in it.

For example : if i select color as blue and cloth as silk, then it should display only three rows of result which has both the two features, but now it is displaying all the rows which has either color as blue and cloth as silk. That shouldn't happen. RThat am not getting how to do.


Please suggest
Apr 26 '13 #3
Rabbit
12,516 Expert Mod 8TB
I don't see any if statements.
Apr 26 '13 #4

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

Similar topics

2
by: bryanilton | last post by:
Hi, I really hope one of you gurus can help me out here. I need to create a webform that has two drop-down boxes and allows a user to select a harddrive manufacturer and another for a harddrive...
7
by: news.microsoft.com | last post by:
Hi, Files related: 1. Search.aspx <- contains a <mycontrol:simplepaging runat=server> 2. Search.aspx.cs <- codebehind 3. SimplePaging.cs How do I send data from the codebehind into the user...
6
by: Jerry Spence1 | last post by:
Why doesn't the following work in my ASP program? I have imported ADOX I am trying to create a temporary database on the user's PC. The example is taken from Microsoft. Dim cat As Catalog =...
4
by: Randy Smith | last post by:
Hi, I would like to limit my user selection to only one row in the GridView at a time. Is there any way to set this? TIA, Randy
3
by: phpnewbie2007 | last post by:
Hello, I have a dropdown which populates from a database. When user selects option 1 from the dropdown, he should be directed to sample.php, if he selects option 2, then to sampleb.php and so on....
4
by: dfent | last post by:
I'm using MS Access 2003 1 form with 7 sub forms, updating 7 tables. I've split my database but still getting data changes error messages when multiple people are in the database. Any help would be...
7
by: jzieba | last post by:
I found the following script that changes the text based on a user selection. I want to modify the code to change an image source based on the user selection. I have been told I can do this with...
8
sumaiya
by: sumaiya | last post by:
I have interactive flash website intelution.com It is getting data from a database using PHP file system print "&photo$i=http://www.intelution.com/$photo&prod$i=$prod&"; The thing is the...
5
by: abhi3211 | last post by:
i am using java inside java script page. in that page i want to use two dropdown list. in first dropdown list i am getting data from ms-access database. in second dropdown list i want to get data...
3
by: yarivot26 | last post by:
Hi, I have a form where the user can select a product and the product info will appear next to it. The products and the data on them all come from a db so I needed it to be dynamically. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.