Removing query string from laravel url in pagination - laravel

I am using laravel pagination to paginate my products By default It gives me url like this
http://example.test/search?page=3
But I want some like
http://example.test/search/page/3
OR If Possible
http://example.test/search
On every page
This Is How I am doing it now but it is not giving me any result
{{ $books->appends(Request::except('page'))->links() }}
And I have also tried like this
$books = Book::paginate(10);
$books->withPath('/search');
But still no result

You can use this to generate custom link where you use link() in your blade.php.
#php
$links = $books->links();
$pattern = $replacement = array();
$pattern[] = '/'.$books->getCurrentPage().'\?page=/';
$replacement[] = '';
$customLinks = preg_replace($pattern, $replacement, $links);
echo $customLinks;
#endphp
Also you can use a package named laravel-paginateroute from spatie in github.

Related

Magento 2: Fetch availability date using dynamic variable

i am using 3rd party module that allow to use the Dynamic Variable to fetch data from magento to push into feed.
This is the code i am working on to get availability date from magento products but it is not returning any date.
$availability_dates = [];
foreach ($product->getAvailabilityDate() ?: [] as $availability_date) {
$availability_dates[] = $availability_date->getDate();
}
echo implode(', ', $availability_dates);
here is another working code that does fetch the categories associated with products
$categories = [];
foreach ($product->getCategoryCollection()->addNameToResult() as $category) {
$categories[] = $category->getName();
}
echo implode('|', $categories);

How do you extract only urls from mysql query in Codeigniter?

