Hi all

I need help with my mysql data dump into excel. In principle my requirements are simple, its just advice on how to code it I need help with.

I run a report online and X numbers of unique records are retrieved as such:

ID | FIELD1 | FIELD2 | FIELD3 | FIELD4

And I export those records into an excel document using the following code:

$select = "SELECT field1, field2, field3, field4 etc etc";                 
$export = mysql_query($select); $fields = mysql_num_fields($export); // get header information for output for ($i = 0; $i < $fields; $i++) { $header .= mysql_field_name($export, $i) . "\t"; } // now get the rows of data from the database while($row = mysql_fetch_row($export)) { $line = ''; foreach($row as $value) {
if ((!isset($value)) || ($value == "")) { $value = "\t"; // tab across } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r","",$data); header("Content-type: application/x-msdownload"); header("Content-Disposition: attachment; filename=export.xls"); header("Pragma: no-cache"); header("Expires: 0"); print "$header\n$data"; // output exit();

Now, what I need to do is run a 2nd query inside the above query which gets records associated to the main record above and lists them in a way I have listed on the attachment (example.xls).

The first part of my export puts each record on one line, but I now need to run the second query and assign these fields to the parent record BEFORE the next parent record is outputted.

Please see my attachment for an example of what I mean.

Can anyone help with highlighting in my code how I can achieve this?

Thanks for reading and any support you offer.

K

    I have managed to completed this with the use of multiple new lines (\n) and tabs (\t).

    Took a while but I got there.

    K

      Write a Reply...