Re: Newbie question, Which way is best?

From: Date: Thu, 20 Mar 2008 20:25:07 +0000
Subject: Re: Newbie question, Which way is best?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-271919@lists.php.net to get a copy of this message
On Mar 20, 2008, at 12:05 PM, George Jamieson wrote:
Hi Philip, Hope you don't mind me sending this to you direct. Thanks for the answer but... I'm sorry I don't follow you. My form sets up the query parameters. It works. My pagination code passes the page no. It works. What it doesn't do is provide the next execution of my script with the query. I pass the page no. but how do I either use the same query but with new LIMIT parameters or reconstruct the entire query with the new LIMIT. My form gives the user options to search by manufacturer, gategory or search string. sorted by description, finish or price. I've just added a drop down box for number of items to be displayed at a time. I want to use my pagination script to scroll, page by page, through the resultset. So if I call my script again, with the new page number, I have no way of reusing the same query as the user is not required to rePOST the form with its parameters. I can't see how your code allows me to do that.
Because I increment the page count ($page) each time... So, each time you hit Go, then it finds the next page. Of course, this is not really made for production - you would want to find a more user-friendly way to accomplish showing a result set. You could change it up to use _GET instead: <a href="thispage.php?page=3">Go to Page 3</a> Then modify your PHP code to accept _GET values along with/instead of _POST values: <?php if (isset ($_POST['submitted']) || !empty ($_GET['page'])) {
    $page = $_POST['page'] ? (int) $_POST['page'] : (int) $_GET['page'];
    ...
} ?> I feel like we've explained this fairly well, but you may not completely understand. Let us know if we need to break it down a little bit more. We would be happy to point you to some materials that can assist you. ~Philip
Regards George
to the called script?
George
I don't know if anyone has answered the question you have asked at least twice... "How do I pass the query to the next page?" Here's how I would approach it. Don't pass the query - all you need is the page number. This code hasn't been tested, but I think you'll get the idea. <?php // thispage.php if (isset ($_POST['submitted'])) {
    $resultsPerPage = 50; // or whatever value
    $page = mysql_real_escape_string ($_POST['page']);
    $start = ($page * $resultsPerPage) - $resultsPerPage;
    $length = $start + $resultsPerPage;
    // Notice how you don't send the query in the POST or GET, just
the page number
    $sql = "SELECT field FROM table WHERE (field_a =
'someValue') LIMIT $start, $length";
    $results = mysql_query ($sql);
} // Go to next page $page = $_POST['page'] ? (int) $_POST['page'] + 1 : 1; ?> ... <form method="post" action="thispage.php">
    <input type="submit" value="Go" />
    <input type="hidden" name="page" value="<?php echo htmlentities
($page); ?>" />
    <input type="hidden" name="submitted" value="1" />
</form> ... <?php while ($row = mysql_fetch_array ($results, MYSQL_ASSOC)) {
    // Display results
} ?> Hopefully that helps a little bit. ~Philip


« previous php.general (#271919) next »