Laravel Sort shoplist front end - php

Hi i'm doing a shop for a university project. This is my first course in web development and I don't understand how to sort a shoplist page by an html dropdown select in laravel.
Frontend Blade
<div class="toolbar-sorter">
<span>Sort By</span>
<select name="sorter" class="sorter-options" style="width:150px; "data-role="sorter">
<option selected="selected" value='comic_name'>Titolo: A-Z </option>
<option value='comic_name'> Titolo: Z-A </option>
<option value='price'> Prezzo: Crescente </option>
<option value='price'> Prezzo: Decrescente </option>
<option value='created_at'> Ultimi Arrivati </option>
</select>
</div>
Route.php (This is not the only route of shoplist)
Route::get('/shoplist', 'ComicController#shoplistBase');
ComicController
public function shoplistBase()
{
$genres = Genre::all();
$comics = Comic::paginate(9);
return view('shoplist')->with(compact('genres'))->with(compact('comics'));
}

Assuming that you are using the same route for your sorting:
Wrap your drop-down in a form
Make sure to use unique values for each options
<form action="/shoplist" method="GET">
<div class="toolbar-sorter">
<span>Sort By</span>
<select name="sorter" class="sorter-options" style="width:150px; " data-role="sorter">
<option selected="selected" value='comic_name_asc'>Titolo: A-Z</option>
<option value='comic_name_desc'> Titolo: Z-A</option>
<option value='price_asc'> Prezzo: Crescente</option>
<option value='price_desc'> Prezzo: Decrescente</option>
<option value='created_at'> Ultimi Arrivati</option>
</select>
</div>
<button type="submit">Filter</button>
</form>
Handle the filter request in your Controller
public function shoplistBase(Request $request)
{
$genres = Genre::all();
if ($request->has('sorter')){
switch($request->get('sorter')){
case `comic_name_asc`:
$comics = Comic::orderBy('name', 'desc')->paginate(9);
break;
case `comic_name_desc`:
//..
break;
}
} else {
$comics = Comic::paginate(9);
}
return view('shoplist')->with(compact('genres'))->with(compact('comics'));
}

Related

codeigniter php search date wise not showing all results

i have a codeigniter website where i have simple search box, which is like below:
<form method="post" action="<?php echo base_url()?>homecontroller/search">
<label>Month: </label>
<select class="form-select" id="basicSelect" name="month">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<label>Year: </label>
<select class="form-select" id="basicSelect" name="year">
<option value="2022">2022</option>
<option value="2021">2021</option>
<option value="2020">2020</option>
<option value="2019">2019</option>
<option value="2018">2018</option>
<option value="2017">2017</option>
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
</select>
<input style="margin-top:24%" type="submit" class="btn btn-primary ml-1" name="billing" value="SEARCH">
</form>
my controller looks like:
$month=$this->input->post('month');
$year=$this->input->post('year');
$stotal=$year.'-'.$month;
$data['search'] = $this->user->searchbilling($stotal);
and i did the following code in model to fetch results:
public function searchbilling($stotal) {
$this->db->select('*, SUM(quantity) as quantity');
$this->db->from('delivered');
$this->db->like('date', $stotal);
$this->db->group_by('YEAR(date), MONTH(date), customer');
$query = $this->db->get();
$result = $query->result();
return $result; }
however this is noy showing all the results from that particular month, can anyone please tell me whats wrong in here, thanks in advance
You can try like this where("DATE_FORMAT(date,'%Y-%m') =", $stotal)
public function searchbilling($stotal) {
$this->db->select('*, SUM(quantity) as quantity');
$this->db->from('delivered');
$this->db->where("DATE_FORMAT(date,'%Y-%m') =", $stotal);
$this->db->group_by('YEAR(date), MONTH(date), customer');
$query = $this->db->get();
$result = $query->result();
return $result;
}
You can do like this where("DATE_FORMAT(date,'FORMAT YOU NEED') =", $stotal)
public function searchbilling($stotal) {
$this->db->select('*, SUM(quantity) as quantity');
$this->db->from('delivered');
$this->db->where("DATE_FORMAT(date,'FORMAT YOU NEED') =", $stotal);
$this->db->group_by('YEAR(date), MONTH(date), customer');
$query = $this->db->get();
$result = $query->result();
return $result;
}

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>

Working with session in PHP

