I am using PHP to keep my webpage organized.
The directory looks like this:
root/folder/file1
root/folder/file2
root/folder/file3
root/index.php
Each file is very simple html doc with content containing a title tag, which I use to create a nav bar.
I want index.php to aggregate all the content from file1, file2 and file3, in addition to creating a nav bar up at the top.
The code I have achieves this successfully, but I'm not sure if I'm doing this in a practical way.
$domain = "domain/"
$dir = "folder/";
$files = scandir($dir);
// loop over all the files, saving their contents and ids to arrays
$contents = Array();
$titles = Array();
$ids = Array();
for ($x = 0; $x < sizeof($files); $x++) {
if ($files[$x] != "." && $files[$x] != "..") {
$filename = $domain . $dir . $files[$x];
$file = fopen($filename, "r");
$data = file_get_contents($filename);
array_push($contents, $data);
$regex = '#<title>(.*?)<\/title>#';
preg_match($regex, $data, $match);
$title = $match[1];
array_push($titles, $title);
$regex2 = '#( |-|,)#';
$id = preg_split($regex2, $title)[0];
array_push($ids, $id);
}
}
// the first loop sets the nav bar
echo "<div id='navigation'>";
echo "<ul>";
for ($x = 0; $x < sizeof($contents); $x++) {
echo "<li><a href='#" . $ids[$x] . "'>" . $titles[$x] . "</a></li>";
}
echo "</ul>";
echo "</div>";
// this second loop sets the contents
echo "<div id='content'>";
for ($x = 0; $x < sizeof($contents); $x++) {
echo "<div id='" . $ids[$x] . "'>" . $contents[$x] . "</div>";
}
echo "</div>";
I like the idea of having all the content on one big page and the nav bar helps a lot when I'm viewing it on my phone.
Questions
- Are there any obvious problems with the way I am aggregating my files?
- Are there challenges I may encounter that I have not yet experienced?
- Is there a generally accepted way of doing this while achieving the same result?