I have a csv import export function where the database table has a category and a sub category column within the data structure.
The Database Fields are: 'SKU*', 'Name', 'Origin', 'Stock', 'Category', 'SubCategory', 'RRP', 'Price Enq', 'Description', 'Dimensions', 'SEO Meta Description', 'SEO Page Title', 'Priority'
I need to implode the values of each of these with a separator of >
so it would be Category>SubCategory in one column for both the heading line and the line values of each row.
now i know on an import from a csv i can run an explode to split the 2 columns out from the csv:
$temp = explode(">", strtoupper($data["productCategory"]));
$data["productCategory"] = trim($temp["0"]);
$data["productSubCategory"] = trim($temp["1"]);
but i cannot for the life of me work out how to take the 2 columns from the database and implode them correctly
id assumed that this would work:
// Merge productCategory and productSubCategory
$temp = implode(strtoupper($data["productCategory"])).">". implode(strtoupper($data["productSubCategory"]));
$data["productCategory"] = $temp;
but I cant work out how to incorporate that correctly into the export code im working with:
$field = "";
$values = array();
// Generate the Heading Row & SQL Select Keys
if (isset($layout) === true) {
foreach ($layout as $data => $ar) {
if ($ar['csv'] === true || $ar['csv'] == "Y") {
// Build SQL Select Query
if (strlen($field) > 0) {
$field .= ', ';
}
$field .= "`".$data."`";
// Add Heading to Array
array_push($values, $ar['heading']);
}
}
// Add Heading to Array
array_push($values, "Delete");
}
// Open New CSV File
$fp = fopen('product_data.csv', 'w');
// Insert Heading Row
fputcsv($fp, $values);
$sql = "SELECT ".$field." FROM 'Product' ORDER BY `productSKU` ";
$result = sql_exec($sql);
while ($line = $result->fetch_assoc()) {
// Reset values array for each line
$values = array();
// Go through data and generate array for fputcsv()
foreach ($line as $key => $data) {
// Decode HTML Entities in the Data (so people know what they're looking at)
$data = html_entity_decode($data, ENT_QUOTES | ENT_HTML401, "UTF-8");
// Add Data to array for fputcsv
array_push($values, $data);
}
// Add line to the CSV
fputcsv($fp, $values);
}
fclose($fp);
can anyone please exlain or advise how to achieve this?
TIA