2 times same query, once with limits

This is a discussion on 2 times same query, once with limits within the PHP Language forums, part of the PHP Programming Forums category; I've a query quite important to execute. I've to fill an array with all result, so the query ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-21-2005
Bob Bedford
 
Posts: n/a
Default 2 times same query, once with limits

I've a query quite important to execute.

I've to fill an array with all result, so the query is called once wich
returns all ID's

Then I call the same query a second time as I shown only 10 results in my
page.

How can I avoid to call the query twice ?

Bob


Reply With Quote
  #2 (permalink)  
Old 01-21-2005
rw
 
Posts: n/a
Default Re: 2 times same query, once with limits

yust save result in session

Bob Bedford wrote:
> I've a query quite important to execute.
>
> I've to fill an array with all result, so the query is called once wich
> returns all ID's
>
> Then I call the same query a second time as I shown only 10 results in
> my page.
>
> How can I avoid to call the query twice ?
>
> Bob
>


--
Robert Was
Reply With Quote
  #3 (permalink)  
Old 01-21-2005
Bob Bedford
 
Posts: n/a
Default Re: 2 times same query, once with limits


"rw" <rwas8@gazeta.pl> a écrit dans le message de news:
csqmcs$4hk$1@diplo.bci.pl...
> yust save result in session


don't really understand what you mean!
should I
$_SESSION['queryresult'] = mysql_result('select...',$mydb); ???

and how to do next and previous then ? I will only know the next and
previous recordid in order to put a next and previous button with param
ID=previousID or ID=nextID

Bob


Reply With Quote
  #4 (permalink)  
Old 01-21-2005
Erwin Moller
 
Posts: n/a
Default Re: 2 times same query, once with limits

Bob Bedford wrote:

> I've a query quite important to execute.
>
> I've to fill an array with all result, so the query is called once wich
> returns all ID's
>
> Then I call the same query a second time as I shown only 10 results in my
> page.
>
> How can I avoid to call the query twice ?
>
> Bob


Hi Bob,

you didn't mention if you want to use both results on the same page.
If so:
- Loop over ALL the results once.
- Then loop over the first 10

If you want the results to be available on another page, store them in a
session.

Regards,
Erwin Moller


Reply With Quote
  #5 (permalink)  
Old 01-21-2005
Bob Bedford
 
Posts: n/a
Default Re: 2 times same query, once with limits


"Erwin Moller"
<since_humans_read_this_I_am_spammed_too_much@spam yourself.com> a écrit dans
le message de news: 41f131a9$0$6211$e4fe514c@news.xs4all.nl...
> Bob Bedford wrote:
>
>> I've a query quite important to execute.
>>
>> I've to fill an array with all result, so the query is called once wich
>> returns all ID's
>>
>> Then I call the same query a second time as I shown only 10 results in my
>> page.
>>
>> How can I avoid to call the query twice ?
>>
>> Bob

>
> Hi Bob,
>
> you didn't mention if you want to use both results on the same page.
> If so:
> - Loop over ALL the results once.
> - Then loop over the first 10
>
> If you want the results to be available on another page, store them in a
> session.

I want 10 results (not necessarely the first 10) in the actual page. In the
other page, I need only 1 result BUT I've to know previous and next record.

Takes this example. I'm in the search page. The result gives 150 results. I
only show 10 results/page.
Now, I click on an element (one of the result). From there I'd like to click
on "next item" or "previous item".
For this I've to know each element of the query's result. But how to do
previous and next ??? now I've executed the query, then I put each element
ID in an array. When I go in element page, I have the array, so clicking on
previous I've the previous ID, clicking on next, I've the next ID. Then I
show the element with the new ID.
The problem is that If I click 12 times on "next", I've to know that
clicking on "return" button (not the previous of the browser), the page has
changed. I should be in page 2 now.
How to fill the array then show only 10 results ? What should I do ?

Bob


