[英]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: HTML下拉列表:
<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). 这是下拉范围(我还将添加一个范围来搜索70-80个数据包)。 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. 如您所见,每个选项值仅由一个数字(“ 1,2,3”)组成,因此数据库将返回其中(例如)为1的任何结果。 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". 因此,如果用户搜索“ 100-199”范围内的内容,SQL查询将返回该范围内的所有结果,但当前还会返回其中包含“ 1”的任何内容,例如高度“ 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. 我想对其进行更改,以便SQL查询将获取该值,然后将其用于与记录中的第一个数字进行比较,例如(1)21中的1,因此返回的结果应仅在该范围内,而没有其他值如“ 212”将不会返回。 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: 并更改sql查询以搜索此范围:
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) 当然,应该进行适当的验证以确保$varHeight
是一个数字(大概在1到9之间)
UPDATED 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
假定$varHeight
的所有可能值的总最小值为000
,而总最大值为999
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.