需要从2个表中获取数据并进行分页

时间:2019-07-05 07:15:00

标签: php sql

所以我有2个表,例如第一个是书店,其中我有数百个具有唯一ID码的书的列表,在第二个表中,我就像拥有这些书的人一样,这也是唯一的ID码。因此,我想进行分页并根据页面显示在屏幕上,第一页的前10个结果为第一页,第二页的前十个结果为10至20,依此类推。但是对于这种分页,我只知道如何从1个表中获取数据,但是在那里我需要从2表中获取数据,这是书的ID码,并通过1表显示书。而且我不知道如何正确地做到这一点,以及如何使通用函数或类。谢谢。

我试图通过第二张表中每个id book的while语句获取所有数据,然后将其放入一个数组中,然后显示它,但是我认为它并不是那么优化。对于while循环中的每个书本ID,它都会向数据库发出请求并显示结果,所有行都存储在$ result_request数组中。

$branch_arr[]; //in this array i have all the id of books from 2nd table
$count_e=1; //just an auxilliar variable to access data from above array
$entities_count= count($branch_arr);//this is the length of the $branch_arr[] with id's;

while ($count_e < $entities_count) { 

     $req_db = new myDB($req_b, $branch_arr[$count_e]);
     $result_b = $req_db->fetchALL();
     $total_rows = $req_db->rowCount + $total_rows;

        foreach ($result_b as $value) {
         $result_request[] =  array($value['Name'],$value['Surname'],$value['Order_id']); //putting data from data base request in array

        }
     $count_e = $count_e + 1;
 }

0 个答案:

没有答案