I have a column named weight in my table accounts. i need to fetch the values from this column like,
when the user clicks weight from 40 - 100, It need to display all the persons with weight in between 40 and 100.
This is my html code,
<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>
This is my php code,
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;
}
}
This code is almost working for an single weight from table. But I need to create a range in between the checkbox values as i mentioned in the code. Do i need to update the code.
In php code change
if(is_numeric($weight) && $weight != NULL){
$sql .= "AND `weight` IN (".$weight.")";
$is_weight = true;
}
To
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;
}
Also, I recommend using radio instead of check box as with check boxes, if you select more than one weight options one value will be lost.
Try this, hope this works, so you won't make big changes in your code
Use radion button
<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>
You can add a variable array in 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
at this line add do this
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;
}
be care-full with quotes