2016-11-30 4 views
0

それは、インラインデータを作成するための良い方法であり、Angularjsのphpとmysqlを使った請求書アイテムです。 ?これはidpayPricepayStatusclientAddressをelemetsとitems Contantを請求書intemsデータを持つネストされた配列でどのように配列をインボイスデータとAngularjsのPHPとmysqlで作成する

は、私は配列を持って欲しいです。

$result = $db->query("select `id`,`payPrice`,`payStatus`,`clientAddress` 
    from `invoice` where `clientId`=22"); 
if($result && $result->num_rows>0){ 
    if($row=$result->fetch_assoc()){ 

     $result1 = $db->query("select * from `invoice_items` where `invoiceId`=". $row['id']); 
     if($result1 && $result1->num_rows>0){ 
       $row["items"]=$result1; 
       return $row; 
     }else{return false;} 

    }else{return false;} 
}else{return false;} 

答えて

0

はい、私はあなたがテーブルからメイクアレイの良いtechniqeを選択したと思うより最初(のように多くはないくぼみ)をフェイルビットの配列を取得することです。 2つの表を結合すると、各行に2つの表の列が含まれるため、ネストできません。

-1

あなたは2番目のクエリで$result変数を再利用し、実際に2番目のクエリからデータをフェッチされていません。あなたが参加し、MySQLでは、配列のデータ型を作成することはできません

function xxx() 
{ 

    $inv = $db->query("select `id`,`payPrice`,`payStatus`,`clientAddress` 
          from `invoice` where `clientId`=22"); 
    if($inv && $inv->num_rows > 0){ 

     // should only ever be one of these 
     if($invoice = $inv->fetch_assoc()){ 

      $items = $db->query("select * 
            from `invoice_items` 
            where `invoiceId` = {$invoice['id']}"); 

      if($items && $items->num_rows > 0){ 
       // loop round possibly many items rows 
       while ($item = $items->fetch_assoc()) { 
        $invoice["items"][] = $item; 
       } 
       return $invoice; 

      } else { 
       return false; 
      } 

     } else { 
      return false; 
     } 
    } else { 
     return false; 
    } 
} 
+0

私はここで2番目の結果を編集しました。 –

+0

申し訳ありませんが私は理解できませんか? – RiggsFolly

0

は、すべてのデータを取得しますが、それは、各レコードに添付請求書を持つすべてのアイテムになります。あなたはその後、次のテーブルとデータ

INVOICE TABLE 
[ id | name | date ] 
[ 1 | Stephen | 30th November 2016 ] 

ITEMS TABLE 
[ id | invoice_id | name | price ] 
[ 1 | 1 | Wine | 12.99 ] 
[ 2 | 1 | Cheese | 4.99 ] 

を持っている場合、参加たとえば

すると、この

SELECT 
    `invoice`.`*`, 
    `item`.`*` 
FROM 
    `invoices` AS `invoice` 
INNER JOIN 
    `items` AS `item` ON (`invoice`.`id` = `item`.`invoice_id`); 

のようなクエリでは、「フォロー行

id | name | date | id | invoice_id | name | price 
1 | Stephen | 30th November 2016 | 1 | 1 | Wine | 12.99 
1 | Stephen | 30th November 2016 | 2 | 1 | Cheese | 4.99 

私はドンあなたを与えるだろうこれはあなたが探しているものだと思っていますが、いずれにしてもそれを使うことができます。とにかく、ここにあなたのコードのスニペットが

$query = $db->query("SELECT `id`, `payPrice`, `payStatus`, `clientAddress` FROM `invoice` WHERE `clientId` = 22"); 
if(! $query || $query->num_rows != 1) { 
    return false; 
} 

$invoice = $query->fetch_assoc(); 
$items = $db->query("SELECT * FROM `invoice_items` WHERE `invoiceId` = {$invoice['id']}"); 
if(! $items || $items->num_rows < 1) { 
    return false; 
} 

$invoice['items'] = $items->fetch_assoc(); 
return $invoice; 
+0

私はこのターゲットのための最善の解決策を探しています。 –

+0

私はあなたが達成しようとしているものの正しい道を進んでいると思います。もしそれをより良くしたいのであれば、アプリケーションの中で再利用するために関数の一部を置くことができます。 – HelloSpeakman

関連する問題