Jump to content

PHP pagination problem!


ktsirig

Recommended Posts

Hi guys,

 

well I'm trying to formulate pagination view of my results from a Mysql query in my database. The thing is that the query is not known beforehand, since the user of the database can enter several search terms in text fields of the webpage (text_search.php) and when he hits the "Submit" button, the new page (retrieve_text.php) looks about which text fields have been filled and appends the values in the mysql query like this:

 

$text_query = ...... WHERE [user-defined conditions]

 

Everything is OK until now. But I wanted to show the results more clear to the user, since it can have e.g 600 results so it's not so good to view them in a loooooooooong page. So I tried to paginate them.

 

Now comes the problem: the first page looks Ok, and brings only 25 results, which I want to be the limit of results to show per page. BUT, when I click on the "Next button", I see nothing. The reason? In pages 2,3...N, the $text_query is empty since there are no values submitted through the $_POST variable..., So, in the 1st page, if I print the SQL query I have something like that:

 

SELECT DISTINCT protein.protein_id, protein.protein_name FROM protein LEFT JOIN protein_reference ON protein_reference.prot_ref_protein_id = protein.protein_id LEFT JOIN reference ON reference.reference_id = protein_reference.prot_ref_reference_id LEFT JOIN families ON protein.protein_families_id = families.families_id WHERE protein.protein_name LIKE '%protein%' ORDER BY protein.protein_id LIMIT 0, 25

 

while in the 2nd page for example I get:

 

SELECT DISTINCT protein.protein_id, protein.protein_name, protein.protein_seq_len, protein.protein_organism, protein_ncbi FROM protein LEFT JOIN protein_reference ON protein_reference.prot_ref_protein_id = protein.protein_id LEFT JOIN reference ON reference.reference_id = protein_reference.prot_ref_reference_id LEFT JOIN families ON protein.protein_families_id = families.families_id WHERE ORDER BY protein.protein_id LIMIT 25, 25

 

The reason is, as I wrote before, that the where clause in the sql statement is written on-the-fly so the sql search is dynamic and not static. If it was static, like

 

SELECT * from protein WHERE name LIKE '%protein%';

 

things would be straight-forward.

 

What can I do? Should/Could I somehow "send" the sql query from page to page? I suppose this is BAD practice, but I don't really know how to proceed...

 

Link to comment
Share on other sites

The user above me is correct, this can be done by either storing the dynamic parts of the query in a session, or by adding them to the query-string.

So you would have something that looks like:

 

http://site.com/some_folder/file.php?like=protein&start=25&limit=25

 

the above would be the link to the second page, which you can use to re-build the query.

 

Note: the best way would be to use a prepared statement, but that is a different story.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.