How to call a view through an item on the select menu? - php

Here is the view that displays the Select Menu that will call another View according to the selected filter:
<form action="<?= base_url() ?>process/processfilter" method="post" >
<div class="col-md-2" >
<div class="form-group">
<label>Filter</label>
<select class="form-control" name="situation" required autofocus>
<option value="All">List All</option> Here I'd like to call another method that has no filter.
<option value="Processing">Processing</option>
<option value="Concluded">Concluded</option>
<option value="Archived">Archived</option>
</select>
</div>
</div>
</form>
Function in Controller Class:
public function processfilter($situation) {
$this->load->model('ProcessModel', 'processo');
$dados['process'] = $this->process->getProcess($situation);
if ($this->process->getProcess($situation)) {
$this->load->view('/reports/process', $dados);
} else {
redirect('dashboard', 'refresh');
}
}
Function in Class Model:
function getProcesss($situation) {
$this->db->select('*');
$this->db->where("situation", $situation);
return $this->db->get('process')->result();
}
Note: I need to make a filter in the View like those in Excel.

Related

In laravel how to use if /else in controller

I am trying to pass value from view to controller, but it not working it was sent only "APPROVED"
my view
<div class="form-group row">
<label for="colFormLabelLg" class="col-md-2 col-sm-3 control-label control-label-lg"><b>DIGITAL SIGNATURE</b></label>
<div class="col-md-3">
<div class="form-group" id=digital_signature >
<select class="form-control" name="digital_signature" value="{{ old('digital_signature') }}" required autofocus >
<option value=""></option>
<option style="color:green">WITH DIGITAL SIGNATURE</option>
<option style="color:green">WITHOUT DIGITAL SIGNATURE</option>
</select>
</div>
</div>
</div>
my controller
public function new_approvel_update(Request $request, $id)
{
if($request->digital_signature == 'WITH DIGITAL SIGNATURE')
{
$input= Student::Where('delete_status','NOT DELETED')->find($id);
$input['center_approved'] = strtoupper ('APPROVED');
$input['date_of_join'] = $request->date_of_join;
} elseif($request->digital_signature == 'WITHOUT DIGITAL SIGNATURE') {
$input= Student::Where('delete_status','NOT DELETED')->find($id);
$input['center_approved'] = strtoupper ('NOT-APPROVED');
$input['date_of_join'] = $request->date_of_join;
}
$certificate->save();
return redirect('new_application')->with('success',' APPLICATION APPROVED SUCCESSFULLY .');
}
In option tag of select you haven't use value attribute. so it will pass null to a controller.
Change option tags as below :
<option value="APPROVED" style="color:green">WITH DIGITAL SIGNATURE</option>
<option value="NOT-APPROVED" style="color:green">WITHOUT DIGITAL SIGNATURE</option>
Change in your controller :
if($request->digital_signature == 'APPROVED'){
// your code
}
elseif($request->digital_signature == 'NOT-APPROVED'){
// your code
}

Get value of dropdownlist in view and use in controller Laravel

I have a dropdownlist in my view:
<form action="./sort" method="post">
<div class="sort-dropdown">
<select name="sort">
<option value="">Sort</option>
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
</select>
</div>
</form>
I want to get the value when user choose in dropdownlist, in my controller, I tried:
$sort = $request->sort;
But when I choose in dropdownlist and use dd($sort), it return null. How I can get the value? Thank you!
Update:
Here is my full controller:
public function search(Request $request) {
$search = $request->get('search');
$sort = $request->sort;
dd($search, $sort);
die;
$result = $this -> model -> getSearch($search, $sort);
$processData['processData'] = $result;
return view('datatracking', $processData);
}
use this and dd($array_name);
$array_name->sort = $request->sort;
You can get this from array
$request['sort'];
Your form is not getting submitted. To submit the form you need to add javascript .
<form action="./sort" method="post" >
<div class="sort-dropdown">
<select name="sort" onchange="this.form.submit()">
<option value="">Sort</option>
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
</select>
</div>
</form>
You can always take advantage of javascript's onchange. Maybe spice it up with a switch case?
Routes/web.php
Route::post('./sort', 'YourController#show')->name('sort-records');
Blade:
<form action="./sort" method="post">
<!-- pass your csrf token -->
#csrf
<div class="sort-dropdown input-group">
<!-- note the use of the onchange -->
<select name="sort" id="sort" class="form-control" onchange="this.form.submit()">
<option value="">Sort</option>
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
</select>
</div>
</form>
YourController:
public function show(Request $request)
{
switch ($request->sort)
{
case "asc":
// Grab your records accordingly
$variable = $x;
break;
case "desc":
// Grab your records accordingly
$variable = $y;
break;
default:
// Set a default sort option
$variable = $z;
break;
}
return view('datatracking', $variable);
}
<select name="sort" id="sort" class="form-control" >
<option value="Sort">Sort</option>
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
</select>

How to fetch data from database and display it in a drop down list using Codeigniter

