Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Create UL Nested Lists From Flatfile
    text
    copied!<p>(<em>Before anyone asks... I cannot use a MySQL database for this project.)</em></p> <p>Using PHP, I need to create nested UL's from a flatfile database. The issue that I'm having is that I don't want duplicate items displayed. I could explain further, but if you'll take a look at the code below, you'll see the data and the goal. Thanks. <br /><br /> <strong>FLATFILE DATA:</strong></p> <pre> section|category|service Section One|Category One| Section One|Category Two|SC1 Section One|Category Two|SC2 Section One|Category Two|SC3 Section One|Category Two|SC4 Section One|Category Three| Section Two|Category Four|SC5 Section Two|Category Four|SC6 Section Three|Category Five|SC7 </pre> <p><br /> <strong>HTML GOAL OUTPUT:</strong> <br /></p> <pre><code>&lt;ul class="section"&gt; &lt;li&gt;Section One &lt;ul class="category"&gt; &lt;li&gt;Category One&lt;/li&gt; &lt;!-- no service --&gt; &lt;/ul&gt; &lt;!-- /category --&gt; &lt;ul class="category"&gt; &lt;li&gt;Category Two&lt;/li&gt; &lt;ul class="service"&gt; &lt;li&gt;SC1&lt;/li&gt; &lt;li&gt;SC2&lt;/li&gt; &lt;li&gt;SC3&lt;/li&gt; &lt;li&gt;SC4&lt;/li&gt; &lt;/ul&gt; &lt;!-- /service --&gt; &lt;/li&gt; &lt;/ul&gt; &lt;!-- /category --&gt; &lt;ul class="category"&gt; &lt;li&gt;Category Three&lt;/li&gt; &lt;!-- no service --&gt; &lt;/ul&gt; &lt;!-- /category --&gt; &lt;/li&gt; &lt;/ul&gt; &lt;!-- /section --&gt; &lt;ul class="section"&gt; &lt;li&gt;Section Two &lt;ul class="category"&gt; &lt;li&gt;Category Four&lt;/li&gt; &lt;ul class="service"&gt; &lt;li&gt;SC5&lt;/li&gt; &lt;li&gt;SC6&lt;/li&gt; &lt;/ul&gt; &lt;!-- /service --&gt; &lt;/li&gt; &lt;/ul&gt; &lt;!-- /category --&gt; &lt;/li&gt; &lt;/ul&gt; &lt;!-- /section --&gt; &lt;ul class="section"&gt; &lt;li&gt;Section Three &lt;ul class="category"&gt; &lt;li&gt;Category Five&lt;/li&gt; &lt;ul class="service"&gt; &lt;li&gt;SC7&lt;/li&gt; &lt;/ul&gt; &lt;!-- /service --&gt; &lt;/li&gt; &lt;/ul&gt; &lt;!-- /category --&gt; &lt;/li&gt; &lt;/ul&gt; &lt;!-- /section --&gt; </code></pre> <p>My 1st attempt... thinking that I need to first check if the "section" exists, then assign the current "section" to a "section_last" to compare the next "section" with the current "section" value ... and if not, print the "section" ... and then onto the category. <em>I didn't code the "service" value section because I wasn't haven't any success with the section &amp; category values. It seems that I'm having an issue with either the 'logic' behind this or maybe there's a method for comparing the past value in the loop with the next value that PHP offers that I'm missing.</em></p> <p>After seeing the example code from @Kyle S, I may be on a completely incorrect path for coding this.</p> <pre><code>&lt;?php $section = ''; $category = ''; $service = ''; $section_last = ''; $category_last = ''; $service_last = ''; $x = 0; $file = fopen("categories_data.txt", "r"); if (!$file) { echo 'ERROR: Unable to open file: &lt;strong&gt;'.$file.'&lt;/strong&gt;'; } fgets($file); // IGNORE FIRST LINE IN FLATFILE - column names while (!feof($file) ) { $lines = fgets($file); $ele = explode('|', $lines); $section = $ele[0]; $category = $ele[1]; $service = $ele[2]; $service = str_replace(array("\n", "\r", "\r\n", "\n\r"), '',$service); if(strlen($section)&gt;0) { if(!($section == $section_last)) { echo ' &lt;ul&gt; &lt;li&gt;'.$section; $section_last = $section; $x++; } } echo ' &lt;ul&gt;&lt;li&gt;'.$category.' &gt; '.$service.'&lt;/li&gt;&lt;/ul&gt; '; if($x) { if($section != $section_last) { echo ' &lt;/li&gt; &lt;/ul&gt; '; } } } // end $data_file WHILE fclose($file); ?&gt; </code></pre>
 

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