Guest MrLeN Posted August 27, 2015 Share Posted August 27, 2015 (edited) I have 2 paces of code that work by themselves. But when I try to put 1 inside the other, it wont work. Basically, I want to bit the bottom code inside the top code where I have a comment "<!---SECOND CODE IN HERE--->": I have been working on this all day and now it's 5:17am. As far as I can work out, I think it has something to do with opendir twice.. because the codes work by themselves (I tested them).. but one wont work inside the other. I am completely out of ideasI know you can put a loop n a loop because I tested it. I think it's the opendirs. I get the error: Warning: readdir(): 372 is not a valid Directory resource 1st code: $dir = '/home/tool/public_html/referrals_bridge/'; if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && $entry != ".php" && $entry != ".txt" && $entry != "error_log") { //echo "$entry\n"; $affiliate_id = file_get_contents($dir . $entry); $user_id = $entry; //echo "Referral ID:" . $affiliate_id . "<br />"; $user_id = str_replace('.txt', '', $user_id); echo "User ID:" . $user_id . "<br />"; //$user = $entry; //$user_role = file_get_contents('/home/tool/public_html/referrals/'.$affiliate_id.'/role.txt'); //exit; //Get user information $user_info = get_userdata($user_id); $paypal_email = $user_info->paypal_email; $contact_email = $user_info->user_email; $user_role = implode(', ', $user_info->roles); echo $user_role . "<br />"; $user_role =preg_replace('/^([^,]*).*$/', '$1', $user_role); <!---SECOND CODE IN HERE---> echo '<table summary="" cellpadding="0" cellspacing="0" border="1" style="font-size: 12px; font-family: arial"><tr>'; echo '<td>ID</td><td>Role</td><td>Aff ID</td><td>Refs</td><td>Free</td><td>VIP</td><td>Commission</td><td>email</td></tr><tr>'; echo '<td nowrap>'.$user_id.'</td>'; echo '<td nowrap style="width: 200px;">'.$user_role.'</td>'; echo '<td nowrap>'.$affiliate_id.'</td>'; echo '<td nowrap>'.$totalMembers.'</td>'; echo '<td nowrap>'.$UnSubscribedMembers.'</td>'; echo '<td nowrap>'.$SubscribedMembers.'</td>'; echo '<td nowrap><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$paypal_email.'&item_name=toolHost+Commission&item_number='.$user_id.'¤cy_code=USD&lc=CA&charset=UTF%2d8&amount='.$referral_commissions.'" target="_blank">'.$referral_commissions.'</a></td>'; echo '<td nowrap><a href="mailto:'.$contact_email.'&subject=Re: toolHost Commissions">contact</a></td>'; echo '</tr></table>'; } } closedir($handle); } 2nd code: // initialize counters to zero $SubscribedMembers = 0; $UnSubscribedMembers = 0; //$current_user = wp_get_current_user(); $dir = '/home/tool/public_html/referrals/' . basename($affiliate_id); if ($handle2 = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && $entry != "role.txt"&& $entry != "promos") { //echo "$entry\n"; $path = "$dir/$entry"; $result = file_get_contents($path); switch(trim($result)) { // increment subscribed couter, if the contents of the file is "subscribed" case 's2member_level2': $SubscribedMembers++; break; // increment unsubscribed couter, if the contents of the file is "unsubscribed" case 's2member_level1': default: $UnSubscribedMembers++; break; } } } closedir($handle2); } // calculate total members $totalMembers = $SubscribedMembers + $UnSubscribedMembers; // output results $referral_commissions = ($SubscribedMembers * '7.5'); //clean up long names: if ($user_role == "subscriber") { $user_role = "Free"; } if ($user_role == "s2member_level1") { $user_role = "Entrepreneur"; } if ($user_role == "s2member_level2") { $user_role = "VIP"; } Edited August 27, 2015 by MrLeN Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/ Share on other sites More sharing options...
ginerjm Posted August 27, 2015 Share Posted August 27, 2015 Save us all some time and SHOW us the final code that you are trying to test out. Then we can see it as you want it Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/#findComment-1519777 Share on other sites More sharing options...
Guest MrLeN Posted August 27, 2015 Share Posted August 27, 2015 (edited) That is the code in its entirety. Here it is together: My putting it into two separate files that work was my attempt at making it easier $dir = '/home/tool/public_html/referrals_bridge/'; if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && $entry != ".php" && $entry != ".txt" && $entry != "error_log") { //echo "$entry\n"; $affiliate_id = file_get_contents($dir . $entry); $user_id = $entry; //echo "Referral ID:" . $affiliate_id . "<br />"; $user_id = str_replace('.txt', '', $user_id); echo "User ID:" . $user_id . "<br />"; //$user = $entry; //$user_role = file_get_contents('/home/tool/public_html/referrals/'.$affiliate_id.'/role.txt'); //exit; //Get user information $user_info = get_userdata($user_id); $paypal_email = $user_info->paypal_email; $contact_email = $user_info->user_email; $user_role = implode(', ', $user_info->roles); echo $user_role . "<br />"; $user_role =preg_replace('/^([^,]*).*$/', '$1', $user_role); // initialize counters to zero $SubscribedMembers = 0; $UnSubscribedMembers = 0; //$current_user = wp_get_current_user(); $dir = '/home/tool/public_html/referrals/' . basename($affiliate_id); if ($handle2 = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && $entry != "role.txt"&& $entry != "promos") { //echo "$entry\n"; $path = "$dir/$entry"; $result = file_get_contents($path); switch(trim($result)) { // increment subscribed couter, if the contents of the file is "subscribed" case 's2member_level2': $SubscribedMembers++; break; // increment unsubscribed couter, if the contents of the file is "unsubscribed" case 's2member_level1': default: $UnSubscribedMembers++; break; } } } closedir($handle2); } // calculate total members $totalMembers = $SubscribedMembers + $UnSubscribedMembers; // output results $referral_commissions = ($SubscribedMembers * '7.5'); //clean up long names: if ($user_role == "subscriber") { $user_role = "Free"; } if ($user_role == "s2member_level1") { $user_role = "Entrepreneur"; } if ($user_role == "s2member_level2") { $user_role = "VIP"; } echo '<table summary="" cellpadding="0" cellspacing="0" border="1" style="font-size: 12px; font-family: arial"><tr>'; echo '<td>ID</td><td>Role</td><td>Aff ID</td><td>Refs</td><td>Free</td><td>VIP</td><td>Commission</td><td>email</td></tr><tr>'; echo '<td nowrap>'.$user_id.'</td>'; echo '<td nowrap style="width: 200px;">'.$user_role.'</td>'; echo '<td nowrap>'.$affiliate_id.'</td>'; echo '<td nowrap>'.$totalMembers.'</td>'; echo '<td nowrap>'.$UnSubscribedMembers.'</td>'; echo '<td nowrap>'.$SubscribedMembers.'</td>'; echo '<td nowrap><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$paypal_email.'&item_name=toolHost+Commission&item_number='.$user_id.'¤cy_code=USD&lc=CA&charset=UTF%2d8&amount='.$referral_commissions.'" target="_blank">'.$referral_commissions.'</a></td>'; echo '<td nowrap><a href="mailto:'.$contact_email.'&subject=Re: toolHost Commissions">contact</a></td>'; echo '</tr></table>'; } } closedir($handle); } Edited August 27, 2015 by MrLeN Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/#findComment-1519782 Share on other sites More sharing options...
ginerjm Posted August 27, 2015 Share Posted August 27, 2015 (edited) if ($handle2 = opendir($dir)) { while (false !== ($entry = readdir($handle))) { [\php] You opened $handle2 and tried to read from $handle. PS - you can write it like this: while ($entry = readdir($handle2)) instead of the more complicated way you did it. Edited August 27, 2015 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/#findComment-1519791 Share on other sites More sharing options...
Guest MrLeN Posted August 28, 2015 Share Posted August 28, 2015 (edited) if ($handle2 = opendir($dir)){while (false !== ($entry = readdir($handle))){[\php] You opened $handle2 and tried to read from $handle. PS - you can write it like this: while ($entry = readdir($handle2)) instead of the more complicated way you did it.Oh I forgot I changed it to handle2 - they were both just 'handle'.. and I figured maybe there was some kind of conflict. I'll revise that again and see if it's still giving me errors. *because there are 2 open dirs one inside the other, I suspect that might be causing the problem. That's why I started trying to change the name of the handles. I worked on this all day yesterday... I just work up. back at it again now. I'll start by changing everything back to handle: $dir = '/home/tool/public_html/referrals_bridge/'; if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && $entry != ".php" && $entry != ".txt" && $entry != "error_log") { //echo "$entry\n"; $affiliate_id = file_get_contents($dir . $entry); $user_id = $entry; //echo "Referral ID:" . $affiliate_id . "<br />"; $user_id = str_replace('.txt', '', $user_id); echo "User ID:" . $user_id . "<br />"; //$user = $entry; //$user_role = file_get_contents('/home/tool/public_html/referrals/'.$affiliate_id.'/role.txt'); //exit; //Get user information $user_info = get_userdata($user_id); $paypal_email = $user_info->paypal_email; $contact_email = $user_info->user_email; $user_role = implode(', ', $user_info->roles); echo $user_role . "<br />"; $user_role =preg_replace('/^([^,]*).*$/', '$1', $user_role); // initialize counters to zero $SubscribedMembers = 0; $UnSubscribedMembers = 0; //$current_user = wp_get_current_user(); $dir = '/home/tool/public_html/referrals/' . basename($affiliate_id); if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && $entry != "role.txt"&& $entry != "promos") { //echo "$entry\n"; $path = "$dir/$entry"; $result = file_get_contents($path); switch(trim($result)) { // increment subscribed couter, if the contents of the file is "subscribed" case 's2member_level2': $SubscribedMembers++; break; // increment unsubscribed couter, if the contents of the file is "unsubscribed" case 's2member_level1': default: $UnSubscribedMembers++; break; } } } closedir($handle); } // calculate total members $totalMembers = $SubscribedMembers + $UnSubscribedMembers; // output results $referral_commissions = ($SubscribedMembers * '7.5'); //clean up long names: if ($user_role == "subscriber") { $user_role = "Free"; } if ($user_role == "s2member_level1") { $user_role = "Entrepreneur"; } if ($user_role == "s2member_level2") { $user_role = "VIP"; } echo '<table summary="" cellpadding="0" cellspacing="0" border="1" style="font-size: 12px; font-family: arial"><tr>'; echo '<td>ID</td><td>Role</td><td>Aff ID</td><td>Refs</td><td>Free</td><td>VIP</td><td>Commission</td><td>email</td></tr><tr>'; echo '<td nowrap>'.$user_id.'</td>'; echo '<td nowrap style="width: 200px;">'.$user_role.'</td>'; echo '<td nowrap>'.$affiliate_id.'</td>'; echo '<td nowrap>'.$totalMembers.'</td>'; echo '<td nowrap>'.$UnSubscribedMembers.'</td>'; echo '<td nowrap>'.$SubscribedMembers.'</td>'; echo '<td nowrap><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$paypal_email.'&item_name=toolHost+Commission&item_number='.$user_id.'¤cy_code=USD&lc=CA&charset=UTF%2d8&amount='.$referral_commissions.'" target="_blank">'.$referral_commissions.'</a></td>'; echo '<td nowrap><a href="mailto:'.$contact_email.'&subject=Re: toolHost Commissions">contact</a></td>'; echo '</tr></table>'; } } closedir($handle); } i got this message: Warning: readdir(): 518 is not a valid Directory resource in/home/tool/public_html/inc/member-commissions.php on line 5Warning: closedir(): 518 is not a valid Directory resource in/home/tool/public_html/inc/member-commissions.php on line 90 Edited August 28, 2015 by MrLeN Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/#findComment-1519805 Share on other sites More sharing options...
Solution Ch0cu3r Posted August 28, 2015 Solution Share Posted August 28, 2015 (edited) No you need to have different handles. The second $handle (for reading the referrals directory) will be overwriting the first $handle (for reading the referrals_bridge directory). @ginerjm PS - you can write it like this:while ($entry = readdir($handle2))instead of the more complicated way you did it. The PHP manual recommends you write it that way. Edited August 28, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/#findComment-1519813 Share on other sites More sharing options...
Guest MrLeN Posted August 28, 2015 Share Posted August 28, 2015 oh, okies.. I will try different handles again (and not try to mess it up this time lol).. Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/#findComment-1519820 Share on other sites More sharing options...
Guest MrLeN Posted August 28, 2015 Share Posted August 28, 2015 Ah.. the opendir problem is fixed now Thanks guys. I have new problems now, but I'll be able to figure them out myself Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/#findComment-1519821 Share on other sites More sharing options...
ginerjm Posted August 28, 2015 Share Posted August 28, 2015 The language has a unique way of testing the readdir function? News to me. How many more functions must be treated this way? Is there a list of them? Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/#findComment-1519822 Share on other sites More sharing options...
scootstah Posted August 28, 2015 Share Posted August 28, 2015 The language has a unique way of testing the readdir function? It has nothing to do with the readdir function, it has to do with PHP's loose type. Did you skip this part in the manual? Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/#findComment-1519823 Share on other sites More sharing options...
Guest MrLeN Posted August 28, 2015 Share Posted August 28, 2015 Y'all lost me now. I am lost glad my problem is fixed lol. I am not a very good programmer, but I try hard Quote Link to comment https://forums.phpfreaks.com/topic/297961-opendir-and-loops/#findComment-1519824 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.