PHP-MySQL product search tool with list menu

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    PHP-MySQL product search tool with list menu

    Hi,
    I am developing a PHP web site that uses a search tool in it. I’ll brief it like this,
    In the form user will first select Type of the computer (Laptop, desktop or all), then user will select the Brand that he/she expected and also the color or all , if the criteria is matched with the records in the table result should display.

    Some times user will select type of the computer and here after by skipping the brand he/she will select the color in that case all brand should display, with specified type and color.

    Is any one has completed such a tool pls submit your result back to me.
    Pls refer to the Coding HTML / SQL.
    Thanks,

    email addresses not allowed, removed. - moderator

    HTML coding:
    [HTML]
    <form id="form" name="form" method="post" action = "" >

    <select name="type">
    <option selected="selec ted">Select Product Type</option>
    <option value="desktop" >Desktop</option>
    <option value="laptop"> Laptop</option>
    <option value="all">All Types</option>
    </select>

    <select name="brand">
    <option selected="selec ted">Select Product Brand</option>
    <option value="intel">I ntel</option>
    <option value="ibm">IBM </option>
    <option value="amd">AMD </option>
    <option value="toshiba" >Toshiba</option>
    <option value="all">All Brands</option>

    </select>

    <select name="color">
    <option selected="selec ted">Select Product Color</option>
    <option value="black">B lack</option>
    <option value="white">W hite</option>
    <option value="Silver"> Silver</option>
    <option value="all">All Colors</option>
    </select>

    </form>
    [/HTML]

    My SQL Code
    Code:
    create table products
    (
    p_id int not null primary key,
    p_brand char (15) not null,
    p_color char (10) not null,
    p_type char (10) not null
    )
    
    
    insert `products` (p_id,p_brand,p_color,p_type)values ('1001','IBM','Black','Laptop')
    insert `products` (p_id,p_brand,p_color,p_type)values ('1002','Intel','white','Desktop')
    insert `products` (p_id,p_brand,p_color,p_type)values ('1003','Toshiba','Silver','Laptop')
    insert `products` (p_id,p_brand,p_color,p_type)values ('1004','Intel','Black','Desktop')
    insert `products` (p_id,p_brand,p_color,p_type)values ('1005','AMD','white','Laptop')
  • BeRtjh
    New Member
    • Jan 2007
    • 12

    #2
    you might want to use
    Code:
    OnChange="location.php"

    [HTML]
    <form id="form" name="form" method="post" action = "" OnChange="locat ion.php">

    <select name="type">
    <option selected="selec ted">Select Product Type</option>
    <option value="desktop" >Desktop</option>
    <option value="laptop"> Laptop</option>
    <option value="all">All Types</option>
    </select>

    <select name="brand">
    <option selected="selec ted">Select Product Brand</option>
    <option value="intel">I ntel</option>
    <option value="ibm">IBM </option>
    <option value="amd">AMD </option>
    <option value="toshiba" >Toshiba</option>
    <option value="all">All Brands</option>
    </select>

    <select name="color">
    <option selected="selec ted">Select Product Color</option>
    <option value="black">B lack</option>
    <option value="white">W hite</option>
    <option value="Silver"> Silver</option>
    <option value="all">All Colors</option>
    </select>

    </form>[/HTML]

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      How do you intend to let the user start the search by clicking a search or something button, i.e. how do you submit your form to the backend process?

      Ronald :cool:

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Originally posted by BeRtjh
        you might want to use
        Code:
        OnChange="location.php"

        [HTML]
        <form id="form" name="form" method="post" action = "" OnChange="locat ion.php">

        <select name="type">
        <option selected="selec ted">Select Product Type</option>
        <option value="desktop" >Desktop</option>
        <option value="laptop"> Laptop</option>
        <option value="all">All Types</option>
        </select>

        <select name="brand">
        <option selected="selec ted">Select Product Brand</option>
        <option value="intel">I ntel</option>
        <option value="ibm">IBM </option>
        <option value="amd">AMD </option>
        <option value="toshiba" >Toshiba</option>
        <option value="all">All Brands</option>
        </select>

        <select name="color">
        <option selected="selec ted">Select Product Color</option>
        <option value="black">B lack</option>
        <option value="white">W hite</option>
        <option value="Silver"> Silver</option>
        <option value="all">All Colors</option>
        </select>

        </form>[/HTML]

        Thank you This Not the thing what i need ! I am Not a newbie for PHP.

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          Originally posted by ronverdonk
          How do you intend to let the user start the search by clicking a search or something button, i.e. how do you submit your form to the backend process?

          Ronald :cool:
          I can pass the HTML data for process it, Sorry I didn't put entire
          Form here and also i didn't put my PHP script also.

          ok pls take a look at for this.

          [PHP]
          <?php

          $con = mysql_connect(' localhost', 'user', 'pwd') or die
          ("Could not connect to the Database");
          mysql_select_db ('myDB', $con) or die (mysql_error()) ;

          if ($_POST['search'])/* we will assume there is a Button Named search*/
          {//main if

          $type = $_POST['type'];
          $brand = $_POST['brand'];
          $color = $_POST['color'];

          /* Here I will Get the Form Data to PHP script
          Then I want pass it to the MySQL table see my SQL script
          */

          /* what should be the most suitable tech for write down the
          SQL statement based on the search criteria;
          If the user is selecting proper value from the all the menus Like
          Laptop , IBM, white No matter I can go for like this;
          */
          $sql ="select * from products where
          p_type = '$type' AND p_brand = '$brand' AND p_brand = '$brand'
          GROUP by p_id Order by p_id asc”;

          /* I thing u got the Idea; But what will happen when some one
          select this criteria ;
          All from Type, IBM , white
          How to change my SQL var again and again for this?
          */

          $result=mysql_q uery($sql) or die("Error select statement : " . mysql_error());
          if (mysql_num_rows ($result) == 0)
          {
          echo "No Maching Result Found for your Criteria!!!";
          }
          else
          {
          // I can show the result as I need Dont worry about this
          }

          [/PHP]

          I didnt debug it some times there might be syntax errors.Thanks

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Too many errors there. This will work. I added the submit form so you can test it out for yourself.

            A.PHP:
            =====
            [html]<form method="post" action="b.php">
            type <input type="text" name="type" /><br />
            brand<input type="text" name="brand" /><br />
            color<input type="text" name="color" /><br />
            <input type="submit" value="send it" />
            </form>[/html]B.PHP:
            ======
            [php]<?php
            $string = array();
            $where = "";
            if (isset($_POST['type']) AND !empty($_POST['type']) )
            $string[] = " p_type = '".$_POST['type']."' ";
            if (isset($_POST['brand']) AND !empty($_POST['brand']) )
            $string[] = " p_brand = '".$_POST['brand']."' ";
            if (isset($_POST['color']) AND !empty($_POST['color']) ) {
            $color = $_POST['color'];
            $string[] = " p_color = '".$_POST['color']."' ";
            }
            // setup the WHERE strings
            if (!empty($string ))
            $where = " WHERE ".implode("AND" , $string);

            $sql="SELECT * FROM products $where GROUP BY p_id ORDER BY p_id asc ";
            echo $sql;
            ?>[/php]
            Ronald :cool:

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Originally posted by ronverdonk
              Too many errors there. This will work. I added the submit form so you can test it out for yourself.

              A.PHP:
              =====
              [html]<form method="post" action="b.php">
              type <input type="text" name="type" /><br />
              brand<input type="text" name="brand" /><br />
              color<input type="text" name="color" /><br />
              <input type="submit" value="send it" />
              </form>[/html]B.PHP:
              ======
              [php]<?php
              $string = array();
              $where = "";
              if (isset($_POST['type']) AND !empty($_POST['type']) )
              $string[] = " p_type = '".$_POST['type']."' ";
              if (isset($_POST['brand']) AND !empty($_POST['brand']) )
              $string[] = " p_brand = '".$_POST['brand']."' ";
              if (isset($_POST['color']) AND !empty($_POST['color']) ) {
              $color = $_POST['color'];
              $string[] = " p_color = '".$_POST['color']."' ";
              }
              // setup the WHERE strings
              if (!empty($string ))
              $where = " WHERE ".implode("AND" , $string);

              $sql="SELECT * FROM products $where GROUP BY p_id ORDER BY p_id asc ";
              echo $sql;
              ?>[/php]
              Ronald :cool:
              *************** *************** *************** *************** *************** ************
              Thank you very much Ronald.

              But,My Search tool is based on List menus.But your one is based on Text Input boxes.

              This is Not my requirement anyway thanks, I'll Try to Make it based on your Post.Thank you once again that technique is cool.

              *************** *************** *************** *************** *************** ************

              Comment

              Working...