0

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

3
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by diafol
0

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

1
$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.

Edited by diafol: n/a

0

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.

0

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

Edited by diafol: n/a

0

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

0

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;

Edited by diafol: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.