I collect data from the database table and display it in the drop-down list, here's what I've done so far
THIS IS MY CONTROLLER:
public function assign_hostel() {
$inner_page_title = 'Assign Hostel';
$this->admin_header('Assign Hostel', $inner_page_title);
$data['hostel'] = $this->hostel_model->get_hostel();
$this->load->view('admin/hostel/assign_hostel');
$this->admin_footer();
}
THIS IS MY MODEL:
public function get_hostel() { //get all hostel
$this->db->order_by('hostel_name', 'asc');
return $this->db->get_where('school_hostel', array('hostel_name' =>
$hostel_name)->result();
}
MY VIEW:
<div class="form-group">
<label class="form-control-label">Select Hostel</label>
<select class="form-control" name="state_of_origin" required>
<option value="">-Select-</option>
<?php
$hostel_name = get_hostel();
foreach ($hostel_name as $hostel ) { ?>
<option value="<?php echo $hostel; ?>"><?php echo $hostel; ?></option>
<?php } ?>
</select>
</div>
Why am I getting an empty drop-down list?
Hope this will help you :
Pass $data['hostels'] in your view as given below :
First your controller assign_hostel should be like this :
public function assign_hostel()
{
$inner_page_title = 'Assign Hostel';
$this->admin_header('Assign Hostel', $inner_page_title);
$data['hostels'] = $this->hostel_model->get_hostel();
$this->load->view('admin/hostel/assign_hostel' ,$data);
$this->admin_footer();
}
Second your view should be like this :
<div class="form-group">
<label class="form-control-label">Select Hostel</label>
<select class="form-control" name="state_of_origin" required>
<option value="">-Select-</option>
<?php if ( !empty($hostels)) {
foreach ($hostels as $hostel ) { ?>
<option value="<?=$hostel->hostel_name; ?>"><?=$hostel->hostel_name;?></option>
<?php } }?>
</select>
</div>
For more : https://www.codeigniter.com/user_guide/general/index.html
You can't get $hostel from view directly to model, you must get from controller and pass it via $data to view
CONTROLLER:
public function assign_hostel() {
$inner_page_title = 'Assign Hostel';
$this->admin_header('Assign Hostel', $inner_page_title);
$data['hostels'] = $this->hostel_model->get_hostel();
$this->load->view('admin/hostel/assign_hostel', $data);
$this->admin_footer();
}
MODEL:
public function get_hostel() { //get all hostel
$this->db->order_by('hostel_name', 'asc');
return $this->db->get_where('school_hostel', array('hostel_name' =>
$hostel_name)->result();
}
VIEW:
<div class="form-group">
<label class="form-control-label">Select Hostel</label>
<select class="form-control" name="state_of_origin" required>
<option value="">-Select-</option>
<?php
foreach ($hostels as $hostel) { ?>
<option value="<?php echo $hostel; ?>"><?php echo $hostel; ?></option>
<?php } ?>
</select>
</div>
Pass $data to view function in controller
CONTROLLER:
public function assign_hostel() {
$inner_page_title = 'Assign Hostel';
$this->admin_header('Assign Hostel', $inner_page_title);
$data['hostel'] = $this->hostel_model->get_hostel();
$this->load->view('admin/hostel/assign_hostel', $data);
$this->admin_footer();
}
And in view get $hostel
VIEW:
<div class="form-group">
<label class="form-control-label">Select Hostel</label>
<select class="form-control" name="state_of_origin" required>
<option value="">-Select-</option>
<?php
foreach ($hostel as $hostel ) { ?>
<option value="<?php echo $hostel; ?>"><?php echo $hostel; ?></option>
<?php } ?>
</select>
There are few changes you need to make:
Change 1:
Pass the $data variable when you load the view:
$data['hostel'] = $this->hostel_model->get_hostel();
$this->load->view('admin/hostel/assign_hostel', $data); // $data as 2nd argument
Change 2:
Access key of $data array as variable in view file like this:
<div class="form-group">
<label class="form-control-label">Select Hostel</label>
<select class="form-control" name="state_of_origin" required>
<option value="">-Select-</option>
<?php
//$hostel_name = get_hostel(); NO NEED THIS LINE
foreach ($hostel as $h ) { ?>
<option value="<?php echo $h->hostel_name; ?>"><?php echo $h->hostel_name; ?></option>
<?php } ?>
</select>
</div>
I still need to know what columns you are fetching. Because in <option> tag you need to use the variable to access their attributes. Right now I have used the variable $h itself which is wrong.
You should use something like $h->name;

How to fetch data from database and display in html dropdown?

I am creating a simple form with fields like First_name, Last_name, city etc.So for city field, I want to display dynamic data.Below is the code I am using it's in PHP CodeIgniter.
Controller Page:
public function city()
{
$this->load->model('dropdownM');
$getcity=$this->dropdownM->get_city();
$this->load->view('form1city',$getcity);
}
Model Page:
<?php
class DropdownM extends CI_Model
{
public function get_city()
{
$this->db->select('fname');
$this->db->from('city');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result();
}
}
}
View page:
<form action="<?php echo base_url(); ?>index.php/Rec/user" method="post">
<select class="form-control" id="city" name="city">
<option value="">Select </option>
<?php if(count($getcity)):?>
<?php foreach($getcity as $city):?>
<option value=<?php echo $city->c_id;?>><?php echo $village1->C_name;?></option>
<?php endforeach;?>
<?php else:?>
<?php endif;?>
</select>
<center>
<input type="submit" value="Submit" class="btn btn-bricky" id="subbtn"
name="submit1">
</center>
<form>
It's not displaying anything in the drop-down.I am not able to find out what the issue.
pass data like this
$data['getcity']=$this->dropdownM->get_city();
$this->load->view('form1city',$data);
and in view
<?php if(count($getcity) > 0):?>
<select class="form-control" id="city" name="city">
<option value="">Select </option>
<?php foreach($getcity as $city):?>
<option value=<?php echo $city['c_id'];?>><?php echo $village1['C_name'];?></option>
<?php endforeach;?>
</select>
<?php else:?>
<p>No Category Found</p>
<?php endif;?>
In model
$this->db->select('fname');
$this->db->from('city');
$query = $this->db->get();
return $query->result_array();

Laravel ErrorException, Performing Multiple dynamic drop down

I have four tables:
asset_category table(columns 'asset_category_id', 'category'), an assets table(columns 'asset_id','asset_category_id','manufacturer_id', 'department_id'),
manufacturers table (columns manufacturers_id, manufacturer) and departments table (columns department_id, department). The asset table columns are linked with the other three table. I'm performing a dynamic drop down in the form to insert into the asset table
Asset.blade.php
<form action="{{url('/insertAsset')}}" method="POST" edata-parsley-validate novalidate>
{{ csrf_field() }}
<div class="form-group">
<label for="userName">Asset ID*</label>
<input type="text" name="asset_id" parsley-trigger="change" required placeholder="Asset ID" class="form-control" id="userName" disabled>
</div>
<div class="form-group">
<label for="userName">Asset Category ID*</label>
<select name="asset_category_id" parsley-trigger="change" required placeholder="Asset Category ID" class="form-control">
<option>Select an Asset Category</option>
#foreach( $asset_categories as $asset_category )
<option value=" {{$asset_category->asset_category_id}}"> {{ $asset_category->category }} </option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="userName">Asset Manufacture ID*</label>
<select name="manufacturer_id" parsley-trigger="change" required class="form-control">
<option>Select a Manufacturer</option>
#foreach( $manufacturers as $manufacturer )
<option value=" {{$manufacturer->manufacturer_id}}"> {{ $manufacturer->manufacturer }} </option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="userName">Department ID*</label>
<select name="department_id" parsley-trigger="change" required placeholder="Department ID" class="form-control" >
<option>Select a Department</option>
#foreach( $departments as $department )
<option value=" {{$department->department_id}}"> {{ $department->department }} </option>
#endforeach
</select>
</div>
<div class="form-group text-right m-b-0">
<button class="btn btn-primary waves-effect waves-light" type="submit">
Submit
</button>
</div>
AssetController
<?php
namespace App\Http\Controllers;
use App\Asset;
use App\Asset_category;
use App\Manufacturer;
use App\Department;
use Illuminate\Http\Request;
class AssetController extends Controller
{
public function asset_category(){
$asset_categories = Asset_category::all();
return view('asset', ['asset_categories' => $asset_categories]);
}
public function manufacturer(){
$manufacturers = Manufacturer::all();
return view('asset', ['manufacturers' => $manufacturers]);
}
public function department(){
$departments = Department::all();
return view('asset', ['departments' => $departments]);
}
}
Web.php
<?php
Route::get('/asset', function (){
return view('asset');
} );
Route::get('/asset', 'AssetController#asset_category');
Route::get('/asset', 'AssetController#department');
Route::get('/asset', 'AssetController#manufacturer');
/*POST */
Route::post('/insertAsset', 'AssetController#add');
When run the application, i get an ErrorException which states:
Undefined variable: asset_categories (View: C:\xampp\htdocs\laravel_app\resources\views\asset.blade.php).
After thorough testing i find out that, just one drop down displays because the laravel routes only one variable from one function among the 3 functions in the web.php and the other two variable from the other functions are not sent to the asset.blade.php. So now i want a way to route the other variables to the asset.blade.php. Please help if you understand it. Thanks in advance.
The first thing is that Laravel will only match the first route with the specific ID and specific type. In these case will only find the first route GET /asset.
To do what I undestand you want. You need to create only one method, that will return the three variables to the view
public function asset(){
$asset_categories = Asset_category::all();
$manufacturers = Manufacturer::all();
$departments = Department::all();
return view('asset', ['asset_categories' => $asset_categories,
'manufacturers' => $manufacturers, 'departments' => $departments]);
}
And only one route
Route::get('/asset', 'AssetController#asset');

Categories