getting data from database according to the user selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalumike
    New Member
    • Apr 2013
    • 2

    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

    Code:
    <form method="post" action="http://bytes.com/search.php" name="form1">
    Select a color <select> <option value="red">Red</option> <option value="blue">Blue</option> <option value="white">White</option> </select> <br /><br />
    
    Select cloth <select> <option value="silk">Silk</option> <option value="cotton">Cotton</option> </select> <br /><br />
    
    Select brand <select> <option value="levis">Levis</option> <option value="ethnica">Ethnica</option> <option value="biba">Biba</option> </select> <br /><br />
    
    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
    Last edited by Rabbit; Apr 25 '13, 03:11 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • shalumike
      New Member
      • Apr 2013
      • 2

      #3
      Now i have corrected my form and php code as well

      Here is my form

      Code:
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form1">
      Select a color <select name="color">
      <option value=""></option>
      <option value="red">Red</option>
      <option value="blue">Blue</option>
      <option value="white">White</option>
      </select>
      <br /><br />
      
      Select cloth <select name="cloth">
      <option value=""></option>
      <option value="silk">Silk</option>
      <option value="cotton">Cotton</option>
      </select>
      <br /><br />
      
      Select brand <select name="brand">
      <option value=""></option>
      <option value="levis">Levis</option>
      <option value="ethnica">Ethnica</option>
      <option value="biba">Biba</option>
      </select>
      <br /><br />
      
      Select type <select name="type">
      <option value=""></option>
      <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" name="go" />
      </form>
      my php code

      Code:
      <?php
      
      if(isset($_POST['go']))
      {
      
      $colors = mysql_real_escape_string($_POST['color']);
      $cloths = mysql_real_escape_string($_POST['cloth']);
      $brands = mysql_real_escape_string($_POST['brand']);
      $type = mysql_real_escape_string($_POST['type']);
      
      include('connect.php');
      $sql = "select * from clothing where color = '$colors' OR cloth = '$cloths' OR brand = '$brands' OR type = '$type'";
      
      $result = mysql_query($sql) or die (mysql_error());
      
      while($row = mysql_fetch_array($result))
      {
      //$cloth = $row['cloth'];
      //$type = $row['type'];
      //$brand = $row['brand'];
      //$color = $row['color'];
       echo "<table width='65%' cellpadding='4' border='0'>" ;
                    echo "<tr><td>  Cloth:   </td> <td>Type</td> <td>Brand</td><td> Color</td></tr> ";
                     echo "<tr><td> " . $row['cloth'] . "</td> <td>" . $row['type'] . "</td><td> " . $row['brand'] . "</td><td> " . $row['color'] . "</td> </tr> </table>";
      
      }
      
      
      }
      
      ?>
      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

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        I don't see any if statements.

        Comment

        Working...