This is how my query looks like the Controller :
public function index(){
$this->load->library('lib_pagination');
$pg_config['sql'] = "SELECT * from SjohlLBzads_products JOIN SjohlLBzads_products_meta ON SjohlLBzads_products.post_id = SjohlLBzads_products_meta.post_id ";
$pg_config['per_page'] = 50;
$data = $this->lib_pagination->create_pagination($pg_config);
$this->load->view("product_listing", $data);
}
This is how how I display it in the view (product_listing.php)
<?php echo $data->gallery; ?>
The problem is the results I got from it not sorted out and contain mix of url and text :
a:6:{i:0;s:159:"https://ae01.alicdn.com/kf/HTB1AHOBOVXXXXazXpXXq6xXFXXXf/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:1;s:159:"https://ae01.alicdn.com/kf/HTB1ov9LOVXXXXbMXXXXq6xXFXXXl/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:2;s:159:"https://ae01.alicdn.com/kf/HTB1e4eHOVXXXXcbXXXXq6xXFXXXG/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:3;s:159:"https://ae01.alicdn.com/kf/HTB10qQ2RVXXXXbfXpXXq6xXFXXX5/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:4;s:159:"https://ae01.alicdn.com/kf/HTB164c0RVXXXXaSXpXXq6xXFXXXF/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:5;s:159:"https://ae01.alicdn.com/kf/HTB1OvumOVXXXXcqXFXXq6xXFXXXs/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";}
Is there any way that I can extract those 4 urls and call/display them separately ?
Hope this will help you :
Use unserialize to decode $data->gallery like this
$string = 'a:6:{i:0;s:159:"https://ae01.alicdn.com/kf/HTB1AHOBOVXXXXazXpXXq6xXFXXXf/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:1;s:159:"https://ae01.alicdn.com/kf/HTB1ov9LOVXXXXbMXXXXq6xXFXXXl/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:2;s:159:"https://ae01.alicdn.com/kf/HTB1e4eHOVXXXXcbXXXXq6xXFXXXG/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:3;s:159:"https://ae01.alicdn.com/kf/HTB10qQ2RVXXXXbfXpXXq6xXFXXX5/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:4;s:159:"https://ae01.alicdn.com/kf/HTB164c0RVXXXXaSXpXXq6xXFXXXF/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";i:5;s:159:"https://ae01.alicdn.com/kf/HTB1OvumOVXXXXcqXFXXq6xXFXXXs/COLROVIE-Work-Summer-Style-Women-Bodycon-Dresses-Sexy-2017-New-Arrival-Casual-Green-Crew-Neck-Half.jpg";}';
/*Replace $data->gallery with $string*/
$arr = unserialize($string);
print_r($arr);
Working example : https://eval.in/1000844
For more : http://php.net/manual/en/function.unserialize.php

Laravel where statement and pagination

I have an SQL search where I use Eloquent whereRaw, and than paginatate results.
Proplem: the results are paginated and if I navigate to the next page the results will be gone and it will just "SELECT All " bacause laravel's pagination sends only ?page= GET request.
code:
if (Input::has('city')) {
$query .= ' and city = '.Input::get('city');
}
if (Input::has('area')) {
$query .= ' and area = '.Input::get('area');
}
if (Input::has('sub_location')) {
$query .= ' and sub_location = '.Input::get('sub_location');
}
if (Input::has('rent')) {
$query .= ' and rent = '.Input::get('rent');
}
if (Input::has('price_min') && Input::has('price_max')) {
$query .= ' and price < '.Input::get('price_max').' and price >'.Input::get('price_min');
}
$listings = ListingDB::whereRaw($query)->paginate(5);
return View::make('page.index')->with('title','Home')->with('listings',$listings);
so when I navigate to the next page all the Input is gone because laravel generates pagination only with ?page= GET variable, how can I solve it?
thank you.
you can append you query string to the pagination link
{!! $listings->appends(Input::all())->render() !!}
"Appending To Pagination Links
You may add to the query string of pagination links using the appends method. For example, to append &sort=votes to each pagination link, you should make the following call to appends:
{!! $listings->appends(['sort' => 'value'])->links() !!}
So in your case you need to add to pagination links like this if you have city and rent search applied then
{!! $listings->appends(['city' => 'london', 'rent' => 5000])->links() !!}
This will sort out your problem.

Customize dynamic URL.change ?id to name

I'm using codeigniter for make dynamic website. In news page, I use url method like: http://test.com/test/test/news_details?id=27
how can i change my url to put news title within ?id=27
example:
http://test.com/test/test/news_details/pass-this-url
"pass-this-url" refer to id=27.
Best Regards.
Use routes : http://www.codeigniter.com/user_guide/general/routing.html
In your case it will be something like this :
$route['pass-this-url'] = "test/test/news_details?id=27";
So http://www.example.com/pass-this-url will be understand by codeigniter as http://www.example.com/test/test/news_details?id=27
however, if you want to make it dynamic, you will have to call your db :
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get( 'news' );
$result = $query->result();
foreach( $result as $row )
{
$route[$row->title] = "test/test/news_details?id=". $row->id;
//We suppose here that your title is URL friendly.
}
Go to application/config/config.php and check this:
$config['enable_query_strings'] = FALSE;

Having Problem with $_get in Codeigniter

I have a page called list.php which retrieves from database and shows all the student names with their respective ids in a list. In raw php, this is how I have done this-
include("connect.php");
$query = "SELECT * FROM marks ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);
mysql_close();
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$studentname = mysql_result($result,$i,"studentname");
$studentid = mysql_result($result,$i,"studentid");
?>
And then---
<? echo $studentname ?>
<?
++$i; } } else { echo "No Record Found"; }
?>
When a user clicks on any of the student names, it takes the user to the page of that particular student and in that page I have a code like following-
include("connect.php");
$number = $_GET['studentid'];
$qP = "SELECT * FROM student WHERE studentid = '$number' ";
$rsP = mysql_query($qP);
$row = mysql_fetch_array($rsP);
extract($row);
$studentid = trim($studentid);
$studentname = trim($studentname);
$studentgender = trim($studentgender);
The above code is working just fine. Now as far as I know $_get is disable in Codeigniter. But how to do the exact same thing that I mentioned above in codeigniter if $_get is disabled ? I went through some tutorials on alternative way of using $_get, but I didn't understand those well. Would you please kindly help? Thanks in Advance :)
The usage of $_GET is discouraged in CI.
The simpliest way to rewrite that files is not using $_GET. Just add method='post' to your forms and tell your ajax requests to use post, if you have any. Use $_POST in PHP instead of $_GET.
If you are absolutely sure you need get requests passing parameters, you have to enable query strings. You can do it in your CI's config file:
$config['enable_query_strings'] = TRUE;
See section 'Enabling query strings' for details. But enabling query strings will cause Url helper and other helpers that generate URLs to malfunction.
So my suggestion is to use POST requests.
UPDATE Replace
<? echo $studentname ?>
With
<? echo $studentname ?>
Create method studentprofile:
<?php
class Yourcontroller extends CI_Controller {
public function studentprofile($id)
{
include("connect.php");
$number = $id;
$qP = "SELECT * FROM student WHERE studentid = '$number' ";
$rsP = mysql_query($qP);
$row = mysql_fetch_array($rsP);
extract($row);
$studentid = trim($studentid);
$studentname = trim($studentname);
$studentgender = trim($studentgender);
// and so on...
}
}
?>

Resources