简体   繁体   中英

Possibility of comparing an “option value” with only the first number of a database record?

Sorry for the fairly ambiguous and long question.

I am creating a search form for a packaging database and over the past few days I have had a few problems with creating one of the forms that searches for the height of a packet. I want the form to display all results associated with a users input instead of it just returning the exact results. I couldn't get anything like that working. So I have resorted to creating a dropdown box that searches within a range.

HTML Dropdown:

        <body>
               <label for="trayheight">Height: </label>
                <select name="trayheight">
                    <option value="">All</option>
                    <option value="1">100 - 199 range</option>
                    <option value="2">200 - 299 range</option>
                    <option value="3">300 - 399 range</option>
                </select><br />
        </body>

Here is the dropdown ranges (I also will be adding a range that searches for the 70 - 80 packets). As you can see the option value in each just consists of one number ("1,2,3"), this is so the database will return any results that has (for example) a 1 in it. So if a user searches for something in the "100-199" range, the SQL query will return all the results that are within that range, but currently also return anything with a "1" in it eg height "201". I want to change it so the SQL query will take the value and then use it to compare it to the first number in a record eg the 1 in (1)21, so the results that return should only be within that range and nothing else such as "212" won't return. Is there some form of server code that would deal with this?

Here is my server side code:

       <body>

        <?php
            $con = mysql_connect ("localhost", "root", "");
                   mysql_select_db ("delyn_db", $con);

            if (!$con)
                { 
                    die ("Could not connect: " . mysql_error());
                }

            $descrip = mysql_real_escape_string($_POST['descrip']); 
            $width   = mysql_real_escape_string($_POST['width']);
            $depth   = mysql_real_escape_string($_POST['depth']);

            $varHeight= mysql_real_escape_string($_POST['trayheight']);
            $varRange = mysql_real_escape_string($_POST['trayrange']);
            $varType  = mysql_real_escape_string($_POST['traytype']);
            $varShape = mysql_real_escape_string($_POST['trayshape']);
            $varImage = mysql_real_escape_string($_POST['imagename']);

            $sql = "SELECT * FROM delyn WHERE 
                        description LIKE '%".$descrip."%'  
                    AND trayheight  LIKE '%".$varHeight."%'
                    AND trayrange   LIKE '%".$varRange."%' 
                    AND traytype    LIKE '%".$varType."%' 
                    AND trayshape   LIKE '%".$varShape."%'";


            $r_query = mysql_query($sql);

                while ($row = mysql_fetch_array($r_query))
                    { 
                        echo '<br /> <img src="   '. $row['imagename'] . '" width="180" length="100">';
                        echo '<br /> Tool Code:   '. $row['toolcode'];
                        echo '<br /> Description: '. $row['description']; 
                        echo '<br /> Tray range:  '. $row['trayrange']; 
                        echo '<br /> Tray type:   '. $row['traytype'];
                        echo '<br /> Tray size:   '. $row['traysize']; 
                        echo '<br /> Tray shape:  '. $row['trayshape'] . '<br />' . '<br />';  
                    }

                if (mysql_num_rows($r_query) <= 0){
                    echo 'No results match your search, please try again';
               }


        ?>
      </body>

Thanks guys.

Just construct the range:

if (isset($varHeight) && !empty($varHeight)) {
    $low = intval($varHeight."00");
    $high= intval($varHeight."99");
} else {
   $low = intval("000");
   $high= intval("999");
}

and change the sql query to search for this range:

AND trayheight  LIKE '%".$varHeight."%'

to

"AND trayheight  BETWEEN '".$low."' AND '".$high."'"

Of course proper validation should be placed to ensure that $varHeight is a number (presumably between 1 and 9)

in order to catch the absence of $varHeight filter. 为了赶缺乏$varHeight过滤器。 This assumes that the total minimum of all possible values for $varHeight is 000 and the total maximum is 999

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM