Page 3 of 5
Posted: Mon Sep 25, 2006 11:56 pm
by akimm
Thanks once again, but now it says invalid argument supplied for foreach..
After that I figured maybe it was because the logic was working before the form was meant to be processed, therefore coming up with an error because there was neither a yes or no selected. I moved it to another file and referred to it from the form. It still had an error. So now i am confused as to why it would be an error. It was line 22.
However, it did write to the guestbook, it wrote the name of the file which was unlinked, which is not exactly what I need. Withmy code I wanted to unlink files which were disapproved, these files of course are seperate entrys for myguestbook, so what it did do was print the file name,which is good progress, but not exactly where i need it to be. i will tweak it when i can and hopefullyget it to work.
Posted: Tue Sep 26, 2006 1:33 am
by shneoh
Good to heard that. For a final attempt, I have twean the code again to follow. This should have worked with no error.
Code: Select all
<?php
if(!isset($_POST)){
echo "<PRE>";
echo "Print all from POST";
print_r($_POST);
echo "Print all from yes";
print_r($_POST["yes"]);
echo "Print all from no";
print_r($_POST["no"]);
echo "</PRE>";
$fp = fopen('guestbook.txt', 'w'); //<-- open file to write
if($fp){
foreach($_POST['yes'] as $filename){ //<-- Loop through all approved files.
fwrite($fp, $filename);
// unlink($filename); //<-- I dont know why you want to remove it, anyway, I just remain here... Smile
fclose($fp); //<-- close file.
}
foreach($_POST['no'] as $filename) { // Remove files which is not approved.
unlink($filename);
}
/* For those files which is not being choose, will do nothing. */
}
?>
<?php
foreach (glob("entrys/*.txt") as $files) {
?>
<table>
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<tr><td><?php include($files);?></td></tr>
<tr><td><h2>yes</h2></td><td><input type="radio" name="yes[]" value="<?php echo $files; ?>" id="yes"></td></tr>
<tr><td><h2>no</h2></td><td><input type="radio" name="no[]" value="<?php echo $files; ?>" id="no"></td></tr>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form></table>
<?php
}
?>
Posted: Tue Sep 26, 2006 6:17 am
by akimm
Thanks once more, but that code is the same problems, only this time, it won't even delete the file or put anything in guestbook.txt. I will continue to mess with this, thanks for the help you gave me so far.
Posted: Tue Sep 26, 2006 6:36 am
by akimm
I've varied Shneoh's code slightly here, its still not deleting or writing, although it appears all the logic should be in place, something is missing, I just don't have the eye to see it.
Code: Select all
<?php
if(!isset($_POST)){
echo "<PRE>";
echo "Print all from POST";
print_r($_POST);
echo "Print all from yes";
print_r($_POST["yes"]);
echo "Print all from no";
print_r($_POST["no"]);
echo "</PRE>";
foreach (glob("entrys/*.txt") as $files) {
$fp = fopen('guestbook.txt', 'a+'); //<-- open file to write
if($fp){
if ($_POST['name'] == 'yes') {
foreach($_POST['yes'] as $filename){ //<-- Loop through all approved files.
fwrite($fp, $filename, $files);
unlink($filename);
fclose($fp); //<-- close file.
}
}
}
if($_POST['name'] == 'no') {
foreach($_POST['no'] as $filename) { // Remove files which is not approved.
unlink($filename);
}
}
}
/* For those files which is not being choose, will do nothing. */
}
?>
<?php
foreach (glob("entrys/*.txt") as $files) {
?>
<table>
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<tr><td><?php include($files);?></td></tr>
<tr><td><h2>yes</h2></td><td><input type="radio" name="yes[]" value="<?php echo $files; ?>" id="yes"></td></tr>
<tr><td><h2>no</h2></td><td><input type="radio" name="no[]" value="<?php echo $files; ?>" id="no"></td></tr>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form></table>
<?php
}
?>
I've also tried
Code: Select all
if($_POST['name'] == $POST['yes']) {
foreach loop to write to file then unlink
}
and same for no.
this too failed.
Posted: Tue Sep 26, 2006 10:56 am
by akimm
I have tried a new method, still with no success, this time I tried a while loop. I mixed some of Shneoh's code and some of my original, I know I'm getting close, but I am still missing the functionality that I need to make this work.
With my code its not deleting the files and its not writing them to guestbook.txt. So far the onlything I have successfully written to guestbook.txt was entrys/sean9.txt which was essentially just a dummy entry to test this guestbook, but it wouldn't write the contents of sean9.txt so it does me no good.
Posted: Tue Sep 26, 2006 10:58 am
by akimm
I have tried a new method, still with no success, this time I tried a while loop. I mixed some of Shneoh's code and some of my original, I know I'm getting close, but I am still missing the functionality that I need to make this work.
With my code its not deleting the files and its not writing them to guestbook.txt. So far the onlything I have successfully written to guestbook.txt was entrys/sean9.txt which was essentially just a dummy entry to test this guestbook, but it wouldn't write the contents of sean9.txt so it does me no good.
here is my revised code, which probably will be modified again, since its still not working.
Code: Select all
<?php
foreach (glob("entrys/*.txt") as $files) {
$filename = print_r($files);
?>
<table>
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<tr><td><?php echo $filename;?></td>
<td><?php include($files);?></td></tr>
<tr><td><h2>yes</h2></td><br>
<td><input type="radio" name="name" value="yes" id="yes"><label for="yes">Yes</label></td></tr>
<tr><td><h2>no</h2></td><br>
<td><input type="radio" name="name" value="no" id="no"><label for="no">No</label></td></tr>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
<?php
echo "</table>";
}
?>
</form>
<?php
$fp = fopen('guestbook.txt', 'a');
if($_POST['yes']) {
fwrite($fp, $filename, $files);
unlink($filename, $files);
}
while($_POST['no']) {
unlink($filename, $files);
}
?>
Posted: Tue Sep 26, 2006 9:42 pm
by shneoh
Good to know you worked so hard to find the answer.
I have noticed a small mistake in my code. I have closed the file handler before the loop is ended. So the fclose should place outside the if($fp) statement. Rewrite as follow:
Original
Code: Select all
if($fp){
foreach($_POST['yes'] as $filename){ //<-- Loop through all approved files.
fwrite($fp, $filename);
// unlink($filename); //<-- I dont know why you want to remove it, anyway, I just remain here... Smile
fclose($fp); //<-- close file.
}
Change to this:
Code: Select all
if($fp){
foreach($_POST['yes'] as $filename){ //<-- Loop through all approved files.
fwrite($fp, $filename);
// unlink($filename); //<-- I dont know why you want to remove it, anyway, I just remain here... Smile
} fclose($fp); //<-- close file.
To get us more easy to debug this, you can try to include a echo on each loop as follow:
Code: Select all
if($fp){
foreach($_POST['yes'] as $filename){ //<-- Loop through all approved files.
fwrite($fp, $filename);
echo $filename; //<-- this will shows you if the loop is sucessfully run.
} fclose($fp); //<-- close file.
Please reminded that if the "no" option is not being checked, it will not perform deletion. Try it out.

Posted: Tue Sep 26, 2006 9:51 pm
by akimm
I have some new codes too, some using your stuff, some not.
Code: Select all
<?php
foreach (glob("entrys/*.txt") as $files) {
?>
<table>
<form method="POST" action="ad1.php">
<tr><td><?php include($files);?></td></tr>
<tr><td><h2>YES</h2></td><br>
<td><input type="radio" name="name" value="<? echo $files;?>" id="yes"><label for="yes"></label></td></tr>
<td><input type="hidden" name="name" value="<? echo $files;?>" id="yes"><label for="yes"></label></td></tr>
<tr><td><h2>NO</h2></td><br>
<td><input type="radio" name="name" value="<?php echo $files;?>" id="no"><label for="no></label></td></tr>
<td><input type="hidden" name="name" value="<?php echo $files;?>" id="no"><label for="no"></label><</td></tr>
<?php
echo "</table>";
}
?>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form>
ad1.php
Code: Select all
<?php
$files = $_POST['files'];
$fp = fopen('guestbook.txt', 'w');
if($_POST['name'] == "yes" && $fp) {
fwrite($fp, $files);
fclose($fp);
unlink($files);
}
if($_POST['name'] == "no") {
unlink($files);
}
?>
Posted: Tue Sep 26, 2006 10:09 pm
by akimm
When I click yes it gives me this:
Code: Select all
#entrys/ is the directory all guestbook entrys go to, sean4.txt is an arbitrary file with ##arbitrary file contents
entrys/sean4.txt
Warning: Invalid argument supplied for foreach() in /nfs/cust/8/25/05/650528/web/guestbook1/approve.php on line 21
Posted: Tue Sep 26, 2006 10:30 pm
by shneoh
Friend, you are still not understand why I want to change your code as my suggested. :S
1. The way you use is only able to handle single selection and it is giving no meaning to further action taken. The code here is logically not right to your idea:
Code: Select all
<input type="radio" name="name" value="<? echo $files;?>" id="yes">
Please think carefully. If you want to ask the program to delete any file on your system, what are the information you need to pass over? The name of the file, right? Now what you are passing? Try to place the code below to your very first line to print out the posted data on your screen and I hope you will understand better.
2. Your codes are really a mass. There are lots of html errors found in the page and some tag is not complete. Remember one rule: Do not mass the HTML with PHP scripts!
I have follow the way you trying to do and rewrite the codes as follow:
Code: Select all
<?php
foreach (glob("entrys/*.txt") as $files) {
?>
<table>
<form method="POST" action="ad1.php">
<tr><td><?php include($files);?></td></tr>
<tr><td><h2>yes</h2></td><td><input type="radio" name="yes[]" value="<?php echo $files; ?>" id="yes"></td></tr>
<tr><td><h2>no</h2></td><td><input type="radio" name="no[]" value="<?php echo $files; ?>" id="no"></td></tr>
<input type="submit" value="submit for addition to guestbook" name="guestbook">
</form>
</table>
<?php
}
?>
ad1.php
Code: Select all
<?php
// First check if any form being posted to this page. //
if(!isset($_POST)){
$fp = fopen('guestbook.txt', 'a');
// Start to do something if the file open sucessful. //
if($fp){
foreach($_POST['yes'] as $filename){ //<-- Loop through all approved files.
fwrite($fp, $filename);
}
fclose($fp); //<-- Since the file write is completed, close file.
// Remove files which is not approved. //
foreach($_POST['no'] as $filename) {
unlink($filename);
}
}
?>
Posted: Tue Sep 26, 2006 10:34 pm
by akimm
That was just a showing of what I tried hehe, that wasn't my only attempt i made about six, that was the least successful, as you clearly pointed out, the code sucked. and that is true, I implemented your code, and it printed out
entrys/sean9.txt
that is what that last post was all about, I will try this new code, but that one thing I just posted was to show you i'm not sitting on my backside waiting for someone to write this forme.
Posted: Tue Sep 26, 2006 10:37 pm
by akimm
However, that crap code i posted, and you revised, doesn't do anything. I click yes, it just goes to ad1.php, when i come back, both files that are tests still remain.
Posted: Tue Sep 26, 2006 10:50 pm
by shneoh
Have you tried to click on no?
Posted: Tue Sep 26, 2006 10:52 pm
by akimm
Yes sir. Both yes and no.
Posted: Tue Sep 26, 2006 10:56 pm
by shneoh
Code: Select all
foreach($_POST['no'] as $filename) {
unlink($filename);
}
Change to this:
Code: Select all
foreach($_POST['no'] as $filename) {
$check_del = unlink($filename);
echo $check_del;
}
What's the result print on screen?