Jump to content

Call to Function is Never True?


neridaj

Recommended Posts

Hello,

 

I have a function that returns a boolean, which I am then using to provide a link or not to provide a link. Sounds simple enough, but I just don't see what I am doing wrong. The boolean function returns true and prints links to the page for each valid folder in a directory. However when I try to use that return in another function to provide a link based on "true" it never happens. should I use global variables instead? Here is the code:

 

<?php


function display_folders()
{
echo '<table border="1" cellpadding="5">';
$dir    = 'members/' . $_SESSION['valid_user'];
$files = scandir($dir);

foreach($files as $value) {

	// check for folders
	if(valid_folder($value))
	{
		echo '<tr><td><a href="photos.php?pa=' . $value . '"><img src="images/files.gif" width=75" height="75 />"<a/></td><td>' . $value . '</td></tr>';
		return true;
	}else{
		return false;
	}
}
echo '</table>';
}

function valid_folder($folder)
{
  // check if file is not an image or ./../
  if (!ereg('^([a-zA-Z].*|[1-9].*)\.(((j|J)(p|P)(g|G))|((g|G)(i|I)(f|F)))|[.|..]$', $folder))
    return true;
  else 
    return false;
}

function display_user_menu()
{

?>
<hr />
<a href="member.php">Home</a>  |  
<?php
  // only offer the get photos option if valid folder is found
  if(display_folders())
    echo '<a href="preview.php">Get Photos</a>  | '; 
  else
    echo "<font color='#cccccc'>No Photos</font> | "; 
?>
<a href="change_passwd_form.php">Change password</a>
<a href="logout.php">Logout</a> 
<hr />

<?php
}


?>

 

Thanks for any help,

 

Jason

 

(edited by kenrbnsn to add


tags)

Link to comment
https://forums.phpfreaks.com/topic/91926-call-to-function-is-never-true/
Share on other sites

The regex is:

 

!ereg('^([a-zA-Z].*|[1-9].*)\.(((j|J)(p|P)(g|G))|((g|G)(i|I)(f|F)))|[.|..]$', $folder)

 

which filters out images and  ./.. I figured out what was wrong with this anyways, but now I'm curious about the is_dir() you mentioned. When I use is_dir() the only directories returned are ./.. i.e., it doesn't include directories other than those. I would rather use this function than the regex, so I'm looking into it.

 

Thanks a lot,

 

Jason

Make the first line in your for loop an if statement. Check to see if the $value is '.' or '..'. If that is the case, skip the iteration. Otherwise, you can do the test.

 

<?php
foreach($files as $value) {
    if(in_array($value, array('.', '..'))) { continue; }
    
    // check for folders
    if(is_dir($dir.DIRECTORY_SEPARATOR.$value)) {
        printf('<tr><td><a href="photos.php?pa=%s">'.
                '<img src="images/files.gif" width=75" height="75 />'.
               '<a/></td><td>%s</td></tr>',
               $value, $value);
        return true;
    } else { return false; }
}
?>

Thanks for the insight into is_dir(), however, for some reason only the last $value is retained for each directory i.e., there are three valid directories but they all have the same link and the table cells containing the $value are not printed.

 

Thanks again though,

 

Jason

That is because you are using your function for all the different folders. Your function is currently set up in a way that whenever it finds a valid folder, it returns true and terminates. The next time you call it, it will do the same thing but start from the beginning again so you are not seeing the next folders. If you want to check each folder individually, just use the is_dir function directly.

I should have mentioned that I divided this function up into two different functions because I actually needed to check if there were folders before I displayed the link to actually display the folders. So, I had modified your function by removing the return true/false and using it in the check function which should have eliminated the termination of the original function as it would now only print for each iteration. For some reason (operator error?) it is working perfectly now and I really want to thank you for the help.

 

Thanks,

 

Jason

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.