A little looping problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

A little looping problem

Post by Zeceer »

I've made a script that first loops thru all the files in a directory, printing the files to the browser with a checkbox. This part works fine.

Code: Select all

<form action="delete_script.php" method="post">

<?php

$dirnavn = "../produkter";
$opendir = opendir( $dirnavn );

for( $x = 1; $file = readdir( $opendir ); $x++ )
       &#123;
             print('<input type="checkbox" name="'."$x".'" value="'."$x".'">'."$file<br>\n");
       &#125;

?>

</form>
The problem is the file that is going to check which of the checkboxes that are checked, and delete those files. I don't know how to do this, since the files that is going to be deletet is not named 1, 2, 3 and so on. But real names. That means that I may have to change the first script to? Since it gives the checkboxes values as 1, 2, 3 and so on?

Hope somebody understand what I mean??
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

I understand what you mean... so why not?

Code: Select all

<?php 

$dirnavn = "../produkter"; 
if ($handle = opendir("$dirnavn"))&#123;

while (false !== ($file = readdir($handle)))&#123;

if ($file != "." and $file != ".." and $file != "index.php" and $file != "index.html")&#123;

$filelist&#1111;] = "$file";
&#125;
&#125;
closedir($handle);
&#125;
sort($filelist);
reset($filelist);

\\ this creates the filenames as the array $filelist&#1111;]

?>

<form action="delete_script.php" method="post"> 

<?php

foreach ($filelist as $key)&#123;
print "<input type="checkbox" name="$key" value="$key"> $key<br>"; 
       &#125; 

?> 

</form>
Meaning... basicly.. not using the $x file nr for del data but the filename in stead. Ingeting ;)
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Thanx, but I made the filenames work after while trying :D . The problem now is the delete script.

Heres the list files script, and it works, perfectly:

Code: Select all

<?php

$dirnavn = "../produkter";
$opendir = opendir( $dirnavn );

while( $file = readdir( $opendir ) )
       &#123;
             print('<input type="checkbox" name="'."slett".'" value="'."$file".'">'."$to_start$file$to_end<br>\n");
       &#125;

?>

But the delete script want work as it's supposed to:

Code: Select all

<?php

$dirnavn = "../produkter";
$opendir = opendir( $dirnavn );

if( ! isset( $_POST&#1111;'slett'] ) )
    &#123;
            header( "Location: http://www.zeceer.tk" );
    &#125;


while( $file = readdir( $opendir ) )
       &#123;

             if( $_POST&#1111;'slett'] == $file )
                 &#123;
                         print("Deletet<br>");
                 &#125;
             else
                 &#123;
                         print("Not Deleted<br>");
                 &#125;
       &#125;

?>
The problem is that if I check more than one of the checkboxes, it only delete one of the files. The unset() function are here print("Deleted");. Just haven't used the unset() yet since I'm still testing. But by if I select all of the checkboxes for an example, it goes like this

Code: Select all

Not Deleted
Not Deleted
Not Deleted
Not Deleted
Deleted
Last edited by Zeceer on Fri Aug 16, 2002 10:53 am, edited 1 time in total.
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

Ah right I think I know what you mean....

How about when you use $file as name for the checkbox too.

Like:

print('<input type="checkbox" name="'."$file".'" value="'."$file".'">'."$to_start$file$to_end<br>\n");

And then in the delete script:

while( $file = readdir( $opendir ) )
{

if(isset( $_POST['$file'] ) )
{
print("Deletet<br>");
}
else
{
print("Not Deleted<br>");
}

should work no?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

You might want to consider naming the selectboxes "slett[]" the square brackets will cause slett to be an array that can hold multiple filenames. Currently only a single filename can be passed through slett from one page to another.

Then you'll have to test if a given filename is in the array slett on the deletion page to know if it should be kept or deleted.
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Sorry, but I didn't get any og that to work :? . It still only "delete" one file (print deleted one time). Even when i select a lot lot of them.

It would be great if someone could show me how, by using the scripts further up?
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

actually why are we doing it all this difficult?

I mean... Why not name the checkboxes $slett[] then use $file as value.

then on the return/del page just don't try to create another array.. you already know what you have... and want to delete:

so to check do:

foreach($_Post['slett'] as $key){
print "$key deleted";
}

and if that works just do the unlink $key thing.
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

i made work by taking a shortcut :D . Just made the checkboxes, radioes instead. When the file is deleted it reloades the page. I'll rather fix it later.

Code: Select all

<?php

$dirnavn = "../produkter";
$opendir = opendir( $dirnavn );

while( $file = readdir( $opendir ) )
       &#123;
             print('<input type="radio" name="'."slett".'" value="'."$file".'">'."$to_start$file$to_end<br>\n");
       &#125;

?>
Post Reply