Hi, i run http://www.allforums.co.uk and we have a tutorials section but we want to display how many tutorials we have in each section
how could we count the number or tutorials and display them automaticly
we dont really want to have to change the number evertime we update
Many thanks
Ben
I need a tutorial counter
Moderators: egami, macek, gesf
- ruturajv
- php-forum Super User
- Posts: 1279
- Joined: Sat Mar 22, 2003 9:42 am
- Location: Mumbai, India
- Contact:
if you are storing the tutorials, then it is very easy just use
mysql_num_rows($result_of_sql_query);
and you have the no. of tutorials
If you have it in afile manner,
load the file names into a array and then
count($array_name);
mysql_num_rows($result_of_sql_query);
and you have the no. of tutorials
If you have it in afile manner,
load the file names into a array and then
count($array_name);
thanks you but im not using a mysql database so the first one wont work but on the second one if i mad an array wouldn't it just be easier to update the number on the page rather than doing the array counter?
Many Thanks
Ben
Many Thanks
Ben
Have you tried the directory functions?
You could count the number of files in the directory that look like tutorials. or if each tutorial is in a directory count the number of directories.
or have one text file tut_num.txt with one number in it, the counter
and everytime a tutorial is added, you or the script could update the counter.
You could count the number of files in the directory that look like tutorials. or if each tutorial is in a directory count the number of directories.
or have one text file tut_num.txt with one number in it, the counter
and everytime a tutorial is added, you or the script could update the counter.
Thanks, i havn't tried a directory counter but it sounds like what i want
How do i use a directory counter?
N.B my tutorials are not yet uploaded so they can go in any folder etc. whatever is needed for this to work
Many Thanks
Ben
How do i use a directory counter?
N.B my tutorials are not yet uploaded so they can go in any folder etc. whatever is needed for this to work
Many Thanks
Ben
here is the basic concept of how you count things in a directory. For a final product you may want a more complex filter, and preferably a way to store your result so this script does not have to be run every time someone loads a page you want this result for. if you are using php 5, it comes with a scandir() function you may want to look into using.
Code: Select all
<?php
# start your counter
$number_of_directories = 0;
# open up the directory you want to look in
$dir = opendir('/home/directory_of_tutorials');
# while there are any more items in the directory
while ( ($item = readdir($dir)) !== false ) {
# if the item meets our criteria
# if the item is not one of the first two: '.' or '..' and its a directory
if ( ($item != '.') && ($item != '..') && is_dir($item) ) {
# count it as a directory
$number_of_directories++;
}
}
# go ahead and close your directory resource
closedir($dir);
?>
So would this code work??
So what do i do where it says
1)# start your counter
$number_of_directories = 0;
2) # while there are any more items in the directory
while ( ($item = readdir($dir)) !== false ) {
# if the item meets our criteria
# if the item is not one of the first two: '.' or '..' and its a directory
if ( ($item != '.') && ($item != '..') && is_dir($item) ) {
# count it as a directory
$number_of_directories++;
}
}
Many Thanks
Ben
Code: Select all
<?php
# start your counter
$number_of_directories = 0;
# open up the directory you want to look in
$dir = opendir('/tutorials/photoshop/files');
# while there are any more items in the directory
while ( ($item = readdir($dir)) !== false ) {
# if the item meets our criteria
# if the item is not one of the first two: '.' or '..' and its a directory
if ( ($item != '.') && ($item != '..') && is_dir($item) ) {
# count it as a directory
$number_of_directories++;
}
}
# go ahead and close your directory resource
closedir($dir);
?>
So what do i do where it says
1)# start your counter
$number_of_directories = 0;
2) # while there are any more items in the directory
while ( ($item = readdir($dir)) !== false ) {
# if the item meets our criteria
# if the item is not one of the first two: '.' or '..' and its a directory
if ( ($item != '.') && ($item != '..') && is_dir($item) ) {
# count it as a directory
$number_of_directories++;
}
}
Many Thanks
Ben
every line with # simply describes the line of code below
make a php file with this in it
make a php file with this in it
Code: Select all
<?php
# start your counter
$number_of_directories = 0;
# open up the directory you want to look in
$dir = opendir('./');
# while there are any more items in the directory
while ( ($item = readdir($dir)) !== false ) {
# if the item meets our criteria
# if the item is not one of the first two: '.' or '..' and its a directory
if ( ($item != '.') && ($item != '..') && is_dir($item) ) {
# count it as a directory
$number_of_directories++;
}
}
print "<br />The number of sub-directories in the same directory as this file is '$number_of_directories'<br />";
# go ahead and close your directory resource
closedir($dir);
?>
Are you basicly saying that all i have to do is make a file like that and include it where i want the counter?
well, that is one solution... however the important part is the logic...
if you understand the idea of what exactly you want... and how you go about doing it... then you can worry about what is a convenient method of executing it...
i would make it into a function... and for each file that wants to us it... simply include() the file your function is in and call the function
does that make sense or are you too new to programming?
if you understand the idea of what exactly you want... and how you go about doing it... then you can worry about what is a convenient method of executing it...
i would make it into a function... and for each file that wants to us it... simply include() the file your function is in and call the function
does that make sense or are you too new to programming?