I have a form from which i take some datas and using session i want to keep that datas as a history on my page, and also when i click on one line from my history i want my datas to be autocomplete in my form. I saw an example on one page and i tried doing it to apply to what i want but it's not quite functional.
This is my code for the form:
<form method="get">
Create:<br><br>
Name:<br> <input type="text" id="name" value="" /><br>
surname:<br> <input type="text" id="surname" value="" /><br>
Sex:<br> <div class="select-wrapper">
<select name="sex" id="sex">
<option value="">- Sex -</option>
<option value="woman">Woman</option>
<option value="male">Male</option>
</select>
</div>
Role: <div class="select-wrapper">
<select name="rol" id="rol">
<option value="">- Role -</option>
<option value="visitor">Visitor</option>
<option value="Professor">Professor</option>
<option value="Student">Student</option>
</select>
</div>
Text color: <div class="select-wrapper">
<select name="cul" id="cul">
<option value="">- Text color -</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="black">Black</option>
</select>
</div>
Font text: <div class="select-wrapper">
<select name="font" id="font">
<option value="">- Font text -</option>
<option value="15px Arial">Arial</option>
<option value="15px Times New Roman">Times New Roman</option>
<option value="15px Georgia">Georgia</option>
<option value="15px Comic Sans MS">Comic Sans MS</option>
<option value="15px Lucida Sans Unicode">Lucida Sans Unicode</option>
<option value="15px Courier New">Courier New</option>
</select>
</div>
Format : <div class="select-wrapper">
<select name="format" id="format">
<option value="">- Format -</option>
<option value="portrait">Portrait</option>
<option value="landscape">Landscape</option>
</select>
</div>
Style text: <div class="select-wrapper">
<select name="stil" id="stil">
<option value="">- Style text -</option>
<option value="stil1">Stil1</option>
<option value="stil2">Stil2</option>
<option value="stil3">Stil3</option>
<option value="stil4">Stil4</option>
<option value="stil5">Stil5</option>
<option value="stil6">Stil6</option>
</select>
</div>
</form>
And this is where i tried to use session:
<?php
session_start();
$createpas = parseRequest();
storecreatepas($createpas);
include "form.php";
$createpases = $_SESSION['createpases'];
include "history.php";
function storecreatepas($createpas) {
if (!isset($_SESSION['createpases'])) {
$_SESSION['createpases'] = [];
}
if (!$createpas->isEmpty()) {
$_SESSION['createpases'][] = $createpas;
}
}
function parseRequest() {
$createpas = new createpasRequest;
$createpas->cul = !empty($_GET['cul']) ? $_GET['cul'] : "";
$createpas->font = !empty($_GET['font']) ? $_GET['font'] : "";
$createpas->format = !empty($_GET['format']) ? $_GET['format'] : "";
$createpas->stil = !empty($_GET['stil']) ? $_GET['stil'] : "";
return $createpas;
}
/**
* createpas request
*/
class createpasRequest
{
public $cul = "";
public $font = "";
public $format = "";
public $stil = "";
function toQueryString() {
$params = [
'cul' => $this->cul,
'font' => $this->font,
'format' => $this->format,
'stil' => $this->stil
];
return http_build_query($params);
}
function isEmpty() {
return !$this->cul || !$this->font || !$this->format || !$this->stil;
}
function culAsObject() {
return new DateTime($this->cul);
}
function fontAsObject() {
return new DateTime($this->font);
}
function formatAsObject() {
return new DateTime($this->format);
}
function stilAsObject() {
return new DateTime($this->stil);
}
}
And the display code:
<ul>
<?php
foreach ($createpases as $s) {
?>
<li><a href="search.php?<?php echo $s->toQueryString() ?>">
<?php echo $s->cul?> - <?php echo $s->font?> - <?php echo $s->format?> - <?php echo $s->stil?>
</a></li>
<?php
}
?>
</ul>
It works just fine until some point. It gets my datas from my form posts them on the page but when i click on them they don't autocomplete in in my form. And also if i want the options to be unique in the list how can i do that? Right now if i complete the same datas 2 or 3 times they appear multiple times in my list.
Thank you for your help!
You need to start the session in the Form page and then check if the SESSION array contains the values to be echoed.
So your code will become:
<?php session_start();
if(!isset($_SESSION['name']){$_SESSION['name']=''}
if(!isset($_SESSION['sex']){$_SESSION['sex']=''}
if(!isset($_SESSION['rol']){$_SESSION['rol']=''}
?>
//this way you will not have issues on page first load before the user fills in the form
<form method="get">
Create:<br><br>
Name:<br> <input type="text" id="name" value="<?php echo $_SESSION['name']; ?>" /><br>
surname:<br> <input type="text" id="surname" value="" /><br>
Sex:<br> <div class="select-wrapper">
<select name="sex" id="sex">
<option value="" <?php if($_SESSION['rol']==''){echo 'selected'} ?>>- Sex -</option>
<option value="woman" <?php if($_SESSION['rol']=='woman'){echo 'selected'} ?>>Woman</option>
<option value="male" <?php if($_SESSION['rol']=='visitor'){echo 'selected'} ?>>Male</option>
</select>
</div>
Role: <div class="select-wrapper">
<select name="rol" id="rol">
<option value="" <?php if($_SESSION['rol']==''){echo 'selected'} ?>>- Role -</option>
<option value="visitor" <?php if($_SESSION['rol']=='visitor'){echo 'selected'} ?>>Visitor</option>
<option value="Professor"<?php if($_SESSION['rol']=='Professor'){echo 'selected'} ?>>Professor</option>
<option value="Student"<?php if($_SESSION['rol']=='student'){echo 'selected'} ?>>Student</option>
</select>
</div>
and so on.
It appears that your form does not auto-fill with the data because you do not have such a functionality implemented. This auto-complete functionality is not automatic, as in, the server does not do it for you.
Here is an example of what you need to do in order to get the autocomplete to work:
Name:<br> <input type="text" id="name" value="<?= $_SESSION['name'] ?>" /><br>
For the select to auto-complete, you'll need to do something like this:
<select name="cul" id="cul">
<option value="">- Text color -</option>
<option value="red" <? echo ($_SESSION['cul']==="red")?"selected='selected'":""; ?>>Red</option>
<option value="blue" <? echo ($_SESSION['cul']==="blue")?"selected='selected'":""; ?>>Blue</option>
<option value="black" <? echo ($_SESSION['cul']==="black")?"selected='selected'":""; ?>>Black</option>
</select>
This sets the value for each form element by directly setting the value / selected item using the $_GET data from a previous submission, or if there is no data, everything will be empty / default values.
Per a suggestion from one of the comments to this answer, I would like to point out that you must include "session_start()" to the top of the document if you wish to use the session variables on the document (if you have not done so already).
One question about the original question: is that first code block procedurally generated, or hand-typed?

Show or hide HTML code based on URL

I am working on a form and I've stumbled upon a problem. I have different users in which I want them to see different values for one of my SELECT tags; so far I am using PHP to get the URL and if $field == 'donor' then it takes off some of the options. Here's the code:
function defineUser($field){
if(isset($_GET['user']) && urldecode($_GET['user'])){
$field = urldecode($_GET['user']);
if($field == "donor"){
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>Select...</option>
<option value=''>Option 1</option>
<option value=''>Option 2</option>
<option value=''>Option 3</option>
<option value='Other'>Option 4</option>
</select>
</p>";
} else if ($field != "donor") {
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>...</option>
</select>
</p>";
} else {
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>...</option>
</select>
</p>";
}
}
}
This is my function, now on the page source itself I simply have,
<?php echo defineUser($_GET['user']); ?>
My question simply is how can I get each user to see a standard options set - inside my select tag - based on the URL the user has been sent from?
Your code could be simplified:
function defineUser() {
$user = isset($_GET['user']) ? urldecode($_GET['user']) : null;
switch ($user) {
case 'donor':
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>Select...</option>
<option value=''>Option 1</option>
<option value=''>Option 2</option>
<option value=''>Option 3</option>
<option value='Other'>Option 4</option>
</select>
</p>";
break;
default:
return "<p>
<label for=designation>Designation</label>
<select name='merchant_defined_field_4' id='merchant_defined_field_4' required=''>
<option value=''>...</option>
</select>
</p>";
}
}
You don't need to pass it $_GET['user'] since that is globally accessible, unless you're worried about dependency. Second, you were essentially repeating the same block of HTML twice in your if else clause, using a switch here makes more sense.

Repeated HTML in wordpress functions.php

I am creating a few shortcodes In the functions.php file for our wordpress site and am working on how best to implement it.
A selection list of 240 countries that can be chosen from will be in a few different shortcodes and instead of placing the entire list 4 or 5 times I was wondering if we could implement it one time and make the call from within the function.
This is what we have right now:
function form_function() {
ob_start();
?>
...
<select id="fc" name="fc" title="From Country">
<option value="United States">United States</option>
<option value="Afghanistan">Afghanistan</option>
...
<option value="Zimbabwe">Zimbabwe</option>
</select>
...
<select id="tc" name="tc" title="To Country">
<option value="United States">United States</option>
<option value="Afghanistan">Afghanistan</option>
...
<option value="Zimbabwe">Zimbabwe</option>
</select>
...
<?php
$html = ob_get_contents();
ob_end_clean();
return $html;
}
and we would like to change it to something like:
function countrylist() {
<option value="United States">United States</option>
<option value="Afghanistan">Afghanistan</option>
...
<option value="Zimbabwe">Zimbabwe</option>
}
function form_function() {
ob_start();
?>
...
<select id="fc" name="fc" title="From Country">
<? countrylist () ?>
</select>
...
<select id="tc" name="tc" title="To Country">
<? countrylist () ?>
</select>
<?php
$html = ob_get_contents();
ob_end_clean();
return $html;
}
Any thoughts on how best to do this? Thanks in advance
Maybe better change countrylist() function to
function countrylist()
{
$countries = array('USA', 'Canada', 'Ukraine');
foreach($countries as $country)
{
$html .= '<option value="'.$country.'">'.$country.'</option>'
}
return $html;
}
And in form_function() replace <? countrylist () ?> with <?= countrylist () ?>

Categories