Reputation: 179
I'm very new to PHP and would like some help creating a menu from files contained in a directory. As I add files i'd like the menu to automatically add an item.
The files in the directory are .htm files and the naming convention is year, then month separated by an underscore i.e, 2013_6.htm (June 2013)
I would like to be able to read the files and then create a menu from those.
I have managed to create a sorted array of the files, like this (which works fine):
$dir = "$cal_path";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if ($filename != "." && $filename != ".."){
$files[] = $filename;
}
}
sort($files);
How do I now get this array of files into a menu item list that I can then style with CSS, something like this, where xxxx is the year from the filename and yy is the month from the file name.
$cal_menu =
<ul>
<li><a href="$base_url/calendar_view?year=xxxx&month=yy">June 2013</a></li>
<li>etc</li>
<li>etc</li>
</ul>
I created an array of months and corresponding month names
$CalendarMonth = array("1"=>"January","2"=>"February","3"=>"March","4"=>"April","5"=>"May","6"=>"June","7"=>"July","8"=>"August","9"=>"September","10"=>"October","11"=>"November","12"=>"December");
I hope I have explained my problem properly. Many thanks in advance.
Upvotes: 1
Views: 1606
Reputation: 2018
You can try this code:
EDIT: as you want to save that output on a variable.
rsort($files);
$cal_menu = '<ul>';
foreach($files as $file){
preg_match('/(\d{4})_(\d.*).htm/i', $file, $date);
$mont_name = $CalendarMonth[$date[2]];
$cal_menu .= '<li><a href="$base_url/calendar_view?year='.$date[1].'&month='.$date[2].'">'.$mont_name.' '.$date[1].'</a></li>';
}
$cal_menu .= '</ul>';
This code set the output of every li item in the format you required: ?year=xxxx&month=yy
See it working here.
Upvotes: 1
Reputation: 781716
foreach ($files as $file) {
preg_match('/^(\d{4})_(\d{1,2}}/', $file, $match); // Parse filename into YYYY_MM
$year = $match[1];
$month = $match[2];
$monthname = $CalendarMonth[$month];
// Interpolate these into HTML output
echo "<ul>
<li><a href='$base_url/calendar_view?year=$year&month=$month'>$monthname $year</a></li>
<li>etc</li>
</ul>\n";
}
Upvotes: 0
Reputation: 4193
Use a loop in which you extract data from each filename with pathinfo
. You could do this inside your existing loop, but here it is as a foreach
:
echo '<ul>';
foreach($files as $filename) {
$f_info = pathinfo($dir . '/' . $filename);
$extension_length = strlen('.' . $f_info['extension']);
$f_parts = substr($f_info['filename'], 0, -$extension_length);
$year = $f_parts[0];
$month = $f_parts[1];
echo '<li><a href="' . $base_url . '/calendar_view?year=' . $year .
'&month=' . $month . 'yy">' . $CalendarMonth[$month] . ' ' .
$year . '</a></li>';
}
echo '</ul>';
You might want to use array_key_exists
to verify that $month
actually exists in $CalendarMonth
before using $CalendarMonth[$month]
.
Upvotes: 0
Reputation: 300
Try this
foreach ($files as $file) {
// pathinfo will return an array containing
// dirname,basename,extension,filename then use extract
// to convert the array into variables
extract(pathinfo($file));
echo "<li><a href="$file">$filename</a></li>"
}
</ul>
read more about pathinfo http://php.net/manual/en/function.pathinfo.php read more about extract https://www.php.net/manual/en/function.extract.php
Upvotes: 0