I'm trying to parse csv

and choto gag

try { $handle = fopen($test, "r"); while (fgetcsv($handle) !== false) { $data = fgetcsv($handle); if ($data !== false) { $output->writeln(sprintf('Updating table price_selected_hotel...')); $selectedHotels = new PriceSelectedHotels; foreach ($data as $row) { $output->writeln($row); $selectedHotels->setCityCode($row['']); $selectedHotels->setCheckInDate($row['']); $selectedHotels->setSelectedHotels($row['']); $selectedHotels->setInsertTime(date("Ymd H:i:s")); $em->flush(); $em->clear(); } $output->writeln(sprintf('Updating success')); } } return 0; } catch (\Exception $e) { $output->writeln($e->getMessage(), $e->getCode()); return 1; } 

the file looks like this

check_in_date; city; selected


2016-04-01; LD6J; CA11SM, CAN861, UK02W7, UK0NGW


2016-04-02; LD6J; UK0G54, CAN861, UK0PA1

separator; column selected is an array

  • And the plug looks like? What's wrong? Doesn't understand, understands not how it was expected or difficulties with placement in the database? - cheops
  • 1 I can’t understand how to get a line and parse it normally `$ selectedHotels-> setCityCode ($ row ['']);` $selectedHotels->setCheckInDate($row['']); $selectedHotels->setSelectedHotels($row['']); those. what to write in $row[' ** СЮДА ** '] - Nikita Rassamahin

1 answer 1

If I have not forgotten how to understand csv , then try this:

  try { $handle = fopen($test, "r"); while (($row = fgetcsv($handle, 1000, ';')) !== false) { $output->writeln(sprintf('Updating table price_selected_hotel...')); $selectedHotels = new PriceSelectedHotels; $output->writeln($row); list($date, $city, $hotels) = $row; $selectedHotels->setCityCode($city); $selectedHotels->setCheckInDate($date); $selectedHotels->setSelectedHotels( explode(',', $hotels) ); $selectedHotels->setInsertTime(date("Ymd H:i:s")); $em->flush(); $em->clear(); $output->writeln(sprintf('Updating success')); } return 0; } catch (\Exception $e) { $output->writeln($e->getMessage(), $e->getCode()); return 1; } 

This is where $selectedHotels->setSelectedHotels( imlode(',', $hotels) ); going to an array of your hotels. If your function should take a string and not an array, then explode not needed.

Offtop
I could be wrong, but I don't see anything in the try block that could return an Exception , in other words, your try catch useless here. Correct if I am mistaken.

  • Fail not found for example =) - Nikita Rassamahin
  • So, parse the distance, but does not write to the database: s - Nikita Rassamahin
  • @NikitaRassamakhin: fopen just returns false to your $handle . There should be no exceptions. - user200141
  • @NikitaRassamahin: If you don’t write to the database, then dig into the methods of your PriceSelectedHotels class. I think there is something wrong. - user200141
  • No, I just forgot the persist) but the question is, this is NULL NULL a: 1: {i: 0; s: 0: "";} 2016-03-22 - Nikita Rassamahin