PHP/MySQL 分页还是 jQuery?
只是一个简单的问题。如果我在我的网站上使用分页并且期望得到很多结果,那么使用 jQuery 还是只加载一个新页面的基本 PHP/MySQL 更好?
如果我使用 jQuery 并且数据库中有超过 300 个结果,是否必须在初始页面加载时一次性加载所有结果?这可能需要很长时间才能加载。或者你可以让它只加载前 10 个,然后当你转到第 2 页时它会加载接下来的 10 个吗?
只是想知道您对我的情况是否有任何建议,以及您是否推荐我可以使用的任何好的脚本。
谢谢!
Just a quick question. If I use a Pagination for my website and am expecting a lot of results, is it better to use jQuery or just a basic PHP/MySQL one that just loads a new page?
If I use jQuery and I have over 300 results from the database, will it have to load it all at once on the initial page load? That might take a long time to load. Or can you make it load only the first 10, and then when you go to Page 2 it will load the next 10?
Just wondering if you have any suggestions for my situation, and if you recommend any good scripts I can use for it.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IMO,从“基本”PHP/MySQL 分页开始(每次用户更改页面时加载新页面)。
一旦你完成了这个工作,如果你想要它,然后在顶部添加 jQuery 分页。 jQuery 分页要做的就是通过 AJAX 加载新的结果页面,而不是加载整个新页面。
IMO, start with "basic" PHP/MySQL pagination (load a new page each time the user changes pages).
Once you've got that working, if you want it, then add in jQuery pagination on top. All the jQuery pagination would do is load the new page of results via AJAX, rather than loading an entire new page.
因此,这里的关键是如何通过 javascript (jQuery) 处理分页结果。如果您在页面上呈现所有 300 个结果,并简单地隐藏 200-300 个结果(并通过 javascript 显示它们),您的页面最初呈现的速度仍然会很慢,并且您将使用可以优化的查询对数据库进行征税通过限制(分页)。
另一方面,如果您通过向 Web 服务发出异步 GET 请求(通过 JSON 吐出数据)来异步查询更多结果,那么您既可以拥有响应式页面,又可以避免繁重的、无限的查询。
使用 PHP / MySQL 和回发来处理该问题还可以防止长时间的初始页面加载 + 繁重的查询。
所以,总而言之,我绝对会对你的结果进行分页。我还建议您执行以下操作:
1)首先使用纯 PHP / MySQL 进行架构设计。例如:
/results/?start=0&limit=20(显示结果 0-19)
/results/?start=20&limit=40 (显示结果 20-40)
2) 然后,如果您想提供响应式 JavaScript 机制来加载更多内容,请扩展您的页面,以便它可以使用格式参数输出 JSON:
/results/?start=0&limit=20&format=JSON
因此,如果 format=JSON 而不是渲染 HTML,它只会吐出 JSON 数据,并进行分页。
3) 然后连接 javascript 以使用 JSON 数据动态加载更多内容:
希望这是有道理的!
So, the key here is how you handle paginating results via javascript (jQuery). If you render all 300 results on the page and simply hide results 200-300 (and reveal them via javascript), your page will still be really slow to render initially, and you'll be taxing the database with a query that could be optimized via a limit (pagination).
On the other hand, if you asynchronously query for more results via say, an asynchronous GET request to a web-service that spits the data out via JSON, you can both have a responsive page and avoid a taxing, limitless query.
Using PHP / MySQL and post-backs to handle the issue also prevents the long-initial page load + taxing query.
So, in summary, I'd absolutely paginate your results. I would also suggest you do the following:
1) First architect things using purely PHP / MySQL. So, for instance:
/results/?start=0&limit=20 (Show results 0-19)
/results/?start=20&limit=40 (Show results 20-40)
2) Then if you want to provide a responsive, javascript mechanism for loading more, extend your page so that it can spit out JSON with a format parameter:
/results/?start=0&limit=20&format=JSON
So if format=JSON instead of rendering the HTML, it'll just spit out the JSON data, paginated.
3) Then wire up the javascript to use the JSON data to dynamically load in more content:
Hopefully that makes sense!
您已经用 ajax 标记了您的问题,这就是答案...您应该使用 PHP/MySQL + Ajax 的组合来使事情更快、更流畅。
这是一个非常流行的插件,它实现了客户端界面: JQGrid
You've tagged your question with ajax, that's the answer... You should use a combo of PHP/MySQL + Ajax to make the things faster and smoother.
Here is a very popular plugin which implement the client interface: JQGrid