在两个不同的桌子上分页

时间:2013-11-19 00:51:21

标签: php mysql pagination

我有两张桌子,想知道是否可以将它们加入一个查询中。

脚本获得catID作为GET,它应该列出所有的subCats(如果有的话),然后列出mainCat cat = catID and subcat = 0中的照片

subCats表是:

id | parentCat | title | addedBy | date | active | defaultPhoto

photos表格为:id | title | description | fileName | fileType | uploadedBy | time | cat | subCat

我有一个分页脚本,需要计数并生成LIMIT才能使用。想知道我是否可以将这个结合到两个表的一个查询中,一些如何使我已经获得的分页更容易。因此,使用连接选择两者的计数,然后另一个选择结果。

有什么想法吗?真的坚持这个。

这是我糟糕的开始,但到目前为止似乎是一个糟糕的设计:

<?php
    if ($catInfoResult = $db->select("SELECT * FROM cats WHERE id = '%s' LIMIT 1", $catID))
    {
        if ($db->numRows > 0)
        {
            $pageTitle = h($catInfoResult[0]['title']) . ' - Photo Gallery';
            ?>
                <div class="headingTitle"><?=h($catInfoResult[0]['title'])?> Photos</div>
            <?php

            //get subCats
            $subCatCountResult = $db->count('subCats', '*', array('parentCat' => $catID));

            if (is_numeric($subCatCountResult))
            {
                $PerPage = 9; //how many locations you want to show per page

                $subCatLastPage = ceil($subCatCountResult/$PerPage);

                if ($subCatLastPage < 1)
                {
                    $subCatLastPage = 1;
                }

                $pageNum = (isset($_GET['pg'])) ? trim($_GET['pg']) : 1;    
                $pageNum = preg_replace('#[^0-9]#', '', $pageNum);

                //////////////////////////////////////////////////

                $subCatLimit = 'LIMIT ' . ($pageNum - 1) * $PerPage . ',' . $PerPage;

                //////////////////////////////////////////////////

                $itemList = array(
                    'subCats' => array(),
                    'photos' => array()
                );

                if ($subCatListResult = $db->select("SELECT * FROM subCats WHERE parentCat = %s ORDER BY title $subCatLimit", $catID))
                {

                    if ($db->numRows > 0)
                    {
                        foreach ($subCatListResult as $row)
                        {

                            $photoID = 0;

                            if ($row['defaultPhoto'] == 0) //look up photo for
                            {

                                if ($firstPhotoResult = $db->select("SELECT id, time FROM photos WHERE subCat = '%s' ORDER BY time ASC LIMIT 1", $row['id']))
                                {
                                    if ($db->numRows > 0)
                                    {
                                        $photoID = $firstPhotoResult[0]['id'];
                                    }
                                    else
                                    {
                                        $photoID = -1;
                                    }

                                }

                            }
                            else
                            {
                                $photoID = $row['defaultPhoto'];
                            }

                            //add to $itemList
                            $itemList['subCats'][$row['id']] = array(
                                'title' => $row['title']
                            );

                            $itemList['subCats'][$row['id']]['defaultPhoto'] = $photoID;

                        }

                    }

                }
                else
                {
                    throwDBError();
                }

                function addPhotoToItemList($row)
                {
                    global $itemList;

                    $itemList['photos'][$row['id']] = array(
                        'title' => $row['title'],
                        'fileName' => $row['fileName'],
                        'fileType' => $row['fileType']
                    );

                    //

                }

                //get photos after subcats
                $photosOffset = 0;

                $grabPhotosCount = 9 - count($itemList['subCats']);

                if ($grabPhotosCount > 0)
                {

                    if ($result = $db->select("SELECT * FROM photos WHERE cat = %s and subCat = 0 ORDER BY title LIMIT 0, $grabPhotosCount", $catID))
                    {
                        if ($db->numRows > 0)
                        {
                            foreach ($result as $row)
                            {
                                $photosOffset++;
                                addPhotoToItemList($row);
                            }
                        }
                    }
                    else
                    {
                        throwDBError();
                    }


                }


                //get photos in maincat

                $photosCountResult = $db->count('photos', '*', array('cat' => $catID, 'subCat' => 0));

                if (is_numeric($photosCountResult))
                {

                }
                else
                {
                    throwDBError();
                }

                if ($pageNum < 1)
                {
                    $pageNum = 1;
                }
                else if ($pageNum > $subCatLastPage) //get photos
                {
                    if (count($itemList) < 9)
                    {
                        //load in photos for cat




                    }

                    //redirect('/gallery/?cat=' . $catID . '&pg=' . $lastPage);
                }

                //pagination
                $totalCounts = $grabPhotosCount + $subCatCountResult;

                $lastPage = ceil($totalCounts/$PerPage);

                if ($lastPage < 1)
                {
                    $lastPage = 1;
                }

                $pageNum = (isset($_GET['pg'])) ? trim($_GET['pg']) : 1;    
                $pageNum = preg_replace('#[^0-9]#', '', $pageNum);





            }
            else
            {
                throwDBError();
            }

            echo '<pre>';
            print_r($itemList);
            echo '</pre>';

        }
        else
        {
            echo '<div align="center">Category not found.</div>';
        }

    }
    else
    {
        throwDBError();
    }
?>

1 个答案:

答案 0 :(得分:0)

您可以创建行号变量,然后从该派生表中进行选择。对你的桌面结构进行了一次尝试,这样的事情可以起作用:

SELECT *
FROM (
  SELECT  p.id, 
          p.title, 
          sc.title as subcat_title,
          @curRow := @curRow + 1 AS row_number
  FROM    photos p 
  INNER JOIN subCats sc on p.subcat = sc.id
  INNER JOIN (SELECT @curRow := 0) r 
  ) AS tbl
WHERE row_number>3 AND row_number<=6

WHERE子句中设置开始和结束分页行数。

以上是小提琴:http://sqlfiddle.com/#!2/107f7/9/0