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
Share on other sites

The first result of scandir is almost always "." followed by "..". According to your function declaration, that is returned false so it is doing what it is supposed to be doing. Also, couldn't you just use the function is_dir to see if a folder is valid or not?

Link to comment
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

Link to comment
Share on other sites

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; }
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.