way to pass variable through url in codeigniter - codeigniter

I got a big search module in my Codeigniter project. Well simply I am passing variable to a view like
<a href=<?php echo site_url('controller/view/1'); ?>>View List</a>
And fetching its data in controller like
$id=$this->uri->segment(3);
For pagination
http://wwww.site.com/controller/view/<filter id>/<page from>
This is working perfectly in the case of simple query.
Now I got some more filter quires like
Country
State
City
Customer type
etc etc
then the url should be
http://wwww.site.com/controller/view/1/id2/id3/i4/id5
Is this the correct way to do the process ? If not please give a little advice...
I am new to codeigniter

The problem you are facing i have recently found a solution for this.
When you are first sending parameters through url use POST instead.
When you get the parameters you can pass them to session in a variable
type. Next time when you paginate get the type value from session and
put it in your query to get the desired result.
If you have more than 1 parameters you can put them in sessions and
unset them on certain conditions so that they are not called in every query.

I think the best approach here is to create another method in the controller something like filtered_view that accepts a filter_id and a page number, and that methode will fetch the data from the database with the provided filter and you'll use your pagination class as usual.
Hope this help.

Related

How to differentiate between two dynamic url in Laravel

I have two dynamic url with simillar structure. For example, lets say, Product page and category page.
I have set both pages in
Route::get('/{product}', [UsersController:: class, 'productDetail']);
Route::get('/{category}', [UsersController:: class, 'categoryProducts']);
But when I click on url which suppose to go in category page, it redirect to product page only because of same structure. How I can differentiate both URLs for Laravel without altering their url structure?
I don't think this can be done without modifying the URL pattern at least a little bit.
If you do something like /50?type=category then in the show method you can use the query parameter to determine which table to look at. But you'll have to use the same show method and I don't recommend doing it this way.
I hope someone else will be able to shine some more light on the matter.
this is the best practice for your case to make yourapi Resful
Route::get('/product/{product-id}', [UsersController:: class, 'productDetail']);
Route::get('/product/categories, [UsersController:: class, 'categoryProducts']);
learn more about Restful api here https://restfulapi.net/resource-naming/
This should be done by calling index, update diff() function. You can try by using the below:
Route::get('/category/{slug}', 'site\categorycontroller#show')->name('category.show');
Route::get('/product/{slug}', 'site\productcontroller#show')->name('product.show');

parse variable from view to controller laravel-backpack

im using Laravel-Backpack. could we parse some param from view to backpack controller? imagine i have some list of a package. each package has 5 round so every package has 5 button in its row. when i click first button(round 1) it will open the list of item that has round_id = 1.
what can i think is each round button in package list will parse id to route that will call backpackCrud Controller. and that id will used for advance queries for the item list
my button
<i>2</i>
my route
CRUD::resource('package/round/{RoundId}', 'Admin\CrudController');
my crud controller
$round= \Route::current()->parameter('RoundId');
$this->crud->addClause('where', 'round', '=', $round);
but it return an error
Route pattern "/admin/package/round/{RoundId}/{{RoundId}}" cannot reference variable name "RoundId" more than once.
what i know is we cannot parse param to restfull controller cause restfull is for quick crud. so what should i do for parse id from view to controller. or maybe there is beautifull way to make the queries? i trully appreciate that
Many thanks!
i found the way to avoid that.
it CANNOT be like:
CRUD::resource('package/round/{RoundId}', 'Admin\CrudController');
we have to follow the pattern of the default route like
CRUD::resource('someRoute/{someId}/someSecondRoute{someSecondId}', 'Admin\CrudController');
with that way, we can get the first and second paramater in controller with this:
$firstParam= \Route::current()->parameter('someID');
$SecondParam= \Route::current()->parameter('someSecondID');
hope this help someone else ;)

Laravel pagination get variables

I have a page that list apartments depending on book dates like this
mypage.com/finder?date-from=2011-03-04&date-to=2011-03-12
Everything is right, I am getting the date-from and date-get from the url and searching the database with those values. The problem is when I paginate and I click to go to another page the url changes to.
mypage.com/finder?page=9
and get an error Value must be provided
The correct url must be
mypage.com/finder?date-from=2011-03-04&date-to=2011-03-12&page=9
I am using paginate at the controller and $searchResult->links(); to generate the links
What can I do pass the date values from page to page so the pagination works?
Thanks
If you want to tack on existing query string data, use this:
$searchResult->appends(array(
'date-from' => Input::get('date-from'),
'date-to' => Input::get('date-to'),
));
Read the docs: Appending To Pagination Links.
You can shorten that a little:
$searchResult->appends( Input::only('data-from', 'date-to') );
which ends up being the same thing.
you can do this using the 'appends' feature. There are examples in the documentation: http://laravel.com/docs/pagination

How to call an action inside another action in Yii?

I've a situation where i get some information from database and based on the data , i want to do/forward to some other controller & action.
How can i do this using Yii? Its like an ajax request..
If i can use the CController->forward() , then how can use the post values for actions?
I shall assume that the reason why redirect() didn't work for you was because you can't sent post variables with it. If that's the case, then let me show you how to overcome the lack of POST support in redirect(). You can use setState(). It creates variables that simulate POST variables. This is the code to store or set a variable:
Yii::app()->user->setState('var', 'value');
And, in order to trace the value you just code as follows:
Yii::app()->user->getState('param1');
It would equally work with forward, but I'm not sure why you want to use it instead of redirect().

CakePHP session data cleared on paginator sort

My session data is being saved in my form as expected.
However, when I run a sort on any column of my results, my form session values are cleared.
I am calling in my search form through en element as it's used on specific locations of the site.
Does anyone know why pagination is clearing out my session? Is this standard Cake?
The paginator sort elements are simply a link generated by the paginator and won't consider any of your form data. The first thing you need to make sure that you're doing is tell the paginator to include any URL paramters for the current page in the url it generates. Put this anywhere in the view before you call any of the $paginator functions.
$paginator->options(array('url' => $this->passedArgs));
Secondly, make sure that your search parameters are being included in the URL. It sounds like they probably aren't. I just answered another question on the best practices of search result URLs here: CakePHP Search Results Best Practices
I solved this:
CakePHP session ID path or other method to share the results of a url - recommendations welcome

Resources