0

In fetching data from file_get_contents function to add the data in array. The data then added to excel with XLSXWriter. It looks like the respond to foreach is limited to 100. Is this possible to change so it could be a larger amount?

$data = file_get_contents("/home/user/testfile.json");
$json_obj = json_decode($data, true);
  
foreach ($json_obj['items'] as $order) {
  
$ordrenr =  $order['id'];
$fornavn= $order['billingPerson']['firstName'];
$etternavn= $order['billingPerson']['lastName'];
$adresse = $order['billingPerson']['street'];
$postnr = $order['billingPerson']['postalCode'];
$sted = $order['billingPerson']['city'];
$mobil = $order['billingPerson']['phone'];
$epost = $order['email'];
$subtotal = $order['total'];
$bestilt = $order['createDate'];
$betalingstype = $order['paymentMethod'];
$fakturanr = $order['invoices'][0]['id'];
$fakturalink = $order['invoices'][0] ['link'];
     
 $header = ['Ordrenr' => 'string',
'Fornavn' => 'string',
'Etternavn' => 'string',
'Adresse' => 'string',
'Postnr' => 'string',
'Sted' => 'string',
'Mobil' => 'string',
'Epost' => 'string',
'Bestilt' => 'string',
'OrdreSum Inkl MVA' => 'string',
'Betalingtype' => 'string',
'Faktura nr' => 'string',
'Ordreskjema' => 'string' ]; 
  
$rows[] = [$ordrenr, $fornavn, $etternavn, $adresse , $postnr, $sted , $mobil, $epost, $bestilt, $subtotal, $betalingstype , $fakturanr, $fakturalink];
}

$structure = "/home/user/testfolder/";
if (!file_exists ($structure))

{
mkdir($structure, 0777, true);
}
 
$writer = new XLSXWriter(); 
$writer->setAuthor('Author'); 
$writer->writeSheet($rows, 'SHEETNAME', $header);
$writer->writeToFile("/home/user/testfolder/"  . "April_".  "$from". "_data.xlsx"  );
Addis1406
  • 15
  • 4

1 Answers1

0
$header = ['Ordrenr' => 'string',
'Fornavn' => 'string',
'Etternavn' => 'string',
'Adresse' => 'string',
'Postnr' => 'string',
'Sted' => 'string',
'Mobil' => 'string',
'Epost' => 'string',
'Bestilt' => 'string',
'OrdreSum Inkl MVA' => 'string',
'Betalingtype' => 'string',
'Faktura nr' => 'string',
'Ordreskjema' => 'string' ]; 

First, the above code should be exclude from foreach loop. Secondly, are you getting an execution time limit exceed error or what? If yes, then you can increase limit by set max_execution in php.

ini_set('max_execution_time', '0');
Sachin
  • 397
  • 4
  • 13
  • No error, and the scripts are not timing out. Looks like it only fetches 100 arrays from the foreach. ( the script should fetch ca 150 ) – Addis1406 Apr 25 '22 at 11:32
  • Could you please compare $rows[] before writing the xls and $json_obj['items'] by printing array and check if both are same or not? – Sachin Apr 25 '22 at 11:41
  • It looks like this is not something that is caused by php. The json file that was used for the foreach was limited. {"total":144,"count":100,"offset":0,"limit":100,"items": } – Addis1406 Apr 25 '22 at 11:52
  • How many rows you get when you print $json_obj['items']? – Sachin Apr 25 '22 at 11:55
  • the respons from $json_obj['items'] is the same as the $rows (json file result was limited to 100 ) - So problem solved – Addis1406 Apr 25 '22 at 12:00
  • Please upvote my answer/comment if you find this helpful. – Sachin Apr 25 '22 at 12:02