sw9 Posted July 23, 2009 Share Posted July 23, 2009 Hi there, I'm pulling in a csv file and looping through the data to insert it into a db. The problem is that some of my rows have empty values in the last couple columns. Those rows are taking the filled in values from the respective columns in the rows above them when instead i want them to retain their empty values. I was trying to reset my array with unset($myarray) and $myarray = array() but neither are doing the trick. unset is giving me a PHP error about array_combine, and setting the array doesn't seem to do anything. Here's the code I have: <?php $fields = array('title', 'episode_number', 'episode_name', 'secondary_name', 'series_id', 'category', 'subcategory', 'first_run', 'active', 'length', 'notes', 'ep', 'source','pdate', 'adate', 'purchase_date', 'exp_date', 'distributor', 'msn', 'msn_description', 'msn_program_type', 'msn_status', 'msn_filename', 'series_name'); $handle = fopen("vlm.csv", "r"); while (($data = fgetcsv($handle, 10000, " ")) !== FALSE) { $data = array_combine($fields, $data); // Do all the insertion in here... $data = array(); // here I am trying to reset it. but it doesn't work.... } // end in file fclose($handle); ?> any help?? much appreciated! Quote Link to comment Share on other sites More sharing options...
9three Posted July 23, 2009 Share Posted July 23, 2009 <?php $fields = array('title', 'episode_number', 'episode_name', 'secondary_name', 'series_id', 'category', 'subcategory', 'first_run', 'active', 'length', 'notes', 'ep', 'source','pdate', 'adate', 'purchase_date', 'exp_date', 'distributor', 'msn', 'msn_description', 'msn_program_type', 'msn_status', 'msn_filename', 'series_name'); $handle = fopen("vlm.csv", "r"); while (($data = fgetcsv($handle, 10000, " ")) !== FALSE) { foreach($data as $value) { if ($value == null) { $value = ' '; } } $insert = array_combine($fields, $value); print_r($insert); } // end in file fclose($handle); ?> Quote Link to comment Share on other sites More sharing options...
sw9 Posted July 24, 2009 Author Share Posted July 24, 2009 thanks for the help....but this doesn't actually work. if i use the code you give, all my values are set empty for some reason and nothing happens. so then i tried this: while (($data = fgetcsv($handle, 10000, " ")) !== FALSE) { foreach($data as $value) { if ($value == null) { $value = ' '; } } $data = array_combine($fields, $data); but that doesn't work either. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 24, 2009 Share Posted July 24, 2009 try this: while (($data = fgetcsv($handle, 10000, " ")) !== FALSE) { $data = array_combine($fields, array_pad($data,count($fields),'')); Quote Link to comment Share on other sites More sharing options...
sw9 Posted July 24, 2009 Author Share Posted July 24, 2009 thanks rhodesa...again it's still not working. no idea what the problem is here. when i print_r the array i can see that the values are empty, yet they are still inserting into the db with the previous value. so here is the code i used that you gave me (it's actually a txt file): <?php $fields = array('title', 'episode_number', 'episode_name', 'secondary_name', 'series_id', 'series_name', 'category', 'subcategory', 'first_run', 'active', 'length', 'notes', 'ep', 'source','pdate', 'adate', 'purchase_date', 'exp_date', 'distributor', 'msn', 'msn_description', 'msn_program_type', 'msn_status', 'msn_filename'); $handle = fopen("vlm.txt", "r"); while (($data = fgetcsv($handle, 10000, " ")) !== FALSE) { $data = array_combine($fields, array_pad($data,count($fields),'')); print_r($data); // and then i would insert the data into my db using an api that looks something like: $node->series_name = $data["series_name"]; // here is where the previous value gets inserted if it's currently empty } ?> and here are 2 sample lines from my file export: 21323 101 Intro to ISD 159 Inside Seven Days Humanities and Language 20080918 1 0:09:29 Seven Days Online Editor Cathy Resmer gives viewers an introduction to INSIDE SEVEN DAYS, a weekly show in which various writers and critics discuss their stories and talk about what did and did not make it into print. Watch INSIDE SEVEN DAYS Thursdays at 8 pm. Cathy Resmer Seven Days 20080918 00000000 00000000 00000000 22034 Intro to ISD DVD-R 4.7 gig Disc Available 200035 0 Spotlight Series 0 DNS 00000000 1 0:01:00 00000000 00000000 00000000 00000000 200094 Spotlight Series 1 Video Clip Digital File Available \\Peg-i-stor-le\G Peg-i-Stor-le Storage\RETN\MZOD\First Contacts Clip Trimmed.mpeg in this example, the 5th column 'Inside Seven Days' gets inserted as $data["series_name"] for the second entry, which has an empty column. When i print_r($data) I do in fact see empty value there, so I don't understand why it's still inserting it into the db. Array ( [title] => 200035 [episode_number] => 0 [episode_name] => Spotlight Series [secondary_name] => [series_id] => 0 [series_name] => [category] => DNS [subcategory] => [first_run] => 00000000 [active] => 1 [length] => 0:01:00 [notes] => [ep] => [source] => [pdate] => 00000000 [adate] => 00000000 [purchase_date] => 00000000 [exp_date] => 00000000 [distributor] => [msn] => 200094 [msn_description] => Spotlight Series 1 Video Clip [msn_program_type] => Digital File [msn_status] => Available [msn_filename] => \\Peg-i-stor-le\G Peg-i-Stor-le Storage\RETN\MZOD\First Contacts Clip Trimmed.mpeg ) ideas on what the issue is? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 24, 2009 Share Posted July 24, 2009 OH...you need to reset the values in $node i bet. can you post the rest of the insert code? Quote Link to comment Share on other sites More sharing options...
sw9 Posted July 24, 2009 Author Share Posted July 24, 2009 OH is right. Now I am just hitting myself. As soon as you said that I realized my stupid error. I had this code: <?php $sSeries = "SELECT nid FROM {content_type_series} WHERE field_synergy_id_value = '".$data["series_id"]."'"; $seriesResult = db_query($sSeries); while ($seriesData = mysql_fetch_assoc($seriesResult)) { $series = $seriesData["nid"]; } ?> and I wasn't resetting the $series var, so it was retaining the previous value if empty. DUH. So obviously putting $series = ''; at the front fixes it. Thanks for the wake up call; sorry for the hassle! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.