Jump to content

Please correct my php code (Beginner!)


prisca91

Recommended Posts

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 !

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by haku
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.