Re: [PHP] Newbie question, Which way is best?



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
.



Relevant Pages

  • Re: OT:MySQL warnings
    ... returning- i.e. what string are you tring to query? ... using an online server so now my interest is piqued. ... read up a little bit on php first, so you can at least do a little ... debugging of the scripts themselves, ...
    (comp.sys.mac.advocacy)
  • Re: [PHP] PHP & MySQL Problem
    ... The funny thing is that all vars are being received except for the last two: ... [PHP] PHP & MySQL Problem ... The query then just goes like ... script" - I would guess the relevant part is not the query but rather ...
    (php.general)
  • Re: Confused by mysqli
    ... When I started using MySQL with Perl back in 1998, ... fetch_assocon a query I prepared with bind variables, ... So I had it print out its eval string that it was trying to run. ... PHP will fail to run at all, because bind_result will not have enough ...
    (comp.lang.php)
  • Re: [PHP] PHP & MySQL Problem
    ... [PHP] PHP & MySQL Problem ... > actually there seems to be no problem with your query (besides that you ...
    (php.general)
  • Re: timestamp
    ... PHP newsgroup, and should be discussed in comp.databases.mysql). ... Then click on the Survey link."; ... your SQL syntax; check the manual that corresponds to your MySQL ... Your query fails because a datetime value needs to be in single quotes in the query, ...
    (comp.lang.php)

Loading