1

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

+-------------------+
| 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 本书。这是我被卡住了。谢谢您的反馈!

4

1 回答 1

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
    );
}
于 2013-02-16T07:38:12.820 回答