Reputation: 165
This script scans the directory 'uploads' and list their subfolders. All subfolders has this structure
YYYY-MM-DD_hh:mm:ss_text
for example
I want to make separate UL TAGS on new day (position 9 and 10). Something like
<ul><li>2013-03-18_23:59:59_cam1</li><li>2013-03-18_09:22:12_cam1</li></ul>
<ul><li>2013-03-17_19:05:02_cam2</li><li>2013-03-17_12:30:28_cam4</li></ul>
I have no idea how to compare position 9 and 10 in a foreach statement and ask for help! Thank you!
Here is my script
<?php
// Name of directory
$directory = "uploads/";
$action=opendir($directory);
while($read=readdir($action)){
$dat_array[] = $read;
}
//sort array reverse
rsort($dat_array);
foreach($dat_array as $read) {
if(!preg_match("!(\.|\..)$!", $read)){
echo '<ul><li><a href="dir.php?id='.$read.'"><span>'.$read.'</span><span></span></a></li></ul>';
}
}
?>
Upvotes: 3
Views: 171
Reputation: 2615
<?php
// Name of directory
$directory = "uploads/";
$action=opendir($directory);
while($read=readdir($action)){
$dat_array[] = $read;
}
//sort array reverse
rsort($dat_array);
foreach($dat_array as $read) {
$date1=explode("_",$read);
if($date2!=$date1)
{
echo"<ul>";
}
echo '<li><a href="dir.php?id='.$read.'"><span>'.$read.'</span><span></span></a></li>';
if($date2!=$date1)
{
echo"</ul>";
}
$date2=$date1;
}
?>
Upvotes: 0
Reputation: 9130
Try:
<?php
// same as a glob() on a dir, just for testing
$data = array('2013-03-18_23:59:59_cam1',
'2013-03-18_09:22:12_cam1',
'2013-03-17_19:05:02_cam2',
'2013-03-17_12:30:28_cam4');
$tmp = '';
foreach($data as $k => $v) {
$day = substr($v,0,10);
if ($day != $tmp) {
$HTML .= '</ul><i>'.$day.'</i><ul>';
}
$HTML .= '<li>'.$v.'</li>';
$tmp = $day;
}
// add a closing tag to the end
$HTML .= '</ul>';
// NOTE: must remove inital closing tag
echo '<p>Daily lisitng:</p>'.substr($HTML,5);
?>
The result is:
Daily lisitng:
2013-03-18
2013-03-17
Upvotes: 0
Reputation: 32750
Try this code :
// Name of directory
$dat_array = array();
$directory = "uploads/";
$action = opendir($directory);
while($read = readdir($action)){
$exp = explode("_",$read);
$dat_array[$exp[0]][] = $read;
}
rsort($dat_array);
foreach($dat_array as $val){
echo "<ul>";
foreach($val as $v){
echo "<li>".$v."</li>";
}
echo "</ul>";
}
Upvotes: 1