php - Export data from a database to a .csv file -
i have php file exports data database csv file.
i want add more fields database export.
original file:
<?php defined( '_jexec' ) or die( 'restricted access' ); jimport('joomla.filesystem.folder'); class iesimpleexport extends iecontroller{ function view(){ $jshopconfig = jsfactory::getconfig(); $ie_id = jrequest::getint("ie_id"); $_importexport = jtable::getinstance('importexport', 'jshop'); $_importexport->load($ie_id); $name = $_importexport->get('name'); $ie_params_str = $_importexport->get('params'); $ie_params = parseparamstoarray($ie_params_str); $files = jfolder::files($jshopconfig->importexport_path.$_importexport->get('alias'), '.csv'); $count = count($files); jtoolbarhelper::title(_jshop_export. ' "'.$name.'"', 'generic.png' ); jtoolbarhelper::custom("backtolistie", "back", 'browser.png', _jshop_back_to.' "'._jshop_panel_import_export.'"', false ); jtoolbarhelper::spacer(); jtoolbarhelper::save("save", _jshop_export); include(dirname(__file__)."/list_csv.php"); } function save(){ $mainframe = jfactory::getapplication(); include_once(jpath_component_site."/lib/csv.io.class.php"); $ie_id = jrequest::getint("ie_id"); if (!$ie_id) $ie_id = $this->get('ie_id'); $_importexport = jtable::getinstance('importexport', 'jshop'); $_importexport->load($ie_id); $alias = $_importexport->get('alias'); $_importexport->set('endstart', time()); $params = jrequest::getvar("params"); if (is_array($params)){ $paramsstr = parsearraytoparams($params); $_importexport->set('params', $paramsstr); } $_importexport->store(); $ie_params_str = $_importexport->get('params'); $ie_params = parseparamstoarray($ie_params_str); $jshopconfig = jsfactory::getconfig(); $lang = jsfactory::getlang(); $db = jfactory::getdbo(); $query = "select values.name_en-gb value, prod.product_id, prod.product_ean, prod.product_quantity, prod.product_date_added, prod.product_price, tax.tax_value tax, prod.`".$lang->get('name')."` name, prod.`".$lang->get('short_description')."` short_description, prod.`".$lang->get('description')."` description, cat.`".$lang->get('name')."` cat_name `#__jshopping_products` prod left join `#__jshopping_products_to_categories` categ using (product_id) left join `#__jshopping_categories` cat on cat.category_id=categ.category_id left join `#__jshopping_taxes` tax on tax.tax_id = prod.product_tax_id left join `#__jshopping_products_extra_field_values` values using (name_en-gb) group prod.product_id"; $db->setquery($query); $products = $db->loadobjectlist(); $data = array(); $head = array("product_id","ean","qty","date","price","tax","category","name","short_description","description", "value"); $data[] = $head; foreach($products $prod){ $row = array(); $row[] = $prod->product_id; $row[] = $prod->product_ean; $row[] = $prod->product_quantity; $row[] = $prod->product_date_added; $row[] = $prod->product_price; $row[] = $prod->tax; $row[] = utf8_decode($prod->value); $row[] = utf8_decode($prod->cat_name); $row[] = utf8_decode($prod->name); $row[] = utf8_decode($prod->short_description); $row[] = utf8_decode($prod->description); $data[] = $row; } $filename = $jshopconfig->importexport_path.$alias."/".$ie_params['filename'].".csv"; $csv = new csv(); $csv->write($filename, $data); if (!jrequest::getint("noredirect")){ $mainframe->redirect("index.php?option=com_jshopping&controller=importexport&task=view&ie_id=".$ie_id, _jshop_completed); } } function filedelete(){ $mainframe = jfactory::getapplication(); $jshopconfig = jsfactory::getconfig(); $ie_id = jrequest::getint("ie_id"); $_importexport = jtable::getinstance('importexport', 'jshop'); $_importexport->load($ie_id); $alias = $_importexport->get('alias'); $file = jrequest::getvar("file"); $filename = $jshopconfig->importexport_path.$alias."/".$file; @unlink($filename); $mainframe->redirect("index.php?option=com_jshopping&controller=importexport&task=view&ie_id=".$ie_id); } } ?>
i need export custom data table jshopping_products_extra_field_values
, column name name_en-gb
.
i modified code, after exporting, nothing exports except first line titles of fields.
my code:
<?php defined( '_jexec' ) or die( 'restricted access' ); jimport('joomla.filesystem.folder'); class iesimpleexport extends iecontroller{ function view(){ $jshopconfig = jsfactory::getconfig(); $ie_id = jrequest::getint("ie_id"); $_importexport = jtable::getinstance('importexport', 'jshop'); $_importexport->load($ie_id); $name = $_importexport->get('name'); $ie_params_str = $_importexport->get('params'); $ie_params = parseparamstoarray($ie_params_str); $files = jfolder::files($jshopconfig->importexport_path.$_importexport->get('alias'), '.csv'); $count = count($files); jtoolbarhelper::title(_jshop_export. ' "'.$name.'"', 'generic.png' ); jtoolbarhelper::custom("backtolistie", "back", 'browser.png', _jshop_back_to.' "'._jshop_panel_import_export.'"', false ); jtoolbarhelper::spacer(); jtoolbarhelper::save("save", _jshop_export); include(dirname(__file__)."/list_csv.php"); } function save(){ $mainframe = jfactory::getapplication(); include_once(jpath_component_site."/lib/csv.io.class.php"); $ie_id = jrequest::getint("ie_id"); if (!$ie_id) $ie_id = $this->get('ie_id'); $_importexport = jtable::getinstance('importexport', 'jshop'); $_importexport->load($ie_id); $alias = $_importexport->get('alias'); $_importexport->set('endstart', time()); $params = jrequest::getvar("params"); if (is_array($params)){ $paramsstr = parsearraytoparams($params); $_importexport->set('params', $paramsstr); } $_importexport->store(); $ie_params_str = $_importexport->get('params'); $ie_params = parseparamstoarray($ie_params_str); $jshopconfig = jsfactory::getconfig(); $lang = jsfactory::getlang(); $db = jfactory::getdbo(); $query = "select values.name_en-gb value, prod.product_id, prod.product_ean, prod.product_quantity, prod.product_date_added, prod.product_price, tax.tax_value tax, prod.`".$lang->get('name')."` name, prod.`".$lang->get('short_description')."` short_description, prod.`".$lang->get('description')."` description, cat.`".$lang->get('name')."` cat_name `#__jshopping_products` prod left join `#__jshopping_products_to_categories` categ using (product_id) left join `#__jshopping_categories` cat on cat.category_id=categ.category_id left join `#__jshopping_taxes` tax on tax.tax_id = prod.product_tax_id left join `#__jshopping_products_extra_field_values` values using (name_en-gb) group prod.product_id"; $db->setquery($query); $products = $db->loadobjectlist(); $data = array(); $head = array("product_id","ean","qty","date","price","tax","category","name","short_description","description", "value"); $data[] = $head; foreach($products $prod){ $row = array(); $row[] = $prod->product_id; $row[] = $prod->product_ean; $row[] = $prod->product_quantity; $row[] = $prod->product_date_added; $row[] = $prod->product_price; $row[] = $prod->tax; $row[] = $prod->value; $row[] = utf8_decode($prod->cat_name); $row[] = utf8_decode($prod->name); $row[] = utf8_decode($prod->short_description); $row[] = utf8_decode($prod->description); $data[] = $row; } $filename = $jshopconfig->importexport_path.$alias."/".$ie_params['filename'].".csv"; $csv = new csv(); $csv->write($filename, $data); if (!jrequest::getint("noredirect")){ $mainframe->redirect("index.php?option=com_jshopping&controller=importexport&task=view&ie_id=".$ie_id, _jshop_completed); } } function filedelete(){ $mainframe = jfactory::getapplication(); $jshopconfig = jsfactory::getconfig(); $ie_id = jrequest::getint("ie_id"); $_importexport = jtable::getinstance('importexport', 'jshop'); $_importexport->load($ie_id); $alias = $_importexport->get('alias'); $file = jrequest::getvar("file"); $filename = $jshopconfig->importexport_path.$alias."/".$file; @unlink($filename); $mainframe->redirect("index.php?option=com_jshopping&controller=importexport&task=view&ie_id=".$ie_id); } } ?>
Comments
Post a Comment