0

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

  • you can export any select directly into a csv https://stackoverflow.com/questions/356578/how-can-i-output-mysql-query-results-in-csv-format – nbk Mar 15 '22 at 22:52
  • i dont see the connection? i need to merge columns before exporting to csv... the export itself isnt the issue its the correct way to merge the 2 columns together before generating the csv – The Oz Snowman Mar 15 '22 at 23:03
  • Simply change the SELECT TO SELECT CONCAT(clo1,'>',col2) and then use your export – nbk Mar 15 '22 at 23:55
  • doesn't seem to work... tried: $sql = "SELECT CONCAT(`productCategory`,'>',`productSubCategory`) ".$field." FROM 'Product' ORDER BY `productSKU` "; – The Oz Snowman Mar 16 '22 at 01:02

0 Answers0