I have a CSV export script which grabs certain fields from a certain table and their values and creates an Excel file with the data. See script below.
It first grabs the column names (except for the ID). Secondly it will grab the values of firstname, lastname, employee_id, unit and present and puts all these values in an Excel file.
Now note that it grabs the data from the table: table_info In this table, the UNIT and PRESENT exists out of numbers only. These numbers represent the ID's of the unit/present from two other tables:
table_units (id, title) table_presents (id, title)
What I would like to have is that in the Excel sheet it doesn't show the numbers (id's) of the unit/present but the title of the unit/present. How can I accomplish this? My PHP knowledge just stops here.
$table = 'table_info';
$file = 'export';
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
if($row['Field'] != 'id'){
$csv_output .= $row['Field']."; ";
$i++;
}
}
}
$csv_output .= "
";
$values = mysql_query("SELECT firstname, familyname, employee_id, unit, present FROM ".$table."") or die(mysql_error());
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "
";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;