Solved

create csv file from recordset  in php

Posted on 2016-10-14
4
36 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
  • 2
4 Comments
 
LVL 108

Expert Comment

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

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 52

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now