2013-02-16 4 views
1

나는 다음과 같은 테이블 "라이브러리"라는 이름의 데이터베이스가 있습니다슬라이스 MySQL의 결과

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

]

제 2 랙부 7 그래서 따라서 회 표시해야 서, 제 4 및 마지막 3 책을 포함한다. 이것은 내가 붙어 버린 것입니다. 귀하의 답변에 감사드립니다!

+1

당신은 당신이 그를 분할해야하는 이유를 설명 할 수 있습니까? 또한 동일한 쿼리에서 모든 서가와 서적을 반환하지 않는 이유는 무엇입니까? 루프 내에서 데이터베이스를 치는 것이 비용이 많이들 수 있습니다. – sgeddes

+1

각 슬라이드에 4 권의 책이 들어있는 슬라이드에 슬라이드를 표시하려고합니다. rhinoslider라는 jquery 플러그인을 사용하여 책을 선물합니다. – potomato

답변

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 
    ); 
} 
+1

고마워요! 그것은 꽤 정직한 대답입니다. 건배! – potomato