I'm building a basically single page web application. In it's finished state, it will use AJAX where it's possible, but I'm working on it's static http fallback in the event if javascript is not available/turned off.
Anyway, there are multiple things that need to be in the url/uri in order to be linkable:
order by (first name, last name)
page number (if paginated)
search term (if selected)
state/county (if selected)
country (if selected)
order type (if selected)
This would look like something like this using only GET variables:
my.app?page=2&order=lastname&country=usa&state=colorado&searchterm=mysearchterms&ordertype=downloadable
But since if I'm right the convention in Laravel is to use named routes, so the URI above would look something like:
my.app/search/usa/colorado/mysearchterms/downloadable/page/2/lastname/
Also, since I'm developing this part without javascript, I need a form to submit the search+country+state+ordertype and links for the page and order, which means that when a user clicks a pagination link or order link, the URL would be overridden.
Is there a simple way to combine all of these into readable urls that don't override each other?
Why not still use GET query sting for search results as you indended?
Pass input from search form to Controller#method_A. Validate the input it and redirect to Controller#method_B - with additional GET params array:
return Redirect::route('search', array(
'order' => $order,
'country' => $contry,
'state' => $lastName
));
The method_B retrieves search results and returns the results page, which is linkable because of the query string in the URL.
You can get the query string params with standard Input::get('name').The pagination &page param should work with it with the GET string out of the box.
Related
I am using CakePHP and trying to come up with Idea of the live search. I have a very big table (6000+ entries) and would like to use input field to search entries based on the user input. But I am not sure where to start because then I cannot search all of the items in the controller as I used to.
This is what I did using select2, but I would like to combine it, I would like results to be shown via select where user can select multiple items, but my function should search just based on user input, whereas my way just filters all searched items.
<?php
echo $this->Form->create('Items', array('url' => 'itemToAdd', ))
echo $this->Form->input('itemToAdd', array('options' => $itemsToAdd));
echo $this->Form->end(__('Add this item'))
?>
So $itemsToAdd is a variable that is instatiated in the controller and gets all the entries.
$itemsToAdd = $this->Item->find('list');
Items schema:
items(id, code, name, description, created, modified)
But could I get some directions how could i tell AJAX only to search entries which are corresponding to the user input. I am not sure how to implement this on the technical level.
Send the input to the server.
Evaluate input.
Construct a search query
Return results
Render results on frontend
For this you need knowledge of HTTP, HTML, CSS, JavaScript, PHP and SQL.
I'm fairly new to php and am working on a script for filtering database entries via the GET method. After submitting the form I get URL results like the following (only query_string):
?mode=compilation&user_filter=Gerhard&user_filter=Harry
For reasons of shortness and for fitting into the existing script I need the URL formatted exactly like this (there's no alternative):
?mode=compilation&user_filter=Gerhard,Harry
"user_filter" is the name of the filtering variable. There are two more named "category_filter" and "tag_filter", so the URL could become very long. In addition the existing php script only filters the last user_filter parameter, whereas in the second example, which contains only one user_filter and separates the values by comma, the results are filtered like I wish and contain all postings from the users "Gerhard" and "Harry". This method works perfectly and also in combination with the other two filter variables exactly like intended, when I enter the URL manually, but I need it parsed/rewritten and sent automatically on submitting the form. Thereby the users would be able to select additional names/categories/tags (which are provided in drop-down lists) and add them one by one to the existing parameters as additional comma-separated values.
I suppose I have to receive the string by $_SERVER['QUERY_STRING'] and manipulate it by utilizing parse_str() or something. But how do I get to automatically unifing each additional identically named user_filter-parameter with the one preexisting user_filter-parameter into a comma-separated array of values and sending it to the page?
Or is there an other apporach?
The user, category and tag filters are provided in three separate select forms, each with it's own drop-down list and own submit button. After submitting the page gets reloaded with the new URL parameter and displays the results in an extra html talbe beneath the select forms. Preexisting selections are passed by input type="hidden" (hidden input fields), which all works fine.
If I had the solution for the user filter (as an example), I think I would be able to implement it also for the other two filter variables.
Regards
Taurec
I've built a webpage that, in its products page has this prototype for its url
http://www.example.com/products/show/some-really-nice-product/512
The last bit of the url is the product id, which serves the sole purpose of searching the db for the product.
But, is it possible, using routing, to be able to hide that part and still be possible to search the product by its id?
Like
http://www.example.com/products/show/some-really-nice-product/ ??
Thank you
If you don't want to put a number in your URL, you can't later search by a number/id :)
So you could add a VARCHAR field to the table, say "urlized", and use "some-really-nice-product" as its content for the given ID (you'll need to 'urlize' all your product names and make them unique). Don't forget to index your new field...
Then, you could access the product page using this URL which is very SEO friendly (ends with ".html"):
http://www.example.com/products/show/some-really-nice-product.html
...by adding a route to CodeIgniter, somehow like this:
$route['^products/show/(.*).html'] = 'products/show/$1';
In your controller/model, search the database for the string that is passed to "show" method, instead of the ID
if you hide id you can't retrieve that from the url anymore
if you want to hide that anyway just do:
$route['products/show/(:any)'] = "products/show/$1/$2";
NOTE :
It's sort of hard to google for this, because "GET" is bringing up a lot of "how to 'get' the value of a form with javascript," etc.
PROBLEM :
TL;DR version first
Can you use a multi-select from with the GET method of a form and still retrieve each individual value?
extended explanation
The requirements of a searchable catalog I'm building include using the GET method with the form, so that the user can see their results in the url, and also send a direct link to the search results to another customer, etc.
One of the searchable fields is a multi-select box (a select box with the MULTIPLE attribute). My back end is written in PHP, and I usually handle a multi-select by setting the name to an array variable (name="multiselect[]") and the post variable includes an array of the selected options ($_POST["multiselect"] == array()).
The problem in this case is that the multi-select values are passed to the get string like this:
action?multiselect=1&multiselect=2
So whatever the last value is replaces the value of the first initialization of the variable in the get string. (in the above example, multiselect would equal "2").
Trying to make the name an array just makes the array value replaced in the same manner, like this
action?multiselect[]=1&multiselect[]=2
Will result in $_GET["multiselect"] == 2
I had originally recommended using a checkbox, as it would allow us to name the elements differently and check for true/false on each one, but there are around 30 values for this particular multi-select, and they want it to be in a scroll-able area.
Works for me. Just tried
http://www.nearby.org.uk/tmp/multi-test.html
<select name="multiselect[]" ...
Results in a URL
?multiselect%5B%5D=2&multiselect%5B%5D=3
which will make $_GET['multiselect'] an array in PHP.
Perhaps you have something else in your system, stripping the [] ?
I have a link in my system as displayed above; 'viewauthorbooks.php?authorid=4' which works fine and generates a page displaying the books only associated with the particular author. However I am implementing another feature where the user can sort the columns (return date, book name etc) and I am using the ORDER BY SQL clause. I have this also working as required for other pages, which do not already have another query in the URI. But for this particular page there is already a paramter returned in the URL, and I am having difficulty in extending it.
When the user clicks on the a table column title I'm getting an error, and the original author ID is being lost!!
This is the URI link I am trying to use:
<th>Return Date</th>
This is so that the data can be sorted in order of Return Date. When I run this; the author ID gets lost for some reason, also I want to know if I am using correct layout to have 2 parameters run in the address? Thanks.
Yes, the layout is correct. Of course it's possible to have even more than 2 parameters in the query string
you have to check resulting query string (just take look into your address bar after click), there must be something like viewauthorbooks.php?authorid=22&orderby=returndate. If number is lost, you have to check it's source - $row['authorid'] variable.
you have to use & and sanitize your output, but apart from that it's correct ;)