Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I cannot take credit for this solution. I was answered on another site by an 'expert.' However, I really feel that this topic is important enough to repost this solution on this 'stack' site.</p> <p><strong>SOLUTION:</strong></p> <pre><code>&lt;?php define('TAB', "\t"); define('TAB2', "\t\t"); // Open the input file $file = fopen('lines.csv', 'rt'); // ignore the first line fgets($file); // Get the next line into an array splitting on the pipe $line = fgetcsv($file, 255, '|'); // set the initial section value $section = $line[0]; // Initialise batch array $current = array(); // Loop through all lines in the file do { // If this line contains a section that is not the same as the previous line then dump the last section if ($section != $line[0]) { dump_categories($current, $section); // Set our section flag to the next section $section = $line[0]; // Clear the array of batched items to dump $current = array(); } // Add the line to the batch $current[] = $line; } while($line = fgetcsv($file, 255, '|')); // Remember to dump batch that has built up dump_categories($current, $section); fclose($file); function dump_categories(&amp; $lines, $section) { // Set our category flag $category = $lines[0][1]; // initialise batch array $current = array(); // Start a new seection echo '&lt;ul class="section"&gt;' . PHP_EOL; echo TAB . '&lt;li&gt;' . $section . PHP_EOL; // If there are categories in this section then process them - don't want empty bullets here if (!empty($lines[0][1])) { foreach($lines as $l) { // if this is a new category then dump category and services for this category if ($category != $l[1]) { dump_services($current, $category); // set our category flag $category = $l[1]; // ... and clear the batch array $current = array(); } // add to batch $current[] = $l; } // and dump batched lines dump_services($current, $category); } echo TAB . '&lt;/li&gt;' . PHP_EOL; echo '&lt;/ul&gt;'; } // Services we assume is the end of the line so output the service if it exists. function dump_services(&amp; $lines, $category) { echo TAB2 . '&lt;ul class="category"&gt;' . PHP_EOL; echo TAB . TAB2 . '&lt;li&gt;' . $category . PHP_EOL; if (!empty($lines[0][2])) { echo TAB2 . TAB2 . '&lt;ul class="service"&gt;' . PHP_EOL; foreach($lines as $l) { if (!empty($l)) { echo TAB . TAB2 . TAB2 . '&lt;li&gt;' . $l[2] . '&lt;/li&gt;' . PHP_EOL; } } echo TAB2 . TAB2 . '&lt;/ul&gt;' . PHP_EOL; } echo TAB . TAB2 . '&lt;/li&gt;' . PHP_EOL; echo TAB2 . '&lt;/ul&gt;' . PHP_EOL; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload