diasansley Posted October 11, 2011 Share Posted October 11, 2011 having a problem importing a file having multiple hierarchy levels!! its a csv file the contents look something like this! just need a jump start! thanks the file looks likes this row 1 row2 row3 name1 subj1 abcd efgh subje2 test1 test2 the file has to first import name1 then subj1 then abcd,then efgh then move back to subj2 and so on! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/248893-parsing-a-hierarchy-csv-file/ Share on other sites More sharing options...
silkfire Posted October 11, 2011 Share Posted October 11, 2011 If you read it row by row, column by column then the data will appear in the order you specified. Show us your attempt to code. Quote Link to comment https://forums.phpfreaks.com/topic/248893-parsing-a-hierarchy-csv-file/#findComment-1278234 Share on other sites More sharing options...
diasansley Posted October 11, 2011 Author Share Posted October 11, 2011 $csv_file = './uploads/file_import.csv'; $csvData = $this->csvreader->parse_file($csv_file); $count = 0; $i = 0; $jj = 0; foreach ($csvData as $row1) { if ($row1[1] != "") { if ($row1[2] != "") { foreach ($csvData as $row) { if ($row[3] != "") { echo $row[3]; } else { break; } } } } } Quote Link to comment https://forums.phpfreaks.com/topic/248893-parsing-a-hierarchy-csv-file/#findComment-1278235 Share on other sites More sharing options...
Psycho Posted October 11, 2011 Share Posted October 11, 2011 The data you showed in your first post is not the data from a CSV file - it looks more like tabular/positional data. Either that, or you modified the data for your post. It is hard to help you when you are not showing the actual data you are working with. Also, since you are using a custom class to read the data it is quite possible the problem is with that code. But, if you have problems such as this, the first step is to validate the data - which is one debugging strategy. Are you getting any errors with your code? Did you validate that an array was returned? Etc., etc. Quote Link to comment https://forums.phpfreaks.com/topic/248893-parsing-a-hierarchy-csv-file/#findComment-1278267 Share on other sites More sharing options...
Psycho Posted October 11, 2011 Share Posted October 11, 2011 Using your example above, here is what the data would look like in a valid CSV format name1,subj1,abcd , ,efgh ,subje2,test1 ,,test2 Here is some sample code that would get all the "valid" values from that file $csv_file = "data.csv"; $handle = fopen($csv_file, "r"); if ($handle === FALSE) { //Unable to read file echo "Unable to open file"; } else { //Create result array to put valid values $results = array(); //Process the data line by line while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { //Trim the values and remove empty $data = array_filter(array_map('trim', $data)); //Add to result array $results = array_merge($results, $data); } //Close the file handler fclose($handle); } //The result array now holds just the valid values foreach($results as $value) { echo "$value<br>\n"; } Output name1 subj1 abcd efgh subje2 test1 test2 Quote Link to comment https://forums.phpfreaks.com/topic/248893-parsing-a-hierarchy-csv-file/#findComment-1278271 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.