切片MySQL结果

时间:2013-02-16 02:57:43

标签: php mysql json

我有一个名为“library”的数据库,其中包含以下表格:

+-------------------+
| Tables_in_library |
+-------------------+
| books             |
| shelves           |
+-------------------+

书籍存放在书架上,我想在查询中每个书架只返回四(4)本书。如果书架有超过4本书,那么该书架必须显示两次而不重复已经显示的书籍。

这是货架表:

+----+------+---------------------------------+
| id | uid  | description                     |
+----+------+---------------------------------+
|  1 | 1000 | Book and TV Storage Combination |
|  2 | 1001 | Shelving Unit                   |
+----+------+---------------------------------+

和书籍表:

+----+------+-------+----------------------------------------------------+
| id | uid  | shelf | title                                              |
+----+------+-------+----------------------------------------------------+
|  1 | 1000 | 1000  | The Mythical Man-Month                             |
|  2 | 1001 | 1001  | Code Complete                                      |
|  3 | 1002 | 1000  | The Art of Computer Programming                    |
|  4 | 1003 | 1001  | The Pragmatic Programmer                           |
|  5 | 1004 | 1001  | Structure and Interpretation of Computer Programs  |
|  6 | 1005 | 1000  | Compilers: Principles, Techniques, and Tools       |
|  7 | 1006 | 1001  | The C Programming Language                         |
|  8 | 1007 | 1001  | Introduction to Algorithms                         |
|  9 | 1008 | 1000  | Patterns of Enterprise Application Architecture    |
| 10 | 1009 | 1001  | Refactoring: Improving the Design of Existing Code |
| 11 | 1010 | 1001  | Design Patterns                                    |
+----+------+-------+----------------------------------------------------+

我将使用JSON打印结果,这就是我的工作方式:

function getShelves(){
    $query = mysql_query("SELECT * FROM shelves") or die(mysql_error());
    return $query;
}

function getBooksFromShelf($shelf){
    $query = mysql_query("SELECT * FROM books WHERE shelf = '$shelf'") or die(mysql_error());
    return $query;
}

$response = array();
$shelves = getShelves();
while($s = mysql_fetch_assoc($shelves)){
    $books = getBooksFromShelf($s["uid"]);
    $bookList = array();
    while($b = mysql_fetch_assoc($books)){
        $bookList[] = array(
            "uid" => $b["uid"],
            "title" => $b["title"]
        );
    }
    $response[] = array(
        "shelf" => $s["uid"],
        "books" => $bookList
    );
}

echo json_encode($response);

这导致:

[
{
    "shelf": "1000",
    "books": [
        {
            "uid": "1000",
            "title": "The Mythical Man-Month"
        },
        {
            "uid": "1002",
            "title": "The Art of Computer Programming "
        },
        {
            "uid": "1005",
            "title": "Compilers: Principles, Techniques, and Tools"
        },
        {
            "uid": "1008",
            "title": "Patterns of Enterprise Application Architecture "
        }
    ]
},
{
    "shelf": "1001",
    "books": [
        {
            "uid": "1001",
            "title": "Code Complete "
        },
        {
            "uid": "1003",
            "title": "The Pragmatic Programmer"
        },
        {
            "uid": "1004",
            "title": "Structure and Interpretation of Computer Programs"
        },
        {
            "uid": "1006",
            "title": "The C Programming Language"
        },
        {
            "uid": "1007",
            "title": "Introduction to Algorithms"
        },
        {
            "uid": "1009",
            "title": "Refactoring: Improving the Design of Existing Code"
        },
        {
            "uid": "1010",
            "title": "Design Patterns"
        }
    ]
}

第二个书架包含7本书,因此必须显示两次,前4页和后3本书。这是我被卡住了。感谢您的回复!

1 个答案:

答案 0 :(得分:3)

您可以使用array_chunk将较大的数组拆分为多个数组,例如在你的情况下4:

http://www.php.net/manual/en/function.array-chunk.php

$pieces = array_chunk($bookList, 4);
foreach($pieces as $bookRow) {
    $response[] = array(
        "shelf" => $s["uid"],
        "books" => $bookRow
    );
}