简体   繁体   English

使用php或mysql向来自不同数据库的数据添加排序和分页

[英]Adding sorting and pagination to data from different DB using php or mysql

My task is to take data from different databases in different server and to display them in a single web page.I am working in joomla and i am able to take data from the databases using separate queries for each database.But the problem is that i need to give sorting and pagination to this web page. 我的任务是从不同服务器中的不同数据库中获取数据并将它们显示在单个网页中。我在joomla中工作,我能够通过对每个数据库使用单独的查询从数据库中获取数据。但是问题是我需要对该网页进行排序和分页。 how can i do this without making any performance issue since all the databases contains a lot of data. 由于所有数据库都包含大量数据,我该怎么做而又不引起任何性能问题。 Or can i use a single sql query for this ?? 或者我可以为此使用单个sql查询吗?

To do what you're describing in Joomla I would recommend the Fabrik extension. 要执行您在Joomla中描述的操作,我会推荐Fabrik扩展。 It lets you create fairly complex front-end visualizations and table views of database data, as well as search forms and single-page table views that have nice ordering/sorting/per-column filtering controls. 它使您可以创建相当复杂的数据库数据的前端可视化效果和表视图,以及具有良好的排序/排序/每列过滤控件的搜索表单和单页表视图。

More importantly, Fabrik lets you set up connections to multiple databases. 更重要的是,Fabrik使您可以建立与多个数据库的连接。 Once you've set up all your database connections in Fabrik, you can then import and configure those tables in the Fabrik back-end, and after that they are all treated the same by the other parts of the administrative interface. 在Fabrik中设置所有数据库连接后,即可在Fabrik后端中导入和配置这些表,然后,管理界面的其他部分将它们视为相同。

In other words, you can create front-end table views or search forms in the Fabrik back-end, which can do things like table joins across multiple database connections, or pre-filter result sets of a table from one database using data stored in another database (among other things) and Fabrik will take care of creating and closing the necessary database connections for you. 换句话说,您可以在Fabrik后端中创建前端表视图或搜索表单,这可以执行跨多个数据库连接的表联接或使用存储在其中的数据从一个数据库中预过滤表的结果集之类的操作。其他数据库(其中包括),Fabrik将为您创建和关闭必要的数据库连接。

You can use a GET parameter to use pass the page that you want to view, and the offset will be calculated from that. 您可以使用GET参数来传递您要查看的页面,然后将从中计算偏移量。

$page = (isset($_GET['page']))? (int)$_GET['page']: 1;

$per_page = 25;

$offset = ($page-1) * $per_page; // you will have to improve this

$sql = "SELECT * FROM table 
       WHERE (condition)
       LIMIT {$offset},{$per_page}";

$result = mysql_query($sql) or die('Error : '.mysql_error());

You will have to integrate it with your code, to make it work better of course. 您必须将其与代码集成,以使其更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM