prisca91 Posted January 8, 2013 Share Posted January 8, 2013 Hi guys! I'm a beginner with php and need to write a code that displays a list of all folders present in the current work directory and a delete-button near each of them, in order to be able to cancel them. Here's my code: ($array_dir is an array containing the names of the directories in the current folder) for($b=0;$b<$conto;$b++){ echo"<tr><td width=\"300\" height=\"50\"><img src=\"folder.png\"><a href=$array_dir[$b]>".$array_dir[$b]."</a><br>"; echo"<form action='' name='deleteform".$b."' method='GET'>"; echo"<input type='submit' name='deletebut".$b."' value='Delete'>"; echo"</form></td><td>"; if(file_exists($array_dir[$b])) {if(isset($_GET['deletebut'.$b])) {if($_GET['deletebut'.$b]==$array_dir[$b]) {if(rmdir($array_dir[$b])) {echo $array_dir[$b]." was removed";} else {echo"Failed to remove directory ".$array_dir[$b];} } } } This output looks fine but when I click on the delete-button it doesn't delete the folder and doesn't even return any error. I can't really understand where the error is ! Quote Link to comment https://forums.phpfreaks.com/topic/272845-please-correct-my-php-code-beginner/ Share on other sites More sharing options...
haku Posted January 8, 2013 Share Posted January 8, 2013 Step one to writing good code is proper indentation. You should start there. Also, please post your code using code tags. You can do this using the <> button in the editor. Quote Link to comment https://forums.phpfreaks.com/topic/272845-please-correct-my-php-code-beginner/#findComment-1404168 Share on other sites More sharing options...
prisca91 Posted January 8, 2013 Author Share Posted January 8, 2013 Step one to writing good code is proper indentation. You should start there. Also, please post your code using code tags. You can do this using the <> button in the editor. <?php $conto=count($array_dir); echo"<table border=\"1\">"; for($b=0;$b<$conto;$b++){ echo"<tr><td width=\"300\" height=\"50\"><img src=\"folder.png\"><a href=$array_dir[$b]>".$array_dir[$b]."</a><br>"; echo"<form action='' name='deleteform".$b."' method='GET'>"; echo"<input type='submit' name='deletebut".$b."' value='Delete'>"; echo"</form></td><td>"; if(file_exists($array_dir[$b])) {if(isset($_GET['deletebut'.$b])) {if($_GET['deletebut'.$b]==$array_dir[$b]) {if(rmdir($array_dir[$b])) {echo $array_dir[$b]." was removed";} else {echo"Failed to remove directory ".$array_dir[$b];} } } ?> I think in this way it's properly indented but still doesn't work, so this is not the issue... Quote Link to comment https://forums.phpfreaks.com/topic/272845-please-correct-my-php-code-beginner/#findComment-1404171 Share on other sites More sharing options...
cpd Posted January 8, 2013 Share Posted January 8, 2013 Whitespace wont affect your code, it just makes it legible. Your code is still somewhat illegible as you've not followed any pretty standard conventions such as dropping a line when opening a curly brace. With regards to the code, you should use a foreach loop to cycle through the array not a for loop. You don't really need a form and button to remove a directory, you can just have a link that links to a directory deletion script with a directory name as GET data or something. That said, we can't see your code that removes the directory so how on earth you expect us to assist in debugging your code I really don't know... Quote Link to comment https://forums.phpfreaks.com/topic/272845-please-correct-my-php-code-beginner/#findComment-1404177 Share on other sites More sharing options...
prisca91 Posted January 8, 2013 Author Share Posted January 8, 2013 Sorry but I'm a beginner and thought that the rmdir(array_dir[$b]) was enough to delete an empty directory. Do you mean I should write a function that deletes a directory and create a link to this function, and not a delete-button? Quote Link to comment https://forums.phpfreaks.com/topic/272845-please-correct-my-php-code-beginner/#findComment-1404179 Share on other sites More sharing options...
haku Posted January 8, 2013 Share Posted January 8, 2013 (edited) Here is how people generally format code: <?php $conto=count($array_dir); echo"<table border=\"1\">"; for($b=0;$b<$conto;$b++){ echo"<tr><td width=\"300\" height=\"50\"><img src=\"folder.png\"><a href=$array_dir[$b]>".$array_dir[$b]."</a><br>"; echo"<form action='' name='deleteform".$b."' method='GET'>"; echo"<input type='submit' name='deletebut".$b."' value='Delete'>"; echo"</form></td><td>"; if(file_exists($array_dir[$b])) { if(isset($_GET['deletebut'.$b])) { if($_GET['deletebut'.$b]==$array_dir[$b]) { if(rmdir($array_dir[$b])) { echo $array_dir[$b]." was removed"; } else { echo"Failed to remove directory ".$array_dir[$b]; } } } ?> With this, you can see at a glance that you are missing some parenthesis. With the formatting you used, it was difficult to see that. As a side note, I actually format differently: <?php $conto=count($array_dir); echo"<table border=\"1\">"; for($b=0;$b<$conto;$b++) { echo"<tr><td width=\"300\" height=\"50\"><img src=\"folder.png\"><a href=$array_dir[$b]>".$array_dir[$b]."</a><br>"; echo"<form action='' name='deleteform".$b."' method='GET'>"; echo"<input type='submit' name='deletebut".$b."' value='Delete'>"; echo"</form></td><td>"; if(file_exists($array_dir[$b])) { if(isset($_GET['deletebut'.$b])) { if($_GET['deletebut'.$b]==$array_dir[$b]) { if(rmdir($array_dir[$b])) { echo $array_dir[$b]." was removed"; } else { echo"Failed to remove directory ".$array_dir[$b]; } } } ?> As you can see, it's still quite clear that there are two parenthesis missing. The actual formatting you use is mostly arbitrary (though when you are coding a project with other people, it's generally a good idea to match styles for consistency). The key point is that you format your code in an easy to read manner. It will not only save you hours and hours of headaches over time, it will also make it easier for people to help you, since they won't have to try to figure out what is going on in messy code. And when it's easier for people to help you, they will be more likely to do so. Good luck! Edited January 8, 2013 by haku Quote Link to comment https://forums.phpfreaks.com/topic/272845-please-correct-my-php-code-beginner/#findComment-1404221 Share on other sites More sharing options...
Jessica Posted January 8, 2013 Share Posted January 8, 2013 Brackets, not parens Quote Link to comment https://forums.phpfreaks.com/topic/272845-please-correct-my-php-code-beginner/#findComment-1404222 Share on other sites More sharing options...
haku Posted January 8, 2013 Share Posted January 8, 2013 Hah, I always get them mixed up. I have troubles with English sometimes. Brackets, parenthesis, braces. They screw me up. Quote Link to comment https://forums.phpfreaks.com/topic/272845-please-correct-my-php-code-beginner/#findComment-1404226 Share on other sites More sharing options...
cpd Posted January 8, 2013 Share Posted January 8, 2013 Sorry but I'm a beginner and thought that the rmdir(array_dir[$b]) was enough to delete an empty directory. Do you mean I should write a function that deletes a directory and create a link to this function, and not a delete-button? You can't "link" to a function, that's not how the web works. Your link directs the client to a script that carries out the directory removal and then perhaps outputs content... Or does something else, that part is up to you. Quote Link to comment https://forums.phpfreaks.com/topic/272845-please-correct-my-php-code-beginner/#findComment-1404230 Share on other sites More sharing options...
cpd Posted January 8, 2013 Share Posted January 8, 2013 Hah, I always get them mixed up. I have troubles with English sometimes. Brackets, parenthesis, braces. They screw me up. That's what the Americans say . The English use square brackets, brackets, curly brackets just to confuse things. Quote Link to comment https://forums.phpfreaks.com/topic/272845-please-correct-my-php-code-beginner/#findComment-1404231 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.