Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

I need a tutorial counter

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

I need a tutorial counter

Postby Allforums » Wed Sep 17, 2003 10:07 am

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
Allforums
New php-forum User
New php-forum User
 
Posts: 14
Joined: Wed Sep 17, 2003 10:01 am

row count

Postby ruturajv » Wed Sep 17, 2003 8:58 pm

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);
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India

Postby Allforums » Thu Sep 18, 2003 9:21 am

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
Allforums
New php-forum User
New php-forum User
 
Posts: 14
Joined: Wed Sep 17, 2003 10:01 am

Postby ednark » Sun Sep 21, 2003 7:11 am

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.
ednark
New php-forum User
New php-forum User
 
Posts: 18
Joined: Fri Oct 04, 2002 8:09 pm

Postby Allforums » Sun Sep 21, 2003 8:31 am

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
Allforums
New php-forum User
New php-forum User
 
Posts: 14
Joined: Wed Sep 17, 2003 10:01 am

Postby ednark » Sun Sep 21, 2003 7:49 pm

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);

?>
ednark
New php-forum User
New php-forum User
 
Posts: 18
Joined: Fri Oct 04, 2002 8:09 pm

Postby Allforums » Sun Sep 21, 2003 10:48 pm

So would this code work??

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
Allforums
New php-forum User
New php-forum User
 
Posts: 14
Joined: Wed Sep 17, 2003 10:01 am

Postby ednark » Tue Sep 23, 2003 5:49 pm

every line with # simply describes the line of code below

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);

?>
ednark
New php-forum User
New php-forum User
 
Posts: 18
Joined: Fri Oct 04, 2002 8:09 pm

Postby Allforums » Tue Sep 23, 2003 10:45 pm

Are you basicly saying that all i have to do is make a file like that and include it where i want the counter?
Allforums
New php-forum User
New php-forum User
 
Posts: 14
Joined: Wed Sep 17, 2003 10:01 am

Postby ednark » Thu Sep 25, 2003 4:28 pm

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?
ednark
New php-forum User
New php-forum User
 
Posts: 18
Joined: Fri Oct 04, 2002 8:09 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron