Maintaining search form input - php

I'm building a CRUD application and need to implement two forms of search:
The basic search function accepts a keyword and searches every column in the database table for that particular keyword. The search keyword is sent as an URL-parameter in the form www.website.com/category?q=keyword. Nothing too special here...
In the advanced search form users can specify up to 5 keywords and for each of those they can select a column from the database table which needs to be searched for this keyword. In short: the advanced search form takes up to 5 keyword-column pairs. This form gets submitted via POST (because i want to avoid hitting limitations on maximum number of URL-characters).
The results need to be paginated, showing 10 records per page. I've searched for pagination classes online, but they all fetch the total amount of records in the database table, and then return the selection that needs to be displayed for that page. Because I'm working with a large set of data here, I can't afford to do it this way.
This lead me to create my own pagination class, which takes the requested page number (and if applied, the $_GET and $_POST parameters from the search forms). Based on that it calculates the total number of results, calculates the total number of pages, decides whether there's a 'previous' and 'next' page... It also returns an SQL statement (with LIMIT and OFFSET values) to be executed by the particular Model (using my own MVC architecture (aka no framework)). This allowed me to only fetch the results for the requested page.
Problem: when the user navigates through the pages (aka clicking 'previous' or 'next' buttons), the $_POST data is lost. However, these values are needed in order to get the results for another page.
This particular problem made me think about the search forms on forums. I submitted a query on one of them and noticed a search ID getting appended to the URL:
example.com/forum/search.php?searchid=5672532
This means that the form data is being kept somewhere, somehow... and I'm thinking what the best option might be, from most to least plausible:
- Database table where each search input gets saved for e.g. 30 minutes
- Sessions
- Files
(hidden fields are obviously not an option, because they need a form submit button to be pressed in order to get sent with the request)
I'd like to here your opinions on this, some of you must have encountered this problem already...

Saving the search parameters in JSON or URL encoded in a database for a set amount of time (have an expiry column) seems the best solution.
Note that in that way, you'll need to query the database over and over, with different LIMIT clauses.

Related

How to link/navigate through SQL search results in PHP one page at a time

I am building an admin type script to view records within a mySQL database. This links in numerous tables and is all working perfect. I have now added a filter form to allow me to search/filter for records that match specific criteria. Again this works perfect.
The problem I cannot get my head around is this...
For each record I have a link that allows me to go into that record and view more information/perform tasks on the record.
Initially it was simple to have a 'Previous' and 'Next' link for cycling through the records which meant I didnt need to go Back to the search results to go to the previous/next record. It simply +1 or -1 from the current record ID to generate the link.
Now however I am filtering so the records ID's might be 1,3,4,5,8,10,15,30 etc
The problem is now that once I click into any one of the results to view that record I essentially lose the filtering and all of the results.
The only way I can think of achieving this is to pass the filter variables in with the link and redo the filtering every time the View page is loaded, working out where the current record is in the result set and creating a link based on the previous and next records in the result set.
Am I doing this correct or is there a better way?
I have searched but most answers are geared towards the basics of looping through mySQL query results which sounds the same but is very different.
Any advice would be greatlt appreciated!

Database Concurrency Problems

I'm currently developing a database/website server interface to facilitate inputting data for a data collection project. There are two types of additions being made to the database: A and B here. Some of the tables in the database that handle these entries are as follows:
dcs_projectname_a
dcs_projectname_b
Each of these have tables for all the required input fields in addition to things like creator, timestamp, etc.
The pages on the website facilitate three different options: add, view, and edit. Each page for each type of entry performs the respective function. That is, the add page adds, view page views, etc.
I am just about done; however, there is a major challenge I haven't really confronted yet: concurrency. There will be multiple users adding content to the database at the same time. Each entry is given its own specific id and there CANNOT be any duplicate id's. That is, the first a entry is A000001, the next is A000002, and so on.
On the add and edit pages, there is a disabled field for the user to view the id for other uses when physically documenting entries.
What I need to figure out is how to implement concurrency management so that when users are concurrently adding a's that they will not be under the same id and row.
The add page automatically generates the id to be used by accessing the database's most recent id and adding one.
My thought was to create a new row in the table every time the add page is opened and give it the calculated id. Then, when information is added it performs a modification to that existing row. This way, if another user opens the add page while another entry is currently being added it will be given the future id, not the same one.
With this method I need a way to delete this entry if the user leaves the add page or closes the browser. Then, I also need other users with open add pages to automatically update their id's to one less when the first user (or any other user less than the most id being used) leaves their add page and cancels the addition.
This is kind of a complicated explanation and if you don't understand let me know and I'll try to answer as best as I can. Any help is much appreciated!
Thanks
There's a number of solutions to your problem, but you seem to have made things harder by having your application generate the record IDs for you.
Instead, you could just be using MySQL's AUTO_INCREMENT functionality to automatically generate/increment the record ID for you (upon insert). MySQL will ensure that there are no duplicates, and you can get rid of the extra database call to retrieve the most recent ID.

Web App: Gathering unknown amount of data from user an unknown number of times

