Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
csv_to_html_tree.php
1 <?php
56 function printUsage()
57 {
58  echo "CSV to HTML structure tree converter\n\n";
59  echo "Usage: csv_to_html_tree [csv file]\n";
60  echo "csv file: A comma separated values file that represents the site structure\n\n";
61 
62 }//end printUsage()
63 
64 
65 /************************** MAIN PROGRAM ****************************/
66 
67 if ((php_sapi_name() != 'cli')) {
68  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
69 }
70 
71 // Has a filename been supplied?
72 $csv_filename = $argv[1];
73 if (empty($csv_filename)) {
74  printUsage();
75  echo "* A CSV filename must be specified as the first parameter\n\n";
76  return -1;
77 }
78 
79 // Does the supplied file exist?
80 $fd = fopen($csv_filename, 'r');
81 if (!$fd) {
82  printUsage();
83  echo "* The supplied CSV file was not found\n\n";
84  return -2;
85 }
86 
87 // Yippee, we have a file. Let's process it now
88 $last_html_tree = Array();
89 
90 while (($data = fgetcsv($fd, 1024, ',')) !== FALSE) {
91  // The level we have stored already (eg; heading 1 = 1)
92  $current_level = 0;
93  $num_fields = count($data);
94 
95  // Grab a line and throw it into an HTML array
96  $html_tree = Array();
97  foreach ($data as $key => $val) {
98  $current_level++;
99  $html_tree[$current_level] = $val;
100  }
101 
102  // Cycle through the line, omitting any levels we have already covered
103  foreach ($html_tree as $level => $page_name) {
104  $current_page = $html_tree[$level];
105 
106  if (!empty($page_name)) {
107  if ($page_name != $last_html_tree[$level]) {
108  // Write out the heading tag, trimming any spaces surrounding the page name
109  echo '<h'.$level.'>'.trim($page_name).'</h'.$level.">\n";
110  }
111  }
112 
113  }
114 
115  // Remember what we saw for the next line
116  $last_html_tree = $html_tree;
117 }
118 fclose($fd);
119 
120 
121 // That's all folks :-D
122 
123 ?>