For my website I want to display search result (on a complete new page) using the parameters in the dropdown select input. I have 4 dropdowns and I tried multiple different ways to make it work. But it doesn't seem to work!
Below is my CodeIgniter 4 code
Home Controller
The home controller file which gathers data from 4 dropdown inputs the data is fetched as an array. The array is then executed through the getSearch method located in the home model.
public function search_residential() {
$data['getfooter'] = $this->homeModel->getFooterInfo();
$params = array(
'category' => $this->request->getVar('category'),
'area' => $this->request->getVar('area'),
'bedrooms' => $this->request->getVar('bedrooms'),
'washrooms' =>$this->request->getVar('washrooms'),
);
$data['search'] = $this->homeModel->getSearch($params);
return view('search', $data);
}
Home Model
The home model contains the query of search.
//Searching data
public function getSearch($params) {
$category = $params['category'];
$area = $params['area'];
$bedrooms = $params['bedrooms'];
$washrooms = $params['washrooms'];
$builder= $this->db->table('tblresidential');
$builder->select('*');
$builder->where('1 = 1');
if ($category != '') {
$builder->where('category', $category);
}
if ($area != '') {
$builder->where('area', $area);
}
if ($bedrooms != '') {
$builder->where('bedrooms', $bedrooms);
}
if ($washrooms != '') {
$builder->where('washrooms', $washrooms);
}
$result = $builder->get();
return $result;
}
HTML Code
The HTML code that is used for execution
<div class="search__area-inner">
<?= form_open('/home/search_residential'); ?>
<div class="row">
<div class="col-6 col-lg-3 col-md-3">
<div class="form-group">
<select name="category" class="wide select_option">
<option data-display="Property Type">Property Type</option>
<option value="Property on sale">Property on Sale</option>
<option value="Property on rent">Property on Rent</option>
</select>
</div>
</div>
<div class="col-6 col-lg-3 col-md-3">
<div class="form-group">
<select name="area" class="wide select_option">
<option data-display="Select Area">Select Area</option>
<option value="2000">2000</option>
<option value="1800">1800</option>
<option value="1500">1500</option>
<option value="1200">1200</option>
<option value="900">900</option>
<option value="600">600</option>
<option value="300">300</option>
<option value="100">100</option>
</select>
</div>
</div>
<div class="col-6 col-lg-3 col-md-3">
<div class="form-group">
<select name="bedrooms" class="wide select_option">
<option data-display="Select Bedrooms">Select Bedrooms</option>
<option value="1 bedroom">1</option>
<option value="1.5 bedrooms">1.5</option>
<option value="2 bedrooms">2</option>
<option value="2.5 bedrooms">2.5</option>
<option value="3 bedrooms">3</option>
<option value="4 bedrooms">4</option>
<option value="5 bedrooms">5</option>
</select>
</div>
</div>
<div class="col-6 col-lg-3 col-md-3">
<div class="form-group">
<select name="washrooms" class="wide select_option">
<option data-display="Select Washrooms">Washrooms</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</div>
<div class="col-12">
<div class="form-group">
<button class="btn btn-primary text-uppercase btn-block"> search
<i class="fa fa-search ml-1"></i>
</button>
</div>
</div>
</div>
<?= form_close(); ?>
</div>
Result View (the page on which result shows)
This is the page where result shows
<?php if(!empty($search)) : ?>
<section class="residential-property-list">
<div class="container">
<div class="row">
<?php foreach($search as $properties) : ?>
<div class="col-lg-12">
<div class="card__image card__box-v1">
<div class="row no-gutters">
<div class="col-md-4 col-lg-3 col-xl-4">
<div class="card__image__header h-250">
<a href="<?= base_url('/home/residential_property'); ?>/<?= $properties->id; ?>">
<img src="<?= $properties->imageone; ?>" alt="featured-image" class="img-fluid w100 img-transition">
<div class="info text-white"> for <?= $properties->category; ?></div>
</a>
</div>
</div>
<div class="col-md-4 col-lg-6 col-xl-5 my-auto">
<div class="card__image-body">
<h6 class="text-capitalize">
<?= $properties->project; ?>
</h6>
<ul class="list-inline card__content">
<li class="list-inline-item">
<span>
Bedrooms <br>
<i class="fa fa-inbox"></i> <?= $properties->bedrooms; ?>
</span>
</li>
<li class="list-inline-item">
<span>
Washrooms <br>
<i class="fa fa-map"></i> <?= $properties->washrooms; ?>
</span>
</li>
<li class="list-inline-item">
<span>
Property Facing <br>
<i class="fa fa-inbox"></i> <?= $properties->facing; ?>
</span>
</li>
<li class="list-inline-item">
<span>
Property area <br>
<i class="fa fa-map"></i> <?= $properties->area; ?>
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</section>
<?php else: ?>
<br>
<br>
<div class="container"><h4 class="text-center">No Result Found Yet</h4></div>
<br>
<br>
<?php endif; ?>
The model's get(...) method returns false|ResultInterface.
To receive the actual result set,
Instead of:
(Home Model)
// ...
$result = $builder->get();
return $result;
// ...
Use this:
Result Arrays
// ...
return $builder->get()->getResult();
// ...
Related
I am building search filtering for my site and I am trying to use livewire to build the search component. The search component has a few filters and one of them is a categories dropdown with a subcategories dropdown below it. When a top-level category is selected it should update the subcategories dropdown with the sub-categories of the top-level category. This should happen dynamically without updating the rest of the component, but the actual search results should only be returned when the apply filter button is pressed.
But the subcategories dropdown is not being updated even though if I dd on $this->subcategories I can see the variable has actually been updated. What am I doing wrong here:
<div class="col-xs-12 col-sm-5 col-md-4 col-lg-3">
<aside id="tg-sidebar" class="tg-sidebar">
<form wire:submit.prevent="submitSearch" class="tg-formtheme tg-formnerrowsearch">
<div class="tg-sidebartitle"><h2>Narrow Your Search:</h2></div>
<fieldset>
<div id="tg-narrowsearchcollapse" class="tg-themecollapse tg-narrowsearchcollapse" wire:ignore>
<div class="tg-collapsetitle">
<i class="fa fa-rotate-left"></i>
<span class="fa fa-angle-down">By Title/Keyword</span>
</div>
<div class="tg-themecollapsecontent">
<div class="form-group tg-inputwithicon">
<i class="icon-magnifier"></i>
<input wire:model.defer="keyword" id="keyword" type="search" name="keyword" class="form-control" placeholder="Search Title/Keyword">
</div>
</div>
<div class="tg-collapsetitle">
<i class="fa fa-rotate-left"></i>
<span class="fa fa-angle-down">By Category</span>
</div>
<div class="tg-themecollapsecontent">
<div class="form-group">
<div class="tg-select">
<select wire:model="category" id="category" name="category">
<option disabled>Select Category</option>
#foreach ($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<div class="tg-selectgroup">
<select wire:model.defer="subcategory" id="subcategory" name="subcategory">
<option disabled>Select Subcategory</option>
#if(count($subcategories) > 0)
#foreach ($subcategories as $subcategory)
<option value="{{$subcategory->id}}">{{$subcategory->name}}</option>
#endforeach
#endif
</select>
</div>
</div>
</div>
<div class="tg-collapsetitle">
<i class="fa fa-rotate-left"></i>
<span class="fa fa-angle-down">By Location</span>
</div>
<div class="tg-themecollapsecontent">
<div class="form-group tg-inputwithicon">
<i class="fa fa-crosshairs"></i>
<input wire:model.defer="city" id="city" type="text" name="city" class="form-control" placeholder="Town/City">
</div>
<div class="form-group tg-inputwithicon">
<i class="fa fa-map-marker"></i>
<input wire:model.defer="postcode"id="postcode" type="text" name="postcode" class="form-control" placeholder="Postcode">
</div>
<div class="form-group">
<div class="tg-select">
<select wire:model.defer="distance" id="radius" name="radius">
<option>Distance</option>
<option value="1">Within 1 mile</option>
<option value="5">Within 5 miles</option>
<option value="10">Within 10 miles</option>
<option value="15">Within 15 miles</option>
<option value="20">Within 20 miles</option>
<option value="25">Within 25 miles</option>
<option value="30">Within 30 miles</option>
<option value="35">Within 35 miles</option>
<option value="40">Within 40 miles</option>
<option value="45">Within 45 miles</option>
<option value="50">Within 50 miles</option>
<option value="55">Within 55 miles</option>
<option value="60">Within 60 miles</option>
<option value="70">Within 70 miles</option>
<option value="80">Within 80 miles</option>
<option value="90">Within 90 miles</option>
<option value="100">Within 100 miles</option>
<option value="150">Within 150 miles</option>
<option value="200">Within 200 miles</option>
<option value="1200">Nationwide</option>
</select>
</div>
</div>
</div>
<div class="tg-collapsetitle">
<i class="fa fa-rotate-left"></i>
<span class="fa fa-angle-down">By Ads Type</span>
</div>
<div class="tg-themecollapsecontent">
<div class="tg-checkboxgroup">
<span class="tg-checkbox">
<input wire:model.defer="featured" type="checkbox" id="tg-featuredads" name="featured" value="1">
<label for="tg-featuredads">
<span>Featured Ads</span>
<span>987327</span>
</label>
</span>
<span class="tg-checkbox">
<input wire:model.defer="urgent" type="checkbox" id="tg-urgentads" name="urgent" value="1">
<label for="tg-urgentads">
<span>Urgent Ads</span>
<span>48952413</span>
</label>
</span>
<span class="tg-checkbox">
<input wire:model.defer="auction" type="checkbox" id="tg-auctionads" name="auction" value="1">
<label for="tg-auctionads">
<span>Auctions</span>
<span>48952413</span>
</label>
</span>
<span class="tg-checkbox">
<input wire:model.defer="will_post" type="checkbox" id="tg-postageads" name="will_post" value="1">
<label for="tg-postageads">
<span>Willing to Post</span>
<span>48952413</span>
</label>
</span>
</div>
</div>
</div>
<button type="submit" class="tg-btn">apply filter</button>
</fieldset>
</form>
</aside>
</div>
<div class="col-xs-12 col-sm-7 col-md-8 col-lg-9">
<div id="tg-content" class="tg-content">
<div class="tg-contenthead">
<div class="tg-sortandview">
<div class="tg-sortby">
<strong>Sort by:</strong>
<div class="tg-select">
<select>
<option value="Most Recent">Most Recent</option>
<option value="Most Recent">Most Recent</option>
<option value="Most Recent">Most Recent</option>
</select>
</div>
</div>
<div class="tg-views">
<strong>View As Grid</strong>
<ul>
<li class="tg-active"><a href="javascript:void(0);"><i
class="fa fa-th-large"></i></a></li>
<li><i class="fa fa-reorder"></i></li>
</ul>
</div>
</div>
<div class="tg-applyedfilters">
<ul>
<li class="alert alert-dismissable fade in">
×
<span>Manchester</span>
</li>
<li class="alert alert-dismissable fade in">
×
<span>0 - 0</span>
</li>
<li class="alert alert-dismissable fade in">
×
<span>Brand New</span>
</li>
<li class="alert alert-dismissable fade in">
×
<span>Featured Ads</span>
</li>
<li class="alert alert-dismissable fade in">
×
<span>Area Unit: Sq. ft.</span>
</li>
<li class="alert alert-dismissable fade in">
×
<span>Sq. ft. 2500 - Sq. ft.5000</span>
</li>
<li class="alert alert-dismissable fade in">
×
<span>Rooms: 04</span>
</li>
<li class="alert alert-dismissable fade in">
×
<span>2014 - 2016</span>
</li>
<li class="alert alert-dismissable fade in">
×
<span>1500 - 3000</span>
</li>
<li><a class="tg-btncleall" href="javascript:void(0);">Clear All</a></li>
</ul>
</div>
</div>
<div class="tg-ads">
<div class="row">
#if(empty($results))
<div class="alert alert-warning">
<strong>Sorry!</strong> No Adverts Found.
</div>
#else
#foreach($results as $result)
<div class="col-xs-6 col-sm-12 col-md-6 col-lg-4 tg-verticaltop">
<div class="tg-ad tg-verifiedad">
<figure>
<span class="tg-themetag tg-featuretag">featured</span>
<a href="{{route('listing.view', $result)}}">
#if(isset($result->images->where('order', '=', 1)->first()->name))
<img src="{{asset ('storage/ad_images/thumbnails/'.$result->images->where('order', '=', 1)->first()->name)}}" title="{{$result->images->where('order', '=', 1)->first()->title}}" alt="{{$result->images->where('order', '=', 1)->first()->alt}}">
#else
#endif
</a>
<span class="tg-photocount">See {{$result->images->count()}} Photos</span>
</figure>
<div class="tg-adcontent">
<ul class="tg-productcagegories">
<li>{{$result->categories->last()->name}}</li>
</ul>
<div class="tg-adtitle">
<h3>{{$result->title}}</h3>
</div>
<time datetime="2017-06-06">Last Updated: {{\Carbon\Carbon::createFromTimeStamp(strtotime($result->updated_at))->diffForHumans()}}</time>
<div class="tg-adprice"><h4>£{{$result->price}}</h4></div>
<address>
#foreach (json_decode($result->user->addresses->where('id', '=', $result->user_address_id)) as $address)
{{$address->city.' '.$address->post_code}}
#endforeach
</address>
<div class="tg-phonelike">
<a class="tg-btnphone" href="javascript:void(0);">
<i class="icon-phone-handset"></i>
<span data-toggle="tooltip" data-placement="top" title="Show Phone No." data-last="{{$result->user->contacts->where('id', '=', $result->user_contact_id)->first()->mobile}}"><em>Show Phone No.</em></span>
</a>
<span class="tg-like tg-liked"><i class="fa fa-heart"></i></span>
</div>
</div>
</div>
</div>
#endforeach
#endif
</div>
</div>
{{-- <nav class="tg-pagination">
namespace App\Http\Livewire;
use Livewire\Component;
use App\Category;
use App\User;
use App\Advert;
class Listings extends Component
{
public $keyword;
public $categories = [];
public $category;
public $subcategories = [];
public $subcategory;
public $city;
public $postcode;
public $distance;
public $featured;
public $urgent;
public $auction;
public $will_post;
public $results;
public $model;
public function mount($model, $paginate = '10')
{
$this->model = $model;
$this->paginate = $paginate;
$this->categories = Category::whereNull('parent_id')->get();
$this->subcategories = Category::with('children')->where('parent_id', $this->categories->first()->id)->get();
}
public function submitSearch()
{
$advert = new Advert();
if($this->featured){
$advert = $advert->where('mark_ad_featured', '1');
}
if($this->urgent){
$advert = $advert->where('mark_ad_urgent', '1');
}
if($this->auction){
$advert = $advert->where('is_auction', '1');
}
if($this->auction){
$advert = $advert->where('is_auction', '1');
}
if($this->will_post){
$advert = $advert->where('local_pickup', '0');
}
$advert = $advert->where('status', '1');
$advert->orderBy('updated_at', 'asc');
$this->results = $advert->get();
}
public function render()
{
if(!empty($this->category)) {
$this->subcategories = Category::where('parent_id', $this->category)->get();
}
//dd($this->subcategories);
return view('livewire.listings');
}
}
I have tried using the updated lifecycle hook for category property change but it still does not work:
public function updatedCategory($value)
{
if($value) {
$this->category = $value;
$this->subcategories = Category::where('parent_id', $this->category)->get();
}
}
could you change the wire:ignore in this line for wire:ignore.self or not use it if isn't necessary ??? i can't figure it out anything else in that code
<div id="tg-narrowsearchcollapse" class="tg-themecollapse tg-narrowsearchcollapse" wire:ignore.self>
This question already has answers here:
Invalid argument supplied for foreach()
(20 answers)
Closed 4 years ago.
I trying to create a survey website, so I found some example that I want to test it and change it as I want, but the problem is when I choose the question type to be "Radio" OR "Checkbox" I get this error message:
Invalid argument supplied for foreach() (View: /Users/ANA/laravel/app/resources/views/survey/detail.blade.php)
I know now there is something wrong passing to foreach, but I don't know what!.
It's my first time to use Laravel, that why I'm struggling to know what's going on.
And this is the highlighted code after the error message:
</div>
</div>
<?php elseif($question->question_type === 'radio'): ?>
<?php $__currentLoopData = $question->option_name; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<p style="margin:0px; padding:0px;">
<input type="radio" id="<?php echo e($key); ?>" />
<label for="<?php echo e($key); ?>"><?php echo e($value); ?></label>
</p>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?php elseif($question->question_type === 'checkbox'): ?>
<?php $__currentLoopData = $question->option_name; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<p style="margin:0px; padding:0px;">
<input type="checkbox" id="<?php echo e($key); ?>" />
<label for="<?php echo e($key); ?>"><?php echo e($value); ?></label>
</p>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?php endif; ?>
<?php echo Form::close(); ?>
This is the code detail.blade.php:
#extends('master')
#section('title', 'Survey')
#section('content')
<div class="card">
<div class="card-content">
<span class="card-title"> {{ $survey->title }}</span>
<p>
{{ $survey->description }}
</p>
<br/>
Edit Survey | View Answers Delete Survey
<!-- Modal Structure -->
<!-- TODO Fix the Delete aspect -->
<div id="doDelete" class="modal bottom-sheet">
<div class="modal-content">
<div class="container">
<div class="row">
<h4>Are you sure?</h4>
<p>Do you wish to delete this survey called "{{ $survey->title }}"?</p>
<div class="modal-footer">
Yep yep!
<a class=" modal-action modal-close waves-effect waves-light green white-text btn">No, stop!</a>
</div>
</div>
</div>
</div>
</div>
<div class="divider" style="margin:20px 0px;"></div>
<p class="flow-text center-align">Questions</p>
<ul class="collapsible" data-collapsible="expandable">
#forelse ($survey->questions as $question)
<li style="box-shadow:none;">
<div class="collapsible-header">{{ $question->title }} Edit</div>
<div class="collapsible-body">
<div style="margin:5px; padding:10px;">
{!! Form::open() !!}
#if($question->question_type === 'text')
{{ Form::text('title')}}
#elseif($question->question_type === 'textarea')
<div class="row">
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea"></textarea>
<label for="textarea1">Provide answer</label>
</div>
</div>
#elseif($question->question_type === 'radio')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<input type="radio" id="{{ $key }}" />
<label for="{{ $key }}">{{ $value }}</label>
</p>
#endforeach
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<input type="checkbox" id="{{ $key }}" />
<label for="{{$key}}">{{ $value }}</label>
</p>
#endforeach
#endif
{!! Form::close() !!}
</div>
</div>
</li>
#empty
<span style="padding:10px;">Nothing to show. Add questions below.</span>
#endforelse
</ul>
<h2 class="flow-text">Add Question</h2>
<form method="POST" action="{{ $survey->id }}/questions" id="boolean">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="input-field col s12">
<select class="browser-default" name="question_type" id="question_type">
<option value="" disabled selected>Choose your option</option>
<option value="text">Text</option>
<option value="textarea">Textarea</option>
<option value="checkbox">Checkbox</option>
<option value="radio">Radio Buttons</option>
</select>
</div>
<div class="input-field col s12">
<input name="title" id="title" type="text">
<label for="title">Question</label>
</div>
<!-- this part will be chewed by script in init.js -->
<span class="form-g"></span>
<div class="input-field col s12">
<button class="btn waves-effect waves-light">Submit</button>
</div>
</div>
</form>
</div>
</div>
#endsection
Could you please help me, I'm struggling here, I don't know what is wrong.
I found the problem, everything was correct except this line in the master.blade.php:
{!! MaterializeCSS::include_css() !!}
AND
{!! MaterializeCSS::include_js() !!}
I change them to be like the other and the code now working ^^. Just change them to look like this one :
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
I am working on a project I need to select option from html dropdown list and then display the data selected from database. Just like filter and display the filtered data from database.
Still not working
Am getting error:
Its just displaying all data from db not by filtered query
<form action="search.php" method="POST">
<div class="md-form">
<select class="mdb-select" name="state_search" required>
<option disabled selected>Choose your State</option>
<option name="state" value="Abia State">Abia State</option>
<option name="state" value="Adamawa State">Adamawa State</option>
<option name="state" value="Anambra State">Anambra State</option>
</select>
</div>
<div class="col-lg-4">
<div class="md-form">
<select class="mdb-select" name="school_search">
<option value="" disabled selected>Choose your School</option>
<option value="Michael Opkara University">Mouau</option>
<option value="University of Benin">UniBen</option>
<option value="University of Porthacourt">UniPort</option>
</select>
</div>
<button type="submit" class="btn purple-gradient btn-lg" name="filter">Search <i class="fa fa-paper-plane-o ml-1"></i></button>
</div>
</form>
PHP
<?php
$school_search = $connect->real_escape_string($_POST["school_search"]);
$state_search = $connect->real_escape_string($_POST["state_search"]);
$query = mysqli_query($connect, "SELECT * FROM roomate WHERE (`state` LIKE '%".$state_search."%') OR (`school` LIKE '%".$school_search."%')") or die(mysql_error());
if($query){
while($row = mysqli_fetch_array($query)){
?>
<div class="container" style="padding-top:70px;">
<!--Grid row-->
<div class="row">
<?php
foreach ($query as $user) {?>
<div class="col-lg-4 col-md-12 mb-r">
<!--Featured image-->
<div class="overlay hm-white-slight z-depth-1-half mb-2">
<img src="<?php echo $user['room_avatar']?>" class="img-fluid" alt="avatar image">
<a><div class="mask"></div></a>
</div>
<!--Excerpt-->
<a href="" class="pink-text">
<h6>
<i class="fa fa-map"></i><strong><?php echo $user['city'];?>, <?php echo $user['state'];?></strong><br>
<small><?php echo $user['school'];?></small>
</h6>
</a>
<h4><?php echo $user['type'];?></h4>
<p>
by <a><strong><?php echo $user['room_name'];?></strong></a>
<?php echo $user['created'];?>
</p>
<label class="badge badge-danger"> non-verified </label>
<a href="contact.php">
<label class="badge badge-primary">Report Scam</label>
</a>
<p><?php echo $user['discription'];?></p>
<a class="btn btn-pink btn-rounded">Contact <?php echo $user['room_name'];?></a>
</div>
<?php
} ?>
</div>
</div>
<?php
}
}
else{
echo(mysql_error());
}
?>
Need help making this work out
The name of your select needs to match the $_POST variable key.
So in your case your <select> need the name state_search.
or change your $_POST search to $_POST['state'] to match your select name.
also you should use the $state_search variable in your query instead of $query.
You may have more issues , just pay attention to details.
You put to much HTML in echo function.
<?php
$school_search = $connect->real_escape_string($_POST["school_search"]);
$state_search = $connect->real_escape_string($_POST["state_search"]);
$query = mysqli_query($connect, "SELECT * FROM roomate WHERE (`room_name` LIKE '%".$query."%') OR (`discription` LIKE '%".$query."%') OR (`discription` LIKE '%".$query."%') OR (`type` LIKE '%".$query."%')") or die(mysql_error());;
$result = mysqli_query($connect, $query);
if($result){
while($row = mysqli_fetch_array($result)){
?>
<div class="container" style="padding-top:70px;">
<!--Grid row-->
<div class="row">
<?php
foreach ($query as $user) {?>
<div class="col-lg-4 col-md-12 mb-r">
<!--Featured image-->
<div class="overlay hm-white-slight z-depth-1-half mb-2">
<img src="<?php echo $user['room_avatar']?>" class="img-fluid" alt="avatar image">
<a><div class="mask"></div></a>
</div>
<!--Excerpt-->
<a href="" class="pink-text">
<h6>
<i class="fa fa-map"></i><strong><?php echo $user['city'];?>, <?php echo $user['state'];?></strong><br>
<small><?php echo $user['school'];?></small>
</h6>
</a>
<h4><?php echo $user['type'];?></h4>
<p>
by <a><strong><?php echo $user['room_name'];?></strong></a>
<?php echo $user['created'];?>
</p>
<label class="badge badge-danger"> non-verified </label>
<a href="contact.php">
<label class="badge badge-primary">Report Scam</label>
</a>
<p><?php echo $user['discription'];?></p>
<a class="btn btn-pink btn-rounded">Contact <?php echo $user['room_name'];?></a>
</div>
<?php
} ?>
</div>
</div>
<?php
}
}
else{
echo(mysql_error());
}
Am using filters to display the products accordingly.
I got filtered result correctly. But I cannot retain the filtered results at next filter.
i.e. Am using three filter.
Created at - Date filter
Price - Filter
Seller - Filter
Everything working separately.
But I cannot make it at the next next filtering results. Also, I cannot make the checked value to be checked and selected value to be selected. How can I do it? My code I given below:
Controller:
$category_id = Category::where('name', $main)->value('id');
if($category_id == "")
{
$all_categories_id = Category::pluck('id');
}
else
{
$all_categories_id = Category::where('parent_id', $category_id)->pluck('id');
$all_categories_id->push($category_id);
}
$product_id = Product::where('name', 'like','%'.$request->name.'%')->pluck('id');
$id = ProductCategory::whereIn('product_id', $product_id)->whereIn('category_id', $all_categories_id)->pluck('id');
$product_id = Product::whereIn('id', $id)->pluck('id');
$seller = Product::whereIn('id', $id)->pluck('seller_id');
$seller_name = User::whereIn('id', $seller)->get();
$products = Product::where(function($query) use ($product_id){
$min_price = Input::has('min_price') ? Input::get('min_price') : null;
$max_price = Input::has('max_price') ? Input::get('max_price') : null;
$seller = Input::has('seller') ? Input::get('seller') : [];
if(isset($min_price) && isset($max_price)){
$query->whereBetween('price_per_unit', [$min_price, $max_price])
->whereIn('id', $product_id);
}
if(isset($days)){
foreach($days as $key => $day){
$end = Carbon::now();
$get_required_date = Carbon::now()->subDays($day);
$products = Product::where('created_at', '>', $get_required_date)->pluck('id');
$query->whereIn('id', $products)->whereBetween('price_per_unit', [$min_price, $max_price]);
}
}
if(isset($seller)){
foreach($seller as $key => $sell){
$query->whereIn('id', $product_id)
->where('seller_id', $sell);
}
}
$query->whereIn('id', $product_id);
})->get();
}
//Categories Name in Sidebar
$category = ProductCategory::whereIn('product_id', $id)->pluck('parent_id');
$category_name = Category::whereIn('id', $category)->pluck('name');
//Categories Name in dropdown
$main_categories = Category::where('parent_id', '0')->pluck('name');
//Profile Image Mapping
$user = Auth::user();
$directory = 'uploads/users/images/'.$user->id;
$main_categories = Category::where('parent_id', '0')->pluck('name');
if (is_dir($directory)) {
$files = scandir ($directory);
$img_file = $directory.'/'.$files[2];
$user['front_img'] = $img_file;
}
$profile = $user['front_img'];
//Product Image Mapping
$products->map(function ($product) {
$directory = 'uploads/products/images/'.$product->id;
$brand = Brand::select('name')->where('id', '=', $product->brand)->pluck('name');
$brand_name = $brand->first(function($value, $key) {
return $key == 'name';
});
if (is_dir($directory)) {
$files = scandir ($directory);
$img_file = $directory.'/'.$files[2];
$product['front_img'] = $img_file;
$product['brand'] = $brand_name;
return $product;
}
return $product;
});
return view('pages/product', compact('main_categories','seller_name', 'profile', 'products', 'main', 'category_name'));
And my Blade code:
#extends('layouts.master')
#section('content')
<section class="bc-product">
<div class="container">
<div class="row" >
<div class="breadcrumb-width">
<div class="col-sm-6">
<ul class="bc-breadcrumb">
<li>Home
</li>
<li class="active category">{{$main}}
</li>
</ul>
</div>
<div class="col-sm-6">
<div class="pull-right">
<ul class="bc-Produc-mlist">
<li>View:Sort :</li>
<li> Popular
</li>
<li> NewDiscount
</li>
<li>Price: LowHigh
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="row" >
<div class="bc-Product-left hidden-sm hidden-xs">
<div class="bc-categories-left">
<span class="bc-expand-title">CATEGORIES</span>
<a class="bc-expand">
<div class="bc-expand-icon">[ - ]</div>
</a>
<div class="bc-detail-list">
<ul class="bc-categories-list">
#foreach($category_name as $m)
<li><i class="fa fa-caret-right" aria-hidden="true"></i> {{$m}}</li>
#endforeach
</ul>
</div>
</div>
<form id="filterform">
<div class="bc-categories-left">
<div class="bc-product-mtb">
<span class="bc-expand-title">New Arrival</span>
<a class="bc-expand">
<div class="bc-expand-icon">[ - ]</div>
</a>
<div class="bc-detail-list">
<div class="checkbox checkbox-select">
<input id="7" name="days[]" value="7" type="checkbox"/>
<label for="7"> Last 07 days </label>
</div>
<div class="checkbox checkbox-select">
<input value="15" name="days[]" id="15" type="checkbox"/>
<label for="15">Last 15 days </label>
</div>
<div class="checkbox checkbox-select">
<input id="30" name="days[]" value="30" type="checkbox"/>
<label for="30">Last 30 days </label>
</div>
<div class="checkbox checkbox-select">
<input id="45" name="days[]" value="45" type="checkbox"/>
<label for="45"> Last 45 days </label>
</div>
</div>
</div>
</div>
<div class="bc-categories-left">
<div class="bc-product-mtb">
<span class="bc-expand-title">Price</span>
<a class="bc-expand">
<div class="bc-expand-icon">[ - ]</div>
</a>
<div class="bc-detail-list">
<div class="bc-range-bar">
<div class="bc-range-handle-left"></div>
<div class="bc-range-handle-right"></div>
</div>
<!--<input id="range" type="range" min="20" max="200"/>-->
<div class="form-group bc-form-pric-sm">
<select name="min_price" class="bc-select">
<option value="0">Min</option>
<option value="200" >$200</option>
<option value="500" >$500</option>
</select>
</div>
<div class="form-group bc-input-group">
<select name="max_price" class="bc-select">
<option value="1000000000000000">Max</option>
<option value="2000" >$2000</option>
<option value="5000" >$5000</option>
</select>
</div>
</div>
</div>
</div>
<div class="bc-categories-left">
<div class="bc-product-mtb">
<?php $seller = Input::has('seller') ? Input::get('seller') : []; ?>
<span class="bc-expand-title">Seller</span>
<a class="bc-expand">
<div class="bc-expand-icon">[ - ]</div>
</a>
<div class="bc-detail-list">
<?php $seller = Input::has('seller') ? Input::get('seller') : []; ?>
#foreach($seller_name as $seller)
<div class="checkbox checkbox-select">
<input id="{{$seller->id}}" name="seller[]" type="checkbox" value="{{$seller->id}}">
<label for="{{$seller->id}}"> {{$seller->name}} </label>
</div>
#endforeach
</div>
</div>
</div>
</div>
<button class="active" style="display: none;">ok</button>
<div class="bc-Product-right">
#foreach($products as $product)
<div class="bc-product-col">
<div class="bc-product-list">
<div class="bc-product-listimg">
<img src="{{ asset($product->front_img) }}" alt="Women">
</div>
<div class="bc-actions">
<a class=" bc-buy-line bc-view" href="#">View</a>
<a class="bc-buy" href="/buy-now">Buy</a>
</div>
<span class="bc-brand-name">{{$product->name}}</span>
<span class="bc-price">{{$product->price_per_unit}}</span>
</div>
</div>
#endforeach
</form>
<div class="text-right">
<div class="page-nation">
<ul class="pagination pagination-large">
<li class="disabled"><span>Prev</span></li>
<li class="active"><span>1</span></li>
<li>2
</li>
<li>3
</li>
<li>4
</li>
<li class="disabled"><span>...</span></li>
<li>
<li><a rel="next" href="#">Next</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</section>
<script>
$("input[name='seller[]']").click(function() {
$( "button.active" ).trigger( "click" );
});
$("input[name='days[]']").click(function() {
$( "button.active" ).trigger( "click" );
});
$("select[name='max_price'").change(function() {
$( "button.active" ).trigger( "click" );
});
// $("#range").change(function(){
// var val = $('#range').val();
// console.log(val);
// });
</script>
#endsection
In such scenario, you can use ajax() instead of form submit. But if you don't want to use ajax() and continue with the form submit approach then you have to maintain the state on your own. To do this you have to return from controller which elements are checked or what value is selected in dropdown and make it checked and selected on behalf of these values. laravel provide this approach return redirect with input
You can use it like:
return Redirect::back()->withInput(Input::all());
how can i get the id passed with the url and save it in an input ? and the controller is like that :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use URL;
use DB;
class productController extends Controller
{
//
public function getIndex(){
$in=URL::get('id');
$product=DB::table('products')->get(['id','name','salary','sale','p_salary','n_salary','best','type','image_name','category_id','sub_id','incategory_id','new']);
return view('contents.products')->with('product',$product)
->with('in',$in);
}
}
and the view is like that :
#extends('master')
#section('content')
<form method="POST" action="{{action('productController#getIndex')}}" enctype="multipart/form-code">
{!! csrf_field() !!}
<div class="em-wrapper-main">
<div class="container container-main">
<div class="em-inner-main">
<div class="em-wrapper-area02"></div>
<div class="em-wrapper-area03"></div>
<div class="em-wrapper-area04"></div>
<div class="em-main-container em-col1-layout">
<div class="row">
<div class="em-col-main col-sm-24">
<div class="page-title category-title">
<h1>Shoes</h1>
</div>
<div class="category-products">
<div class="toolbar-top">
<div class="toolbar">
<div class="pager">
<p class="amount"> Items 1 to 12 of 20 total</p>
<div class="pages">
<ol>
<li class="current">1</li>
<li>2
</li>
<li>
<a class="next i-next" href="#" title="Next"> <img src="{{asset('assets/images/pager_arrow_right.gif')}}" alt="Next" class="v-middle" /> </a>
</li>
</ol>
</div>
</div><!-- /.pager -->
<div class="sorter">
<p class="view-mode">
<label>View as:</label> <strong title="Grid" class="grid">Grid</strong> List
</p>
<div class="sort-by toolbar-switch">
<div class="toolbar-title">
<label>Sort By</label>
<select class="sortby" name="sortby">
<option value="position" selected="selected"> Position</option>
<option value="name"> Name</option>
<option value="price"> Price</option>
</select>
</div>
<a href="#" title="Set Descending Direction"><img src="{{asset('assets/images/i_asc_arrow.png')}}" alt="Set Descending Direction" class="v-middle" />
</a>
</div>
<div class="limiter toolbar-switch">
<div class="toolbar-title">
<label>Show</label>
<select class="toolbar-show">
<option value="12" selected="selected"> 12</option>
<option value="24"> 24</option>
<option value="36"> 36</option>
</select>
</div>
</div>
</div><!-- /.sorter -->
</div>
</div><!-- /.toolbar-top -->
<div id="em-grid-mode">
<ul class="emcatalog-grid-mode products-grid emcatalog-disable-hover-below-mobile">
#foreach($product as $p)
#foreach($in as $n)
#if($p->incategory_id == $n->id)
<li class="item first">
<div class="product-item">
<div class="product-shop-top">
<a href="{{action('productdetailController#getProduct' , array('id' => $p->id))}}" title="{{$p->name}}" class="product-image">
<img class="em-img-lazy img-responsive em-alt-hover" src="{{asset('images/')}}/{{$p->image_name}}" width="220" height="220" alt="WIASSI Version 1" />
<img id="product-collection-image-206" class="em-img-lazy img-responsive em-alt-org" src="{{asset('images/')}}/{{$p->image_name}}" width="220" height="220" alt="WIASSI Version 1" />
<span class="bkg-hover"></span> </a>
<div class="bottom">
<div class="em-btn-addto text-center ">
<button type="button" title="Add to Cart" class="button btn-cart" onclick="206"><span><span>Add to Cart</span></span>
</button>
<ul class="add-to-links">
<li><button type="submit" class="link-wishlist" name="submit" title="Add to Wishlist">Add to Wishlist</button>
</li>
<li>Add to Compare
</li>
</ul>
</div>
<div class="quickshop-link-container"> Quickshop
</div>
</div>
</div>
<div class="product-shop">
<div class="f-fix">
<h2 class="product-name text-center "> {{$p->name}} </h2>
<div class=" text-center">
<div class="ratings">
<div class="rating-box">
<div class="rating" style="width:%"></div>
</div> <span class="amount">(0)</span>
</div>
</div>
<div class="text-center ">
<div class="price-box"> <span class="regular-price" id="product-price-206"> <span class="price" content="{{$p->salary}}">${{$p->salary}}</span> </span>
</div>
</div>
<div class="desc std text-center em-element-display-hover">{{$p->name}}</div>
</div>
</div>
</div><!-- /.product-item -->
</li>
#endif
#endforeach
#endforeach
</ul>
</div><!-- /.em-grid-mode -->
<div class="toolbar-bottom em-box-03">
<div class="toolbar">
<div class="pager">
<p class="amount"> Items 1 to 12 of 20 total</p>
<div class="pages">
<ol>
<li class="current">1</li>
<li>2
</li>
<li>
<a class="next i-next" href="#" title="Next">
<img src="{{asset('assets/images/pager_arrow_right.gif')}}" alt="Next" class="v-middle" /> </a>
</li>
</ol>
</div>
</div><!-- /.pager -->
<div class="sorter">
<p class="view-mode">
<label>View as:</label> <strong title="Grid" class="grid">Grid</strong> List
</p>
<div class="sort-by toolbar-switch">
<div class="toolbar-title">
<label>Sort By</label>
<select class="sortby" name="sortby">
<option value="position" selected="selected"> Position</option>
<option value="name"> Name</option>
<option value="price"> Price</option>
</select>
</div>
<a href="#" title="Set Descending Direction"><img src="{{asset('assets/images/i_asc_arrow.png')}}" alt="Set Descending Direction" class="v-middle" />
</a>
</div>
<div class="limiter toolbar-switch">
<div class="toolbar-title">
<label>Show</label>
<select class="toolbar-show">
<option value="12" selected="selected"> 12</option>
<option value="24"> 24</option>
<option value="36"> 36</option>
</select>
</div>
</div>
</div><!-- /.sorter -->
</div>
</div><!-- /.toolbar-bottom -->
</div><!-- /.category-products -->
</div>
</div>
</div><!-- /.em-main-container -->
</div>
</div>
</div><!-- /.em-wrapper-main -->
</form>
#stop
how to get the products that have the incategory_id passed with the url ?
You could use the Request facade to get the id and then, use the where method to get the requested product like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
use DB;
class productController extends Controller
{
public function getIndex()
{
$in=Request::get('id');
$product=DB::table('products')->where('incategory_id', $in)->get(['id','name','salary','sale','p_salary','n_salary','best','type','image_name','category_id','sub_id','incategory_id','new']);
return view('contents.products')->with('product',$product);
}
}
Also, you could type-hint the Illuminate\Http\Request:
public function getIndex(Request $request)
{
$in=$request->get('id');
// ...
}