Page 1 of 1
A little looping problem
Posted: Fri Aug 16, 2002 6:56 am
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++ )
{
print('<input type="checkbox" name="'."$x".'" value="'."$x".'">'."$file<br>\n");
}
?>
</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??
Posted: Fri Aug 16, 2002 9:29 am
by lc
I understand what you mean... so why not?
Code: Select all
<?php
$dirnavn = "../produkter";
if ($handle = opendir("$dirnavn")){
while (false !== ($file = readdir($handle))){
if ($file != "." and $file != ".." and $file != "index.php" and $file != "index.html"){
$filelistї] = "$file";
}
}
closedir($handle);
}
sort($filelist);
reset($filelist);
\\ this creates the filenames as the array $filelistї]
?>
<form action="delete_script.php" method="post">
<?php
foreach ($filelist as $key){
print "<input type="checkbox" name="$key" value="$key"> $key<br>";
}
?>
</form>
Meaning... basicly.. not using the $x file nr for del data but the filename in stead. Ingeting

Posted: Fri Aug 16, 2002 10:06 am
by Zeceer
Thanx, but I made the filenames work after while trying

. 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 ) )
{
print('<input type="checkbox" name="'."slett".'" value="'."$file".'">'."$to_start$file$to_end<br>\n");
}
?>
But the delete script want work as it's supposed to:
Code: Select all
<?php
$dirnavn = "../produkter";
$opendir = opendir( $dirnavn );
if( ! isset( $_POSTї'slett'] ) )
{
header( "Location: http://www.zeceer.tk" );
}
while( $file = readdir( $opendir ) )
{
if( $_POSTї'slett'] == $file )
{
print("Deletet<br>");
}
else
{
print("Not Deleted<br>");
}
}
?>
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
Posted: Fri Aug 16, 2002 10:17 am
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?
Posted: Fri Aug 16, 2002 10:33 am
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.
Posted: Fri Aug 16, 2002 11:05 am
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?
Posted: Fri Aug 16, 2002 11:42 am
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.
Posted: Fri Aug 16, 2002 1:29 pm
by Zeceer
i made work by taking a shortcut

. 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 ) )
{
print('<input type="radio" name="'."slett".'" value="'."$file".'">'."$to_start$file$to_end<br>\n");
}
?>