Hi everyone:
I am new with PHP, though I can do simple coding in PHP.
I would like to create category section in the main page, as in some website you see as follows:
Category-1 (20)
Category-2 (13)
Category-3 (5)

1- I want to use the similar technique, counting files in a specific folder and displays it in specific categories.
2- When clicked the category, it should list you all the title in that category as link.
I hope there would be any easy way and i really appreciate it. thanks
Sam

Recommended Answers

All 8 Replies

I think you are needing tree menu.. Just google it, "tree menu in JS "
To count the folder items use 'readdir();' or 'opendir();'

Thanks Asif, i will check it out.

Member Avatar for diafol
$maindir = "./"; //change this to the path of your choice - ensure that a '/' is at the end
$len = strlen($maindir);
$dirs = glob($maindir . '*' , GLOB_ONLYDIR);
foreach($dirs as $dir){
  echo substr($dir,$len) . ": ". count(glob($dir . '/*.*')) . "<br />";
}

You may find a SPL DirectoryIterator better, but I have a soft spot for glob!
I've assumed that you category names match the subdirectories. If this is not the case - post back with your structure and naming conventions. It should be a trivial matter to get it working.

Thanks Ardav for your post. This works in terms of counting the files in directory.
However, what i want is a code to count the files and display it as link, when you click on it then it should list files in that category / folder, and could be listed by title name. when you click on any links this should open the page. now i hope it make sense.
babsically, i am looking for code to creat a joke site, wherein i could display jokes in different categories and cound how many jokes in that category. plus you should be able to click on that category where the jokes would be listed as links. hope this explanition helps. once again thanks for the reply.

Hi ardav.. Nice Code.. Its useful to me also. Thank You

Member Avatar for diafol

Do this for the loop:

$output = "";
foreach($dirs as $dir){
  $file_r = glob($dir . '/*.*');
  $count = count($file_r);
  if(count > 0){
    $output .= "\n<h3>" .substr($dir,$len) . ": $count</h3>\n<ul class=\"toggled\">";
    foreach($file_r as $file){
      $output .= "\n\t<li>$file</li>";
    }
    $output .= "</ul>";
  }  
}
echo $output;

Not tested - user the .toggled class for a javascript show/hide

got error:
Warning: Invalid argument supplied for foreach()

Member Avatar for diafol

Change to this:

if($count > 0){

I forgot the '$'.
Works for me.

$output = "";
$maindir = "./"; //change this to the path of your choice - ensure that a '/' is at the end
$len = strlen($maindir);
$dirs = glob($maindir . '*' , GLOB_ONLYDIR);
foreach($dirs as $dir){
  $file_r = glob($dir . '/*.*');
  $count = count($file_r);
  if($count > 0){
    $output .= "\n<h3>" .substr($dir,$len) . ": $count</h3>\n<ul class=\"toggled\">";
    foreach($file_r as $file){
      $output .= "\n\t<li>$file</li>";
    }
    $output .= "</ul>";
  }  
}
echo $output;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.