PHP types - help ne...
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] PHP types - help needed ....... again 🙁

11 Posts
7 Users
0 Reactions
35 Views
Posts: 13916
Free Member
Topic starter
 

OK so I'm back on paginating results from a mysql search.

I'm guessing they're all the same but this method uses your sql 'select' query to get the total number of rows then the same query again but adding LIMIT to do the paginating (so results page one is "myquery 0,10", results second page would be "myquery 10,10" and so on.

So far (using a tutorial) I've got the first page of results showing correctly BUT when I go to the second page of results the query has 'lost' the variables that was passed from the search form using $searchterm=$_POST['searchterm'].

I'm at a loss - any pointers?


 
Posted : 26/10/2015 3:13 pm
 dazh
Posts: 13182
Full Member
 

Your client must be very patient 🙂

I'm afraid from that description there's not a lot to go on, but have you checked that the limit data is being passed to the page via the post?


 
Posted : 26/10/2015 3:21 pm
Posts: 13916
Free Member
Topic starter
 

Your client must be very patient

Not put it out there yet!

Limit data def right (I'm echoing it along with the results). If I hard code $searchterm it works fine but goes wrong as soon as I change it back to the $_POST data.

Happy to supply more code or links to the pages.


 
Posted : 26/10/2015 3:29 pm
 pdw
Posts: 2206
Free Member
 

You will need to include the searchterm when you link to the 2nd page of results. If it's a simple get request, you'll need to include it as a URL parameter e.g. myscript.php?searchterm=mysearchterms or if it's a post request you can use a hidden form input element.

If you're generating a URL, you'll need to ensure that the searchterm value is URL encoded, otherwise searches that include characters such as "&" or "=" will break it.


 
Posted : 26/10/2015 3:36 pm
Posts: 0
Free Member
 

Slightly OT: Is there a reason you're using POST and not GET? A search using POST would be really annoying to use.


 
Posted : 26/10/2015 3:39 pm
Posts: 0
Free Member
 

As above, as you're using the $_POST variable then you have to make the 'searchterm' field available as a hidden field in whichever form you're using to invoke the POST HTTP request.

Rather than write all this stuff yourself, have you considered using library code ? I've used PEAR paging stuff to provide paged access to a database source.

https://pear.php.net/search.php?q=page&in=packages&x=0&y=0


 
Posted : 26/10/2015 3:49 pm
Posts: 13916
Free Member
Topic starter
 

Thats' the thing PDW, the link to the second page is just "blahblah/searchresults2.php?pn=2"


 
Posted : 26/10/2015 3:51 pm
Posts: 13916
Free Member
Topic starter
 

As above, as you're using the $_POST variable then you have to make the 'searchterm' field available as a hidden field in whichever form you're using to invoke the POST HTTP request.

Well it works great for the initial search/first page of results just not for pages 2, 3, etc.

Search page [url= http://185.25.241.129/~targetme/wof/search2.php ]is here[/url]. If you do a search for 1364 it will return the results and also display $limit, $searchterm and $searchtype (which I've hard coded to stop it failing completely). You'll see $searchterm disappear when you go to page 2 of the results and the query return every [4200] row in the database.


 
Posted : 26/10/2015 3:57 pm
Posts: 0
Free Member
 

Yeah, you want to be putting your search term in your pagination link, too. It makes for an ugly URL, but gets the job done.

You want something like this

$pageLink="searchterm=".$searchterm."&pn=".$pagenumber;

Edit: you could also use a GET form request, as this will allow users to link to a set of results.


 
Posted : 26/10/2015 3:58 pm
 dazh
Posts: 13182
Full Member
 

Rather than write all this stuff yourself, have you considered using library code ?

+1 Doing this stuff yourself is fine for learning, but for an actual application you want to be looking at a framework or something similar. There's plenty out there that will do this sort of thing pretty much out the box.

[url= http://phpframeworks.com/ ]http://phpframeworks.com/[/url]


 
Posted : 26/10/2015 4:43 pm
Posts: 13916
Free Member
Topic starter
 

Sorted 🙂

(put $searchterm into the url and $_GET[] to pull it back out for use in the query on the next page)

Thanks for the pointers.


 
Posted : 27/10/2015 3:11 pm
Posts: 371
Free Member
 

if this is public facing, worth taking a read of this: http://php.net/manual/en/security.database.sql-injection.php


 
Posted : 27/10/2015 9:37 pm

6 DAYS LEFT
We are currently at 95% of our target!