Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

increment mysql_query using limit

0 views
Skip to first unread message

chris

unread,
Jul 17, 2002, 12:23:00 AM7/17/02
to
hi
how can i increment my query?

im using 2 variables $start and $amount which are used to limit my search to
say ..10 rows.

but then i check to see if there are more rows in the database ...if there
is i have a button appear to offer the user to search through the next ten
rows...

this works fine if im prepared to have the results displayed on differant
pages but of course i could end up having to have hundreds of pages.

how do i submit the query so it apears on the same page?
heres a clip of my code.

thanks chris

mysql_select_db("surfing",$connect);
$result =mysql_query ("select * from sale_items order by date desc limit
$start, $amount ",$connect);
// user ,items ,email,make ,model,year ,condition ,price ,description
,date
// from sale_items",$connect);

$the_result =mysql_query ("select * from sale_items order by date desc
",$connect);

if($amount != mysql_num_rows($the_result)) {
$start = $start+3;
$amount = $amount+3;
echo"<a
href='test4sale.php?start=$start&amount=$amount&type=next'>Next</a>";


Jeff Walter

unread,
Jul 17, 2002, 12:49:58 AM7/17/02
to
Chris,
First off, I'm not sure I understand your post correctly. But I will
respond under the impression that I did. I mean, I can't tell you what your
problem is, I just don't see it. But I'm guessing it has something to do
with checking for more rows because if you where searching for something
you'd have a "WHERE..." in your SQL query, but I don't see one. So on with
the show...

Why not always increase $amount by 1, then check to see if
mysql_num_rows returns $amount + 1? If it does, simply ignore that last row
when returning data (return $amount rows), and pass $start + $amount + 1 on
as $start. So your code might look something like this:

mysql_select_db ("surfing",$connect);
$tamount = $amount + 1;
$result = mysql_query ("select * from sale_items order by date desc limit
$start, $tamount ", $connect);

if ($tamount == mysql_num_rows ($result)) {
$start = $start + $amount + 1;
echo ("<a href='test4sale.php?start=$start&amount=$amount&type=next'>Next
$amount Results</a>");
}

Cheers!

Jeff Walter
je...@jeffs-place.org

PS - I didn't try this code, but it *should* work


chris

unread,
Jul 17, 2002, 4:36:18 AM7/17/02
to
im sorry im not expalining too clearly ....i sometimes wonder how i got this
far!.

anyway ..ihave no problem finding out if there are more rows the problem is
that if there is, i want the next query to increment the limit set by the
first query by 20 each time so that the next 20 rows are shown displayed on
the same page when the next btn is clicked.....as in the google style search
results.

im sorry i know this probably dosnt look clear at all....

anyway thanks chris

"Jeff Walter" <je...@jeffs-place.org> wrote in message
news:WH6Z8.33040$Wt3.29414@rwcrnsc53...

Jeff Walter

unread,
Jul 17, 2002, 6:02:00 AM7/17/02
to
Chris wrote:
> anyway ..ihave no problem finding out if there are more rows the problem
is
> that if there is, i want the next query to increment the limit set by the
> first query by 20 each time so that the next 20 rows are shown displayed
on
> the same page when the next btn is clicked.....as in the google style
search
> results.

Okay...Now I know what you want... (even though on a recent visit to Google
I didn't get this functionality) ...

My idea is this...Add one more variable to your URL, we'll call it
$howmanypages for simplicity's sake. What you'll end up doing is
"repeating" your query. Code has been provided (I modified the original
code I posted).

mysql_select_db ("surfing",$connect);
for ($s = 1; $s <= $howmanypages; $s++) {
$tstart = $start + $amount * $s;


$result = mysql_query ("select * from sale_items order by date desc

limit $tstart, $amount ", $connect);

// Display your query results here //
// Add a "page" seperator if you want //

if ( $s == $howmanypages ) { // This is the last page being displayed
//


$tamount = $amount + 1;
$result = mysql_query ("select * from sale_items order by date desc

limit $tstart, $tamount ", $connect);
if ($tamount == mysql_num_rows ($result)) { // More results after
this last page //
$howmanypages = $howmanypages + 1;
echo ("<a
href='test4sale.php?start=$start&amount=$amount&type=next&howmanypages=$howm
anypages'>Next $amount Results</a>");
}
}
}

Hopefully this helps. Let me know if the code works for you.

Jeff Walter
je...@jeffs-place.org


chris

unread,
Jul 17, 2002, 11:55:39 AM7/17/02
to
thanks jeff

itll take me a while to digest that ...ill try to put it in action sometime
this week

cheers chris


"Jeff Walter" <je...@jeffs-place.org> wrote in message

news:sgbZ8.11380$_51....@rwcrnsc52.ops.asp.att.net...

hairyone

unread,
Jul 17, 2002, 3:03:11 PM7/17/02
to
----- Original Message -----
From: "chris" <chrise...@hotmail.com>
Newsgroups: alt.php
Sent: Wednesday, July 17, 2002 4:55 PM
Subject: Re: increment mysql_query using limit


> thanks jeff
>
> itll take me a while to digest that ...ill try to put it in action
sometime
> this week
>
> cheers chris


I have a site on Lycos I am using to practice using PHP and have
written a script which does almost what you want. It works for me. You
might want to have a look at
http://members.lycos.co.uk/billemmott/scripts.php?page_no=1 and see
the Full Article for "Fixed Number Of Items Per Page ".

Hope it might help
--
Bill Emmott

If at first you dont succeed - read the instructions


0 new messages