开发者

Show parts of the result of an SQL statement using PHP

I have an SQL query which returns a set of data (around 40-50 tuples). I would like to display the results 5 at a time on an HTML page using PHP.

I already managed to have the right SELECT statement, but i am having problems to display the results 5 by 5 using a "more" button.

Can you please help? Note that every time i call the query, the data is being randomized, so it is not possible to set limits and call the query again. I have to find the method to开发者_高级运维 store the results somewhere, and then show them 5 by 5.


so it is not possible to set limits and call the query again.

Yes it is if you use the same seed.

For instance

SELECT column FROM table WHERE condition ORDER BY RANDOM(seed) LIMIT start, num

You generate seed once in your PHP and reuse it for the next calls (pass it with GET or save it into a session var)


The probably easiest way to do this is using sessions:

session_start()
[..] // Your query stuff..
$ids = array();
while(($row = mysql_fetch_assoc($result)) !== false)
{
   $ids[] = $row['id'];
   // probably output
}
$SESSION['ids'] = $ids;

The idea is to store the IDs of your query in the order they rows were returned. When a user clicks on "more" you just fetch the next 5 IDs from the array in the session and build your SQL-Query to fetch the rows.

Hope that helps.

Cheers,
Fabian


You can actually load all your data in your query and store them somewhere for examples in DIVs and for the first div, you show 5 rows and for the rest of the divs that contains 5 rows each you hide them initially on and when more link is clicked you show them again. In fact, you can get an idea from this script too written for the same purpose.

<div id="div_1">
 5 rows here, this is shown by default
</div>

<div id="div_2">
 5 rows here, this is hidden initially
</div>

<div id="div_3">
 5 rows here, this is hidden initially
</div>
and so on.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