My Problem I am writing an app for internal use at my pharmacy. I am gathering some usage data from multiple pharmacy systems to help manage inventory. 95% of the records I can automatically parse how many tablets are used every day, but there are some that require user input. This input consists of 3 fields (date, time, number of tabs) for an unknown number of records. For example, twice a day would have 6 records for 3 days, whereas 3 times a day would have 9.
My Current Solution The user selects the records to process and starts the procedure. Any records that can be automatically processed are taken care of. Any records requiring user input are stuck in a table. Upon refresh of the page, an Ajax request checks the table for any records. If a record is present, a modal is shown to the user to allow their input. The input form has a "new record" button that shows another round of the 3 fields, then the data is POSTed to a php script. The script processes the data and removes the affected record. If multiple records, the ajax script pops another modal. This continues until the table is empty.
Help There has to be a more elegant solution out there. I've look at several CRUD grids, but most seem to be about showing and editing data, not about general data entry.
Sorry for the wall of text.
Do you mean something like this
http://www.labs.mimmin.com/inlineedit/
http://dev.iceburg.net/jquery/tableEditor/example_new.php
I found it on google by using 'jquery inline editable table'
I know Yii has a nice admin interface specifically fast input, but I sense you wrote the code yourself, transferring to Yii might be a bigger challenge that solving this in a nice way.
good luck

identifying dynamic pages (created by PHP from database)

In case this question has been asked before just refer me to the answer and I shall be greatful.. it's a very common problem however I have not been able to find solution form google
I am developing a website in PHP using codeigniter. I have articles /Posts. These posts are located in a database and when ever a user demands to see an Article, all its relevant data is pulled from database using a unique "Content_id" and a page is presented to user dynamically.
However once the data is dispatched to the view and an html page is created from that page I have no way of finding which "Content_id" was used in generating that page. Now in case user comments on that page I dont know which "content_id" to save in the comments table along with this comment so that next time when this article is displayed then all the relevant comments can also be displayed.
some of the solutions that I thought are as follows
Can I use session to store this data? (However I am not using sessions for those users who are not logged in ... So can I use sessions for all users )
Will using a form on the page with hidden fields be a viable option...
How do Wordpress or other CMS are handle this problem?
To achieve your goal, your table needs four columns:
Unique row number
Content
Post number
Post date (timestamp)
The unique row number is the primary key. Auto_increment it.
The content is the posted message.
The post number is the row number of the post that starts the conversation. Make it the same as the row number in the conversation's first post. It is the row number of the post being responded to. All responses to the conversation will have the same post number.
The post date is a timestamp. It permits you to organize the order of the posts in a particularized conversation.
Each conversation, or sub_conversation, follows this routine.
Usually, the post id is passed as a GET variable anyway. If not, a hidden input is fine.
I am Very sory to Know that no one answerd my question except one persone
Any ways here is the solution that I have figured out in 1 sentence **
The pages are not required to be numbered But the content needs to be numbered... thus each content (Article, post etc) has a content id and that id needs to be considered.
**

post/redirect/get and saving database query in SESSION

I'm new to post/redirect/get. I'm coding up the first real site that led me to discover the need for PRG.
So I had written and gotten working code that did the following:
1) user enters a search string
2) we search the database and find their desired search results
3) if we found their search results successfully, we alter the database --
a 'frequency of lookups' -- to indicate the user searched and found what
he was looking for
4) then display the results he searched on
What I found was refreshing the page in the browser resulted in the user seeing his same search results again but we incremented the 'frequency of lookup' -- the user is limited in the frequency of lookup in the database -- so if the user refreshed the page too many times, they ran through their hourly ration of database lookups. That's when I discovered Post/Redirect/Get a few days ago.
I am now going through the site and altering all pages that alter the database and display results and switching them over to PRG. Here is the new process:
1) user enters a search string
2) we search the database and find their desired search results
3) if we found their search results successfully, we alter the database --
a 'frequency of lookups' -- to indicate the user searched and found
what he was looking for
4) PRG to a 'results' page
5) then display the results he searched on
And I ran into a problem. I need to somehow 'pass' their search results from step (2) above
to the new 'results' page in step (5) that I created to implement PRG.
So my question is -- is there a 'most common' way to do this? In reading around I've seen 'save the results in a .CSV file' and also 'save the database search results in the SESSION.'
I want to save the search results in the SESSION then on my 'GET' page I added for PRG to display the result, I'll read the search results from the session variable and display them.
By that I mean I want to do this:
$result = mysql_query($query, $theDbServer);
$_SESSION['theSearchResults'] = $result.
Then on the 'display' page, read back the search results from $_SESSION['theSearchResults']
and display them using:
$result = $_SESSION['theSearchResults'];
$row = mysql_fetch_row($result);
Is this a normal approach? And I suspect I cannot save the raw $result in a session variable like the above -- but I'm also not sure how to put the $result above into the $_SESSION -- how is that normally done?
Assuming you don't want to have the final landing page do the querying (without decrementing their quota of course), then you're going to need to use session.
Saving to a CSV is not standard and wouldn't scale very well. I would parse the results of the query into a more user-friendly form (a simple class or list or whatever you need). I'd then store the class and not the reader into the session. You'll probably want to clear out that portion of the session when they leave the results page (especially if it's a huge amount of data).
This is assuming you're using raw php. There are many frameworks with features for this exact case (you want to shuttle a piece of data from one page to the next).

Categories