I have two mysql tables in following format
Table 1 - Institute Basic Details (institute_basic_details)
id | institute_unique_id | institute_name | institute_city
1 ABCD1234 NAME-1 AAA
2 EFGH4567 NAME-2 BBB
3 IJKL8999 NAME-3 CCC
Table 2 - Institute Owner Details (institute_owners_details)
id | institute_unique_id | owner_name | owner_email_id | owner_mobile
1 ABCD1234 OWNER-A-1 [email protected] 98811xxxxx
2 ABCD1234 OWNER-B-2 [email protected] 87454XXXXX
3 ABCD1234 OWNER-C-3 [email protected] 54785XXXXX
4 EFGH4567 OWNER-D-1 [email protected] 47874XXXXX
5 EFGH4567 OWNER-E-2 [email protected] 45455XXXXX
6 IJKL8999 OWNER-F-1 [email protected] 78478XXXXX
7 IJKL8999 OWNER-G-2 [email protected] 87478XXXXX
Table 2 Contains Un-Even numbers of owners per Institute...
I want to export combined data from both table in excel / csv in following format...
CELLs In A Row - for example
LIKE : In A Row - Institute Name, Institute City, Owner 1 Name , Owner 1 Email, Owner 1 Mobile, Owner 2 Name (if Exists), Owner 2 Email (if Exists), Owner 2 Mobile (if Exists),.... And So on for owner 3, owner 4, owner 5 ......
Currently I am using following Code to export data to csv from One Table only....
<?php
include("db.php");
$query = "SELECT * FROM institute_basic_details ORDER BY id ASC";
$numrows = $database->num_rows($query);
if($numrows > 0){
$delimiter = ",";
$filename = "INSTITUTE-LIST-" . date('d-F-Y-H-i-s') . ".csv";
$count = 1;
$f = fopen('php://memory', 'w');
$fields = array("SERIAL NUMBER", "INSTITUTE NAME", "INSTITUTE CITY");
fputcsv($f, $fields, $delimiter);
$result = $database->get_results($query);
foreach($result as $row){
$lineData = array($count, $row['institute_name'], $row['institute_city']);
}
fseek($f, 0);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
fpassthru($f);
}
?>
institute_basic_detailsyou can do a query for the corresponding owners. Then you can loop over those results, usingarray_merge()to add them to the output array.