Hello!
So I made a nice price-range-slider on the front-end and Im trying to make it filter some of the products (in this case houses). For the filtering Im using Ajax and Php. So I have a database with prices in it and I manage to get the prices out of the database with php-functions in my php-block. I also manage to display the maximum and minimum-price in the range-slider on the front end. But I think that I am having trouble getting the user-input-values back or that I do not manage to filter the database with them.
Im really not managing to figure this out somehow ;) Can anyone help me to figure this out? Any tips are welcome!
The slider looks like this (see the price-range-slider): https://spanje.centralxl.com/
The code of the price-ranger looks like this:
<div class="col-12 col-md-6 col-lg-3">
<div style="margin-bottom: 1rem;">
<span class="slider-span" style="color: white;" >Price: </span>
{% if price %}
<input name="Filter[minprice]" type="text" class="sliderValue2" style="width: 5rem" data-index="0" value="{{ minprice }}" />
<input name="Filter[maxprice]" type="text" class="sliderValue2" style="width: 5rem" data-index="1" value="{{ maxprice }}" />
{% endif %}
<span class="slider-span" style="color: white;">euro</span>
</div>
<div id="slider"></div>
</div>
So, I am trying to use AJAX and PHP and I have the following code in the PHP-block on the page:
==
<?php
use RafaelTeBoekhorst\Rafael\Models\House;
function onStart() {
$this->prepareVars();
$this->preparePrices();
}
function onFilterHouses() { $this->prepareVars(); }
function prepareVars() {
$options = post('Filter', []);
$this['houses'] = House::listFrontEnd($options);
$this['pages'] = $this['houses']->lastPage();
$this['page'] = $this['houses']->currentPage();
// $this['sortOptions'] = House::$allowedSortingOptions;
}
# prices
function preparePrices() {
$houses = House::all();
$price = [];
foreach ($houses as $province) {
$price[] = $province->price;
};
$price = array_unique($price);
$this['price'] = $price;
$maxprice = max($price);
$this['maxprice'] = $maxprice;
$minprice = min($price);
$this['minprice'] = $minprice;
}
?>
==
And the prepared values from this block go to my model-php-file in the backend:
<?php namespace RafaelTeBoekhorst\Rafael\Models;
use Model;
use Input;
/**
* Model
*/
class House extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Disable timestamps by default.
* Remove this line if timestamps are defined in the database table.
*/
public $timestamps = false;
/**
* @var string The database table used by the model.
*/
public $table = 'rafaelteboekhorst_rafael_spaanse_huisjes_';
/**
* @var array Validation rules
*/
public $rules = [
];
public function scopeListFrontEnd($query, $options = []){
extract(array_merge([
'page' => 1,
'perPage' => 10,
'price' => '',
'minprice' => '',
'maxprice' => ''
], $options));
$lastPage = $query->paginate($perPage, $page)->lastPage();
if($lastPage < $page){
$page = 1;
}
if($price && $minprice && $maxprice) {
$query->whereBetween('price', [$minprice, $maxprice]);
}
return $query->paginate($perPage, $page);
}
}