1

次のテーブルを持つ「library」という名前のデータベースがあります。

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

書籍は棚に保管されているので、クエリで棚ごとに4冊の本だけを返したいと思います。棚に4冊以上の本がある場合は、すでに表示されている本を繰り返さずに、その棚を2回表示する必要があります。

これは棚のテーブルです:

+----+------+---------------------------------+
| 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"
        }
    ]
}

]

2番目の棚には7冊の本が含まれているため、最初の4冊と最後の3冊の2回表示する必要があります。これは私が立ち往生していたことです。ご回答ありがとうございます!

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 に答える