Jump to content
xisto Community
Sign in to follow this  
The_Element_Him

Help .. Cant Even Explain It

Recommended Posts

Alright....I have a idea for my website but im not sure how to do it.Basicly i want to count my files and show it on the main page ... kinda likeBackground : 200 Scripts: 155 Tutorials: 325 Templates: 5 Total: 695like i said i dont know really where to start. any ideas?

Share this post


Link to post
Share on other sites

Ok, the idea is that you need to count the number of files (Backgrounds, Scripts, tutorials, templates...). Let's make it simple by placing each file type in different folders. Scripts files will go in /Scripts, Tutorials file will do in /Tutorials, and so on...

Then using this code to count number of files in a particular folder, for instance, /Backgrounds. Remember the path could be absolute or relative. You have to work out the right path for each of them.

<?php$numofbackgrounds = 0;if ($handle = opendir('/Scripts/')) {    while (false !== ($file = readdir($handle))) {         if ($file != "." && $file != "..") {            $numofbackgrounds++;        }    }}closedir($handle); ?>

Using the same technique but diffrent path for Scripts, Tutorials and Templates:

<?php$numofscripts = 0;if ($handle = opendir('/Scripts/')) {    while (false !== ($file = readdir($handle))) {         if ($file != "." && $file != "..") {            $numofscripts++;        }    }}closedir($handle); ?>

<?php$numoftutorials = 0;if ($handle = opendir('/Tutorials/')) {    while (false !== ($file = readdir($handle))) {         if ($file != "." && $file != "..") {            $numoftutorials++;        }    }}closedir($handle); ?>

<?php$numoftemplates = 0;if ($handle = opendir('/Templates/')) {    while (false !== ($file = readdir($handle))) {         if ($file != "." && $file != "..") {            $numoftemplates++;        }    }}closedir($handle); ?>

So, finally, you can print out what you're after:

<?phpecho "Background : $numofbackgrounds Scripts: $numofscripts Tutorials: $numoftutorials Templates: $numoftemplates Total: ".($numofbackgrounds+$numofscripts+$numoftutorials+$numoftemplates);?>

Hope it helps, buddy

Share this post


Link to post
Share on other sites

Very good explaination of how to do it. That is exactly how I would do it. Speaking of which, could I have the other half of my brain back now. :DThat would be the most straight forward way to do it. If you can't do it that way for some reason, let us know and we'll see what we can do.vujsa

Share this post


Link to post
Share on other sites

if ($handle = opendir('/Scripts/')) {    while (false !== ($file = readdir($handle))) {         if ($file != "." && $file != "..") {
Would you or anyone else, explain this? LOL I am trying to learn some php and am lost about the "." &&$file != ".." (the like before that LOL)

 

".($numofbackgrounds+$numofscripts+$numoftutorials+$numoftemplates);
OK, I get this, but why the . ?

Hope it helps, buddy

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites

nice dungsport :(

 

moonwitch: the && $file != ".." is basically telling it in the while loop to NOT count the '.' and '..' directories/files which are always present when you read a dir with php. it basically ignores it so it doesn't show up as a file when counting how many files are in the dir. say there is a directory with 10 text files, if you count how many files there is in there it will return 12 unless you specifically state to ignore the '.' and '..'.

 

The && is basically 'AND', if file = 'asda' AND file = 'asda2'.

 

the second thing about the period; you use this when you want to add a variable into a string with " ", if you did not add this into the code when you want to add variables together among other things for example:

 

$num1 = 10;$num2 = 20;$num3 = 222;echo "total: " . ($num1 + $num2 + $num3);

output: total: 252

 

 

$num1 = 10;$num2 = 20;$num3 = 222;echo "total: ($num1 + $num2 + $num3)";

output: total: (10 + 20 + 222)

 

you see the second does not use the period to join the variables into the string. it is just getting what value the variables have and echoing them out it is not adding them up.

 

hope the examples helped. i am not very good at explaining things lol.

Share this post


Link to post
Share on other sites

The dot * is the string concatenate operator in PHP. It just likes the ampesand (&) in Visual Basic, plus sign (+) in java/javascript.Any numeric data in the expression will be converted into string before it is joined together.

Share this post


Link to post
Share on other sites

Wow moonwitch, you won't get a better explaination than that.

 

dungsport and overture explained the code you asked about very well.

 

I like the way you learn scripting. You read the code and ask questions about things you don't understand. That is the same way I learn scripting.

 

I bet you have a lot of trouble trying to figure out what the php manual is trying to explain too. I usually need to see the code and the output before I can really get the concept to sink in. That is probably why I have so much trouble with classes. Hard to see the output without a lot more code.

 

I'm glad that there are so many php coders here. I'm sure I'll have maany more questions in the future.

 

Happy coding. :(

 

vujsa

Share this post


Link to post
Share on other sites

I wonder if there would be a performance penalty when using the opendir function and iterating through each file to count them?Seems to me if it's not a time critical task, that the webmaster can schedule Cronjob to run the script once or a few times a day. and input the resulting data into the database, then query the static data each time those numbers are being presented.Anyway, does anyone actually familiar with the performance cost?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.