networkthis Posted September 15, 2008 Share Posted September 15, 2008 How can I compare the output from this function with a set variable....example this fucntion is counting the number of folders I have in a current said directory. I need to have the output when the directory count is zero to not allow an auto filled drop down menu to be populated as empty. I already have all code finished for this and to autofill the drop menu. So, what I am having problems with is taking the output of this function and comparing it 0 Here is what I currently have... <?php $Number_of_Folders = displayFolderCount($path); if ($Number_of_Folders== '0'){ //Then do nothing.... } ?> How do I pull the value from the function and change it to a variable???????? Thought this would work but it doesn't........any ideas?? <?php $Number_of_Folders = displayFolderCount($path); ?> example of my count folder function...... <?php function displayFolderCount($path) { $dir = opendir($path); while(FALSE !== ($file = readdir($dir))) { if ($file != "." && $file != "..") { $files[] = "$file"; }} closedir($dir); $foldercount=0; foreach($files as $value){ # Fname will be used to simply copy the Actual Name of the File!!! $fName = $value; # Create the path for the file $value = $path.'/'.$value; #****************************# # Simply checks to see if the $file is # # actually a DIRECTORY. If it is display # # the following. # #*****************************# if (is_dir($value)) { $foldercount=$foldercount+1; } } echo $foldercount; } ?> Link to comment https://forums.phpfreaks.com/topic/124254-solved-function-question/ Share on other sites More sharing options...
rhyspaterson Posted September 15, 2008 Share Posted September 15, 2008 Your function needs to return something - like TRUE or FALSE or a variable. echo $foldercount; // should be return $foldercount; http://au2.php.net/return Link to comment https://forums.phpfreaks.com/topic/124254-solved-function-question/#findComment-641639 Share on other sites More sharing options...
DarkWater Posted September 15, 2008 Share Posted September 15, 2008 Just for the record, you don't need that gigantic function to get the number of directories other than . and .. in a folder. You can just do: $foldercount = count(glob('*', GLOB_ONLYDIR)); Link to comment https://forums.phpfreaks.com/topic/124254-solved-function-question/#findComment-641641 Share on other sites More sharing options...
networkthis Posted September 15, 2008 Author Share Posted September 15, 2008 Thanks for the quick responses.........the reason I have that long of a function is that I am having it not count folders hidden to the user...which are removed from the function...I left that part of the code out.... is it possible to use the glob count and to filter out specific folder names...as to not count them??? Link to comment https://forums.phpfreaks.com/topic/124254-solved-function-question/#findComment-641647 Share on other sites More sharing options...
networkthis Posted September 15, 2008 Author Share Posted September 15, 2008 Thank you seriously for the help...sometimes its the little things!!! it seems I had a typo in my if statement. I had originally had a RETURN at the end of the function...but it would never work... so I changed it to echo to see if it was counting properly. After you saying to use the RETURN I got to looking at the rest of the code. Come to find out this does not work <?php if ($Number_of_Folders = '0'){ //Then do nothing.... } ?> this does! <?php if($Number_of_Folders == 0 { //Then do nothing.... } ?> Once again thank you and happy coding!! Link to comment https://forums.phpfreaks.com/topic/124254-solved-function-question/#findComment-641657 Share on other sites More sharing options...
rhyspaterson Posted September 15, 2008 Share Posted September 15, 2008 Remember that = is not the same as == (or even ===). = http://au.php.net/manual/en/language.operators.assignment.php == and === http://au.php.net/manual/en/language.operators.comparison.php //This won't work - as you stated if ($Number_of_Folders = '0'){ //Then do nothing.... } //This will - as you stated if ($Number_of_Folders == 0){ //Then do nothing.... } //This will too! if ($Number_of_Folders == '0'){ //Then do nothing.... } Link to comment https://forums.phpfreaks.com/topic/124254-solved-function-question/#findComment-641681 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.