I have a table which one column contains a comma seperated value. I need to, within the query, get those values, explode them, and do a query to another table.
Let me explain the structure.
Table 1: courses. Columns we are worried about for this example:
courses.id, courses.products
This will return a value
$query['id']-> 1000
$query['products']-> 1,5
Within the query I need to pull the product name for each product from the products table
so
products.productname
even if i have to create 5 productname columns in the query because no one should have more than 5 products.
The point of all of this is I have to dump this to a spreadsheet and i need to have the product names in there.
To accomplish the CSV dump I have this code. ( I already have the query written however right now just returning "1,5" instead of product names
$query = " QUERY GOES HERE ";
$result = mysql_query($query, $db) or die(mysql_error());
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++)
{
$headers[] = mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result)
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_row($result))
{
fputcsv($fp, array_values($row));
}
die;
}