phpmysqlloopsquickbooksintuit-partner-platform

Issue with mysql loop


Intuit integration. With create invoice. I am needing to get items from a loop when I create the invoice.

I also tried by creating the loop first and using $items in the invoice create but also get an error. "Format Error. Expect an element of an array or an object."

Not liking my loop. How can I accomplish this ?

$theResourceObj = Invoice::create([
"TotalAmt" => 200.00, 
    
    "Line" => [
      
$queryitems = 'SELECT * FROM mydatabse',
$query_items  = mysqli_query($con,$queryitems),
while($row = mysqli_fetch_array($query_items)) { 

      [
        "Description"=> "Loop Description", 
        "DetailType"=> "SalesItemLineDetail", 
        "SalesItemLineDetail"=> [
          "TaxCodeRef"=> [
            "value"=> "TAX"
          ], 
          "Qty"=> 1, 
          "UnitPrice"=> 200, 
          "ItemRef"=> [
            "name"=> "Loop Name", 
            "value"=> "5"
          ]
        ], 
        "LineNum"=> 1, 
        "Amount"=> 200.00, 
        "Id"=> "1"
      ],
}
        
    
    ],
    "CustomerRef"=> [
          "value"=> 1
    ],
    "BillEmail" => [
          "Address" => "email@somewhere.com"
    ]
]);

Solution

  • You can't put loops inside array literals.

    Construct the array incrementally outside the call to Invoice::create().

    $invoice = [
        "TotalAmt" => 200.00,
        "CustomerRef"=> [
            "value"=> 1
        ],
        "BillEmail" => [
            "Address" => "email@somewhere.com"
        ]
    ];
    $lines = []; 
    $queryitems = 'SELECT * FROM mydatabse';
    $query_items  = mysqli_query($con,$queryitems);
    while($row = mysqli_fetch_array($query_items)) { 
        $lines[] = [
            "Description"=> "Loop Description", 
            "DetailType"=> "SalesItemLineDetail", 
            "SalesItemLineDetail"=> [
                "TaxCodeRef"=> [
                    "value"=> "TAX"
                ], 
                "Qty"=> 1, 
                "UnitPrice"=> 200, 
                "ItemRef"=> [
                    "name"=> "Loop Name", 
                    "value"=> "5"
                ]
            ], 
            "LineNum"=> 1, 
            "Amount"=> 200.00, 
            "Id"=> "1"
        ];
    }
    
    $invoice['Line'] = $lines;
    
    $theResourceObj = Invoice::create($invoice);