我的表帐户中有一个名为 weight 的列。我需要从这个列中获取值,如
当用户点击40 - 100的重量时,需要显示重量在40到100之间的所有人。
这是我的HTML代码,
<div class="float-right span35">
<form class="list-wrapper search-form" method="get" action="{{CONFIG_SITE_URL}}/index.php">
<div class="list-header">
Talent Search
</div>
<h4 style="margin-left: 10px; color: gray;">Weight</h4>
<fieldset id="weight">
<input type="checkbox" name="weight" value="1" {{WEIGHT_1}}/>Below 40<br>
<input type="checkbox" name="weight" value="2" {{WEIGHT_2}}/>40-70<br>
<input type="checkbox" name="weight" value="3" {{WEIGHT_3}}/>70-100<br>
<input type="checkbox" name="weight" value="4" {{WEIGHT_4}}/>Above 100<br>
</fieldset>
<br/>
<input style="margin-left: 10px" type="submit" name="submit" value="search"/><br><br>
<input type="hidden" name="tab1" value="search">
</form>
</div>
这是我的PHP代码,
if(isset($_GET['submit'])){
$weight = ($_GET['weight'])? $escapeObj->stringEscape($_GET['weight']): NULL;
$sql = "SELECT id FROM `".DB_ACCOUNTS."` WHERE `id` IS NOT NULL ";
if(is_numeric($weight) && $weight != NULL){
$sql .= "AND `weight` IN (".$weight.")";
$is_weight = true;
}
$sql .= " ORDER BY `id` ASC";
}
if($is_weight){
$themeData['weight_'.$weight] = 'checked';
}
$query = $conn->query($sql);
if($query->num_rows > 0){
while($fetch = $query->fetch_array(MYSQLI_ASSOC)){
$get[] = $fetch['id'];
}
$i = 0;
$listResults = '';
$themeData['page_title'] = "Advanced Search - ".$lang['search_result_header_label'];
foreach($get as $row){
$timelineObj = new \SocialKit\User();
$timelineObj->setId($row);
$timeline = $timelineObj->getRows();
$themeData['list_search_id'] = $timeline['id'];
$themeData['list_search_url'] = $timeline['url'];
$themeData['list_search_username'] = $timeline['username'];
$themeData['list_search_name'] = $timeline['name'];
$themeData['list_search_thumbnail_url'] = $timeline['thumbnail_url'];
$themeData['list_search_button'] = $timelineObj->getFollowButton();
$listResults .= \SocialKit\UI::view('search/list-each');
$i++;
}
$themeData['list_search_results'] = $listResults;
}
}
此代码几乎适用于表中的单个权重。但我需要在代码中提到的复选框值之间创建一个范围。我是否需要更新代码。
答案 0 :(得分:0)
在PHP代码更改
if(is_numeric($weight) && $weight != NULL){
$sql .= "AND `weight` IN (".$weight.")";
$is_weight = true;
}
要
if(is_numeric($weight) && $weight != NULL){
if($weight==1){
$sql .= "AND weight<40";
}
if($weight==2){
$sql .= "AND weight>=40 AND weight <=70";
}
if($weight==3){
$sql .= "AND weight>70 AND weight <=100";
}
if($weight==4){
$sql .= "AND weight>100";
}
$is_weight = true;
}
另外,我强烈建议使用无线电而不是复选框,如果您选择多个权重选项,则会丢失一个值。
答案 1 :(得分:0)
试试这个,希望这有效,所以你不会在你的代码中做出重大改变
使用radion按钮
<input type="radio" name="weight" value="0" {{WEIGHT_1}}/>Below 40<br>
<input type="radio" name="weight" value="1" {{WEIGHT_2}}/>40-70<br>
<input type="radio" name="weight" value="2" {{WEIGHT_3}}/>70-100<br>
<input type="radio" name="weight" value="3" {{WEIGHT_4}}/>Above 100<br>
您可以在php中添加变量数组
$arrayWeight = ["`weight` >= 0 && `weight` <=40",
"`weight` >= 40 && `weight` <=70",
"`weight` >=70 && `weight` <=100",
"`weight` >= 100"];
// $arrayWeight[$weight] = $arrayWeight[1];
// $arrayWeight[1] = 'weight' >= 0 && 'weight' <=40 a string
在此行添加执行此操作
if(is_numeric($weight) && $weight != NULL)
{
$sql .= "AND ".$arrayWeight[$weight].")"; //sample $arrayWeight[1] = 'weight' >= 0 && 'weight' <=40
//" SELECT id FROM `".DB_ACCOUNTS."` WHERE `id` IS NOT NULL AND 'weight' >= 0 && 'weight' <=40
$is_weight = true;
}
小心引用