I want to export my database to an csv file. I have this alrealdy:
$query=mysqli_query($conexion, "SELECT * FROM table_name") or die("Problemas con la
conexión: ".mysqli_error($conexion));
$variable_name = array();
if (mysqli_num_rows($query) > 0) {
while ($row = mysqli_fetch_assoc($query)) {
$variable_name[] = $row;
}
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="filename.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$output = fopen('php://output', 'w');
fputcsv($output, array('col1', 'col2', 'col3', 'col4', 'col5', 'col6',
'col7', 'col8', 'col9', 'col10'));
if (count($varible_name) > 0) {
foreach($variable_name as $row) {
fputcsv($output, $row);
}
}
fclose($output);
This is working fine, but I just realice that one column has the id value an not the real value that is related to another table. The thing is that I want to replace the id numbers of that column with the real names of the values. Let's say that col3 has values of 1, 2, or 4 and I want to replace those values with 'Column value for 1', 'Column value for 2', etc. I try to join the tables but it joined the ids, so i still have the numbers an not the named values.
How can I do it? thank you
UPDATE.
Example of the table to export.
table1
id name job payment workshop
1 susan job1 yes 1
2 mike job2 yes 3
3 bob job3 no 2
4 rob job3 no 3
table2
id workshop_name
1 cooking
2 talking
3 running
When I try to join both tables the cvs file was:
id name job payment workshop 1 susan job1 yes 1 cooking 2 mike job2 yes 3 running 3 bob job3 no 2 talking 4 rob job3 no 3 running
What I want is to replace the numbers in col workshop with the names form table2: cooking running talking. Maybe with a simple switch i can change the numbers with the names, but I don't know how to do it.
Thanks