Viewing 12 posts - 1 through 12 (of 12 total)
  • PHP types – help needed ……. again :(
  • sharkbait
    Free Member

    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?

    dazh
    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?

    sharkbait
    Free Member

    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.

    pdw
    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.

    lemonysam
    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.

    allthepies
    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

    sharkbait
    Free Member

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

    sharkbait
    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.

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

    Search page is here. 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.

    robware
    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.

    dazh
    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.

    http://phpframeworks.com/

    sharkbait
    Free Member

    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.

    pat12
    Free Member

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

Viewing 12 posts - 1 through 12 (of 12 total)

The topic ‘PHP types – help needed ……. again :(’ is closed to new replies.