Solved

create csv file from recordset  in php

Posted on 2016-10-14
4
63 Views
Last Modified: 2016-10-15
I have a table named packings with two fields.
 boxno Item
records are like this
 boxno Item
1 X0016SFS03
1 X0017Y11U7
1 X000YF7GWH
2 X0018C6JU5
2 X0018C5D9D
2 X0018C5D9D
2 X0018C9YQB
2 X0018C5D9D
2 X0018C5D9D
3 X0018C51I1
3 X000WIVZPF
3 X0018AQ2B3
3 X0018C5D9D
3 X0018C5D9D
3 X0018C5D9D
3 X0018C5D9D

The boxno can be any int value - not limited to 3
Now I required to get output file in csv/text format using fputcsv like this
Box no in header and items below that as records
box 1,box2,box3,box4,box5
X0016SFS03,X0018C6JU5,X0018C51I1
X0017Y11U7,X000YF7GWH,

I want help in php to do so.

Thanks and regards,
Rajesh.
0
Comment
Question by:Rajesh Joshi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41844265
Is the table a SQL database table?
0
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41844444
Assume  MySQL

<?php
// GET THE DB HANDLE
$mysqli = new mysqli('localhost','user','password','database');

// GET DATA FROM DB
$query = "SELECT * FROM packings ORDER BY boxno";
$result =  $mysqli->query($query);

// SOMEWHERE TO COLLECT THE RESULTS
$boxes = array();
// LOOP THROUGH RESULTS
while($row = $result->fetch_object()) {
  // HAVE WE SEEN THIS BEFORE
  // IF NOT CREATE A SPACE FOR IT
  if (!isset($boxes[$row->boxno])) {
    $boxes[$row->boxno] = array();
  }
  // ADD THE item TO THE RIGHT BOX
  $boxes[$row->boxno][] = $row->item;
}
// OPEN csv FILE
$file = fopen('t1715.csv','wt');

// GET THE KEYS OF THE BOX ARRAY
// TO BUILD OUR HEADER
$header = array_keys($boxes);

// PREFIX HEADERS WITH `box`
foreach($header as &$h) {
  $h = "box {$h}";
}
// WRITE HEADER
fputcsv($file, $header);

// WE ARE NOT DONE YET
$moredata = true;
while($moredata) {
  // START A NEW ROW
  $current = array();
  
  // ASSUME THIS IS THE LAST ROW
  $moredata = false;
  foreach($boxes as &$box) {
    // BOX NOT EMPTY SO
    // WE ARE NOT DONE
    // SHIFT THE VALUE OFF AND ADD IT 
    // TO THE CURRENT ROW
    if (!empty($box)) {
      $moredata=true;
      $current[] = array_shift($box);
    }
    // BOX IS EMPTY SO ADD A SPACE
    else {
      $current[] = '';
    }
  }
  // DON'T OUTPUT THE BLANK ROW AT THE END
  if ($moredata) {
    fputcsv($file, $current);
  }
}
// CLEAN UP
fclose($file);

Open in new window

1
 
LVL 1

Author Closing Comment

by:Rajesh Joshi
ID: 41844704
Excellent. Thank you Dear Mr.Julian Hansen
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41844725
You are most welcome.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modify Filter to cause a field to be not Required 3 50
Help installing Laravel app on MAMP on MAC 7 56
Form submit takes only for one form 23 52
Format Date 7 28
This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question