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

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

<?php
# 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

commented: I like glob too :) +14

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

Member Avatar for diafol

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

http://www.daniweb.com/web-development/php/threads/407500

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.

@ardav
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:

<?php
# 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 :)

Member Avatar for diafol

NO prob :)

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>
	                   </tr>
                <?php
										$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))
  {

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

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

    <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>
</tr>
<?php
										  }
										
										mysql_close($con);
								?> 
</table>

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