Reply With Quote
  #6 (permalink)  
Old 01-22-2005
Virgil Green
 
Posts: n/a
Default Re: 2 times same query, once with limits

Bob Bedford wrote:
> "Erwin Moller"
> <since_humans_read_this_I_am_spammed_too_much@spam yourself.com> a
> écrit dans le message de news:
> 41f131a9$0$6211$e4fe514c@news.xs4all.nl...
>> Bob Bedford wrote:
>>
>>> I've a query quite important to execute.
>>>
>>> I've to fill an array with all result, so the query is called once
>>> wich returns all ID's
>>>
>>> Then I call the same query a second time as I shown only 10 results
>>> in my page.
>>>
>>> How can I avoid to call the query twice ?
>>>
>>> Bob

>>
>> Hi Bob,
>>
>> you didn't mention if you want to use both results on the same page.
>> If so:
>> - Loop over ALL the results once.
>> - Then loop over the first 10
>>
>> If you want the results to be available on another page, store them
>> in a session.

> I want 10 results (not necessarely the first 10) in the actual page.
> In the other page, I need only 1 result BUT I've to know previous and
> next record.
>
> Takes this example. I'm in the search page. The result gives 150
> results. I only show 10 results/page.
> Now, I click on an element (one of the result). From there I'd like
> to click on "next item" or "previous item".
> For this I've to know each element of the query's result. But how to
> do previous and next ??? now I've executed the query, then I put each
> element ID in an array. When I go in element page, I have the array,
> so clicking on previous I've the previous ID, clicking on next, I've
> the next ID. Then I show the element with the new ID.
> The problem is that If I click 12 times on "next", I've to know that
> clicking on "return" button (not the previous of the browser), the
> page has changed. I should be in page 2 now.
> How to fill the array then show only 10 results ? What should I do ?
>
> Bob


Plenty of examples available from Google.

See this, for example:
http://phpclasses.mirrors.nyphp.org/...kage/2012.html

--
Virgil


Reply With Quote
  #7 (permalink)  
Old 01-22-2005
Chung Leong
 
Posts: n/a
Default Re: 2 times same query, once with limits


"Bob Bedford" <bedford1@YouKnowWhatToDoHerehotmail.com> wrote in message
news:41f14262$0$3409$5402220f@news.sunrise.ch...
> I want 10 results (not necessarely the first 10) in the actual page. In

the
> other page, I need only 1 result BUT I've to know previous and next

record.
>
> Takes this example. I'm in the search page. The result gives 150 results.

I
> only show 10 results/page.
> Now, I click on an element (one of the result). From there I'd like to

click
> on "next item" or "previous item".
> For this I've to know each element of the query's result. But how to do
> previous and next ??? now I've executed the query, then I put each element
> ID in an array. When I go in element page, I have the array, so clicking

on
> previous I've the previous ID, clicking on next, I've the next ID. Then I
> show the element with the new ID.
> The problem is that If I click 12 times on "next", I've to know that
> clicking on "return" button (not the previous of the browser), the page

has
> changed. I should be in page 2 now.
> How to fill the array then show only 10 results ? What should I do ?


Hmmm. Tricky. In order to determine what the previous and next item are, you
will need to perform the original query. There's really no way around that.
Storing data in session variable is always rather dicey in these kinds of
scenarios.

What I would do is in the URL to the element page, put in the variables that
we'd need to reconstruct the search query, the id of the element, and the
position of that element within the result set. In the element page itself,
perform the same query, but use a window of 3--one ahead of the position of
the element and one behind. To make sure that the position of the element
within the result set hasn't since changed, compare the id of the middle row
to the one from the URL. If they don't match, retrieve the entire data set
to look for the correct 3 rows. For the "prev" and "next" links, go the same
thing.

The drawback is that running the query is slower than getting the data of
the element using a primary key. On the other hand, because you're running
the same query over and over, the database server should be smart enough to
cache that data set.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:30 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0