Pagination doesn't work after perform search - pagination

I have a form in Laravel. I used pagination in this form and search option too. Pagination works fine when I didn't search anything. If I search something which has records more than 10 (my pagination record limit is 10), it shows result perfectly in first page but in second page or third page it shows the records of whole database. It's ignore the search criteria.
Controller Code:
public function index()
{
if (Input::has('invoiceNumber')) {
$invoiceNumber = Input::get('invoiceNumber');
$invoices = Invoice::where('invoiceNumber','like', '%'.$invoiceNumber.'%')->orderBy('date','desc')->paginate(10);
} elseif (Input::has('client')) {
$client = Input::get('client');
$invoices = Invoice::where('client','like', '%'.$client.'%')->orderBy('date', 'desc')->paginate(10);
} elseif (Input::has('startDate') && Input::has('endDate')) {
$startDate = Input::get('startDate');
$endDate = Input::get('endDate');
$invoices = Invoice::whereBetween('date', [$startDate, $endDate])->orderBy('date', 'desc')->paginate(10);
} elseif (Input::has('startDate')) {
$startDate = Input::get('startDate');
$invoices = Invoice::where('date', $startDate)->orderBy('date', 'desc')->paginate(10);
} elseif (Input::has('endDate')) {
$endDate = Input::get('endDate');
$invoices = Invoice::where('date', $endDate)->orderBy('date', 'desc')->paginate(10);
} else {
$invoices = DB::table('invoices')
->join('vehicles', 'invoices.vehicle', '=', 'vehicles.registration')
->select(
'invoices.*',
'vehicles.brand',
'vehicles.model',
'vehicles.seat',
'vehicles.remarks'
)
->orderBy('date', 'desc')
->paginate(10);
}
return view('invoice.index',compact('invoices'));
}
After performing search my URL look like this:
http://my-site.com/invoice?invoiceNumber=&client=&startDate=2016-01-01&endDate=2016-02-08
After this search when I click on page 2, the URL become this:
http://my-site.com/invoice?page=2

