Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function importproduct() {
- // El archivo csv debe estar en la ruta /app/webroot/csv/nombre.csv
- $filename = WWW_ROOT . DS . 'csv' . DS . 'nombre.csv';
- // open the file
- $handle = fopen($filename, "r");
- // read the 1st row as headings
- $header = fgetcsv($handle);
- // create a message container
- $return = array(
- 'messages' => array(),
- 'errors' => array(),
- );
- $i = 0;
- $records = 0;
- $data = array();
- // read each data row in the file
- while (($row = fgetcsv($handle)) !== FALSE) {
- // for each header field
- foreach ($header as $k=>$head) {
- // get the data field from Model.field
- if (strpos($head,'.')!==false) {
- $h = explode('.',$head);
- $data[$h[0]][$h[1]]=(isset($row[$k])) ? $row[$k] : '';
- }
- // get the data field from field
- else {
- $data[$i]['Product'][$head]=(isset($row[$k])) ? $row[$k] : '';
- $temp['Product'][$head]=(isset($row[$k])) ? $row[$k] : '';
- if ($head == 'category') {
- $temp['Product']['category_id'] = $this->strToUrl($row[$k]);
- }
- }
- }
- if ($this->Product->save($temp)) {
- $records++;
- }
- $i++;
- }
- echo "Fueron grabados ".$records." productos";
- debug($data);
- // close the file
- fclose($handle);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement