Jump to content

parsing a hierarchy csv file


diasansley

Recommended Posts

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

Link to comment
Share on other sites

      $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;

                            }

                        }

                       

                    }

                   

                }

            }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.