Try to use this bellow code (change as per your requirement)
return view('invoice.index')->withInvoices($invoices->appends(Input::except('page'));
Hope it's working for you...

In your view file, the render has to be like given below:
{!! $invoices->appends(Input::except('page'))->render() !!}

Simply use this on the result page.Pagination will be automatically visible at the bottom of the page
{!! $invoices->render() !!}

Related

Changing search method in Prestashop

The search function is as shown below matching results after 3 characters and input and they match the product name or description. I'm looking for a change in search function of MegaShop theme in Prestashop 1.7 as follows:
The search should be able to find words parts. In example, If the user writes "hi he", the search should be able to find "high heels". This should work also in other orders, lets say "he hi" (instead of "hi he") would return also "high heels" and every other article that match these word parts in different words.
Inside /root/modules/tptnsearch the file "tptnsearch-ajax-php contains:
<?php
require_once('../../config/config.inc.php');
require_once('../../init.php');
require_once(dirname(__FILE__).'/tptnsearch.php');
$tptnsearch = new TptnSearch();
$result_products = array();
$products = array();
$tptnsearch_key = Tools::getValue('search_key');
$context = Context::getContext();
$count = 0;
$product_link = $context->link;
if (Tools::strlen($tptnsearch_key) >= 3) {
$products = Product::searchByName($context->language->id, $tptnsearch_key);
$total_products = count($products);
if ($total_products) {
for ($i = 0; $i < $total_products; $i++) {
if (($products[$i]['name']) && ($products[$i]['active'])) {
$images = Image::getImages($context->language->id, $products[$i]['id_product']);
$product = new Product($products[$i]['id_product']);
$products[$i]['link'] = $product_link->getProductLink($products[$i]['id_product'], $product->link_rewrite[1], $product->id_category_default, $product->ean13);
$products[$i]['link_rewrite'] = $product->link_rewrite[1];
$products[$i]['id_image'] = $images[0]['id_image'];
$products[$i]['price'] = Tools::displayPrice(Tools::convertPrice($products[$i]['price_tax_incl'], $context->currency), $context->currency);
if ($count < Configuration::get('TPTN_SEARCH_COUNT')) {
$result_products[] = $products[$i];
$count ++;
} else {
break;
}
}
}
}
$context->smarty->assign(array(
'enable_image' => Configuration::get('TPTN_SEARCH_IMAGE'),
'enable_price' => Configuration::get('TPTN_SEARCH_PRICE'),
'enable_name' => Configuration::get('TPTN_SEARCH_NAME'),
'search_alert' => $tptnsearch->no_product,
'link' => $context->link,
'products' => $result_products,
));
$context->smarty->display(dirname(__FILE__).'/views/templates/hook/popupsearch.tpl');
} else {
echo '<div class="wrap_item">'.$tptnsearch->three_character.'</div>';
}
I believe changes must be done within this file.
I think your approach wouldn't give you desirable behavior. Basically, I think you need to create your own search query or override existed one and modify SQL query. Because now there are only LIKE %text% conditions and mean that your text should appear in the exact same way. So it means that that you are able to find "gh he" but not "hi he".
Or you can split your search request by gaps and then search by words doing the checking if all of them are in the request. But also I think it would be better to modify LIKE from %text% to %text to exclude duplicating and search only by words beginnings

kohana 3.3 get next value in array

i have an array that am calling from the database using kohana 3.3 i have gotten the data to the view and i can get it with print_r($variable). i want to create a movenext, movefirst,moveprevious and movelast buttons based on the data that is in the array.
my model looks like this
public function get_customer_list(){
$cust_list= DB::select('*')->from('customers')
->order_by('id','ASC')
->execute()
->as_array();
//print_r($cust_list);
return $cust_list;
my controller looks like this
$id=#$_POST["id"];
//echo $id;
$amend = Model::factory('amendments')->get_customer_list(#$d);
$this->page_title = 'amending';
$this->content = View::factory('forms/amendment_next');
//$this->rightwidget =view::factory('rightwidget');
$this->amend = $amend;
$this->next = $id;
my view looks like this
foreach ($amend[0] as $key=>$value){
echo '<p><span>'.$key.'</span><input id="'.$next.'" class="contact" type="text" name="'.$key.'"value="'.$value.'" /></p>';
}
Next Record
Previous Record<button >Next Record</button><button>Last Record</button>
i dont mind using a link or a button
please assist am stack here and my brain is locked.
Try this in your view, just a some javascript code is needed (i have used jQuery)
---next() Example---
$(document).ready(function (){
var customers = <?php echo json_encode($amend) ?>;
var count = 0;
function nextItem(){
if(count >= customers.length )return;
console.log("customer:",customers[count]['id'],customers[count]['name']);
count =++count;
}
})
Your controller should look like this:
$id=#$_POST["id"];
//echo $id;
$amend = Model::factory('amendments')->get_customer_list(#$d);
$this->page_title = 'amending';
$inputArray = array('amend'=>$amend, 'id'=>$id);
$this->content = View::factory('forms/amendment_next',$inputArray);
//$this->rightwidget =view::factory('rightwidget');
You either have to pass the variables you want to use in your view directly to the factory as an associative array or you have to make them globally availible to the factory.
Your view should look like this:
<?
foreach ($amend[0] as $key=>$value){
echo '<p><span>'.$key.'</span><input id="'.$next.'" class="contact" type="text" name="'.$key.'"value="'.$value.'" /></p>';
}
?>
Next Record
Previous Record<button >Next Record</button><button>Last Record</button>
after passing the $inputArray to the factory the vairable $amend will be filled with $inputArray['amend'] and $id will be filled with $inputArray['id']
I used your example to project myself over the problem. and now it stands solved here is the
The Controller looks like this now
$id=#$_GET["id"];
$amend = Model::factory('amendments')->get_customer_list(#$d);
$this->page_title = 'I&M CRB Online App amending';
$this->content = View::factory('forms/amendments');
//$this->rightwidget =view::factory('rightwidget');
$this->amend = $amend;
$this->next = $id;
the view looks something like this
<?php
if (!$next){
$next = "0";}
$nextid = $next+"1";
$previd=$next-"1";
?>
First Record |
Previous Record |
move next |
Last Record
<?php
foreach (#$amend[#$next] as $key=>$value){
echo "<p><span>".$key."</span><input class='contact' type='text' name='".$key."'value='".$value."' /></p>";
}
?>
and for the last record i had to use a different model function
public function get_last_customer(){
$last_customer= DB::select()->from('customers')
->order_by('id','DESC')
->limit(1)
->execute()
->as_array();
return $last_customer;
Thank you all for the support and the answers you provided me with.

Concrete5: Can I use $_GET variable for query string on Regular Page?

How does $_GET Variable works with Concrete5? Can I use that on regular page?
I know I can do this with single page via url segment, I'm just wondering if it is possible with regular page.
Example is :http://www.domain_name.com/about-us/?name=test...
Get-parameters are available via the controllers. In the view of a page or block use:
$this->controller->get("parameterName");
A cleaner way for custom parameters would be to define them in the function view() of the page controller. If at http://www.domain_name.com/about-us is your page and you define the view function of it's pagetype controller like this:
function view($name) {
$this->set("name", $name);
}
... and call the URL http://www.domain_name.com/about-us/test – then "test" will be passed under $name to your page view.
Note that controllers for page types must be in controllers/page_types/ and called BlablaPageTypeController ... with "PageType" literally being in there.
You can use it in a template. For instance, you can grab a variable...
$sort_by = $_GET['sort'];
And then use that variable in a PageList lookup, similar to:
$pl = new PageList();
$ctHandle = "teaching";
// Available Filters
$pl->filterByCollectionTypeHandle($ctHandle); //Filters by page type handles.
// Sorting Options
if ($sort_by == "name") {
$pl->sortByName();
} else {
$pl->sortBy('teaching_date', 'desc'); // Order by a page attribute
}
// Get the page List Results
$pages = $pl->getPage(); //Get all pages that match filter/sort criteria.
$pages = $pl->get($itemsToGet = 100, $offset = 0);
Then you can iterate over that array to print stuff out...eg
if ($pages) {
foreach ($pages as $page){
echo ''.$page->getCollectionName() . '<br />';
}
}
Props to the C5 Cheatsheet for the PageList code.

PHP variables not showing up. GETid not working properly

I have all the login scripts and profiles set up. I have a data table where all the profile information is stored called plus_signup but I can't seem to get this GET[id] thing to work. Am I missing something? Here's what I have.
When I view the page with the code I have now, there are blank areas where PHP is supposed to fill in the variables.
session_start();
include "include/z_db.php";
if ($_GET['id']){
$id = $_GET['id'];
} else if (isset($_SESSION['id'])) {
$id = $_SESSION['id'];
}
else {
print "important data to render this page is missing";
exit();
$sql = mysql_query("SELECT * FROM plus_signup WHERE id='$userid'");
while($row = mysql_fetch_array($sql)){
$userid = $row["userid"];
$name = $row["name"];
$location = $row["location"];
$sex = $row["sex"];
$aboutme = $row["aboutme"];
}
$check_pic = "users/$id/image01.jpg";
$default_pic = "users/0/image01.jpg";
if (file_exists($check_pic)){
$user_pic = "<img src=\"$check_pic\" width=\"175px\"/>";
} else {
$user_pic = "<img src=\"$default_pic\"/>";
}}
I know it's a little late and that you may have already figured out the answer to this question, but I was having the same issue and just figured it out.
Your SQL query is calling on a variable that has not been defined.
session_start();
include "include/z_db.php";
if ($_GET['id']){
$id = $_GET['id'];
} else if (isset($_SESSION['id'])) {
$id = $_SESSION['id'];
}
else {
print "important data to render this page is missing";
exit();
$sql = mysql_query("SELECT * FROM plus_signup WHERE id='$userid'");
while($row = mysql_fetch_array($sql)){
$userid = $row["userid"];
$name = $row["name"];
$location = $row["location"];
$sex = $row["sex"];
$aboutme = $row["aboutme"];
}
$check_pic = "users/$id/image01.jpg";
$default_pic = "users/0/image01.jpg";
if (file_exists($check_pic)){
$user_pic = "<img src=\"$check_pic\" width=\"175px\"/>";
} else {
$user_pic = "<img src=\"$default_pic\"/>";
}}
You are getting the 'id' from the url and storing it as $id. When you call your query, you are having it search for where ID = $userid when you really should be having it search for ID = $id.
Make sure that your variables are the same throughout!
^Hope that helps some people avoid an hour or two of frustration when trying to figure out why their function isn't working!

Kohana 3.0 paginator does not paginate

i have a problem with the kohana pagination. i have made a method for paginating elements from the database, (let's say 3 on a page), but, (though the pagination links are there), the pagination is not actually done, meaning i always get listed all the elements of the query.
the code i am using:
in a helper:
public function come_paginate_users(){
$count = Model::factory('user')->count_all(); //echo $count; exit();
$pagination = Pagination::factory(array(
'total_items' => $count,
'items_per_page' => 2,
'auto_hide' => TRUE,
));
$results = Model::factory('user')->order_by('user_id','ASC')->limit($pagination- >items_per_page)->offset($pagination->offset)->find_all();//->execute();
$page_links = $pagination->render();
return $pagination;
}
in the controller:
$pagination = #helper_utilities::come_paginate_users();
$this->view->pagination = $pagination;
and in the view:
<? echo 'Classic style: '.$pagination;?>
but... the pagination is not working. any idea why? thank you!
Actually, You need to return $page_links (wich is rendered html) and $result instead of $pagination object in your come_paginate_users() method
Maybe total_items < items_per_page and auto_hide hides the pagination? Try to set auto_hide to FALSE

Resources