And if the case is that

1. having only 1 directory lets suppose Images

2. Added category and categorized images category wise...

3. Now want to count the images according to their category then what will be the code for this by counting images from 1 directory but for different categories... plz any one help....

Edited by emannoor455: sample is here

Attachments 404312_2455550838369_1540035750_1875971_1780826363_n.jpg 11.19 KB
6 Years
Discussion Span
Last Post by emannoor455

Are you using a database or is just a directory?
If only a directory (images) and no database registration you can use glob():

# GLOB_NOSORT flag will help to go a bit faster
$a = glob('a_*.jpg',GLOB_NOSORT);
$b = glob('b_*.jpg',GLOB_NOSORT);

echo count($a) . " " . count($b);

This can work by using a prefix for each category (like a_ and b_), but it can become slow if you start to have thousands of images. To limit reads on filesystem you can create a text file where you store a bidimensional array with categories and filenames, you can also store this information to memory with memcached. It's up to your needings.

edit: you can also consider scandir()* which is faster than glob() but available only with PHP5.

* http://php.net/manual/en/function.scandir.php

Edited by cereal: n/a

Votes + Comments
I like glob too :)

m using database also.

1. I have added categories in the database.

2. then stored the images against categories where images goes in Images directory and name stored in the database.

3. Now i want that if i add new category then its image count done automatically... glob function fetch all images of the Image directory and if i separately give the name of categories each time then its quite hectic :( plz help m new in php


I answered something similar yesterday, before you posted you thread, which set me thinking, 'coincidence' or 'homework assignment'?


You don't need to use glob at all if you have all the info in the DB. Just query the DB. You should have shared the info at the start.


Sorry ^^! I opened the thread and went away.. didn't see you answer.. bye ;D

This should work, but maybe someone else can provide a better solution:

# database connection ...

$q = mysql_query("select category, count(category) as cat_total from images group by category");

while($row = mysql_fetch_object($q))
    echo "$row->category $row->cat_total";

bye :)

Edited by cereal: n/a


Sir its not working on my side i have checked it here is my code

<div class="entry">
							<table width="555" border="1">
	                   <tr><th width="188" align="left"><span class="style3">Category Name</span></th><th width="214" align="left"><span class="style3">Images count</span></th><th width="131" align="left"><span class="style3">Options</span></th>
										$con = mysql_connect("localhost","root","");
										if (!$con)
										  die('Could not connect: ' . mysql_error());
										mysql_select_db("my_image_gallery", $con);
									  $result = mysql_query("SELECT * FROM `gallery_category`");
										while($row = mysql_fetch_array($result))

<td><?php echo  $row['category_name']?></td>

    $maindir = "/Images/"; //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 />";

    <td><a href="EditCategory.php?cid=<?php echo $row['category_id'];?>" title="Edit Gallery" target="_self">Edit</a> &nbsp; | &nbsp;<a href="DeleteGallery.php?action=delete&id=<?php echo $row['category_id']; ?>">Delete | View </a></td>

							<p class="links">&nbsp;</p>
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.