Page 3 of 3

Re: Help comparing variable with lines from a file..

Posted: Wed Jan 13, 2010 12:25 pm
by ShadowIce
Ok, I redid the code to work w/ ur new code, now here's the problems:

In testbed.php:

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
 
<?php
 
$coupons = file('my_file.txt', FILE_SKIP_EMPTY_LINES);
$coupons = array_map('trim', $coupons);
$myCoupon = $_POST['cc_0001'];
$sdiscount = $myCoupon;
 
// if found return the position in the array as $key
if(($key = array_search($myCoupon, $coupons)) !== false) {
echo $myCoupon . ' is valid'.'<br>';
$hasdiscount = 1;
// delete the coupon from the array using $key
echo "Deleted coupon code $myCoupon!"."<br>";
unset($coupons[$key]);
}
else {
$hasdiscount = 0;
echo 'Invalid coupon code: "' . $myCoupon . '"<br>';
}
 
// join the array elements into a string and write back to the file
file_put_contents(implode("\n", $coupons), 'my_file.txt');
 
?>
When the user enters an invalid coupon code, this error displays:

Code: Select all

Invalid coupon code: "a"
 
Warning: file_put_contents(4wjtg84wtjw4tjw48ujtw84jt83wujr8wj4t8rujw3t8ujhw48t8w4twj48t w4t8uw4tj3w8ru8w3ur8q3ujr83w) [function.file-put-contents]: failed to open stream: Invalid argument in \htdocs\testbed.php on line 27
and its still not deleting the coupon code the user used IF it is valid. these are the only 2 problems left that i know of.

Re: Help comparing variable with lines from a file..

Posted: Wed Jan 13, 2010 12:33 pm
by AbraCadaver
Ahh crap!

Code: Select all

file_put_contents('my_file.txt', implode("\n", $coupons));
Also, you can move the file_put_contents() to after the unset().

Re: Help comparing variable with lines from a file..

Posted: Wed Jan 13, 2010 12:36 pm
by ShadowIce
ur an EFFING genius! it WORKED! thank u SO much man! =D

Re: Help comparing variable with lines from a file..

Posted: Wed Jan 13, 2010 1:09 pm
by ShadowIce
Forgot to say.

PROBLEM SOLVED!

Re: Help comparing variable with lines from a file..

Posted: Wed Jan 13, 2010 1:21 pm
by ShadowIce
One. MINOR thing that I just now noticed.

When it deletes the coupon code, IF I have more than one line like this here file:

Code: Select all

a
b
c
d
e
f
g
It SHOULD show this:

Code: Select all

b
c
d
e
f
g
After it deletes, and i open the text file to check it, it shows this:

Code: Select all

bcdefg
testbed.php:

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
 
<?php
 
$coupons = file('my_file.txt', FILE_SKIP_EMPTY_LINES);
$coupons = array_map('trim', $coupons);
$myCoupon = $_POST['cc_0001'];
$sdiscount = $myCoupon;
 
// if found return the position in the array as $key
if(($key = array_search($myCoupon, $coupons)) !== false) {
echo $myCoupon . ' is valid'.'<br>';
$hasdiscount = 1;
// delete the coupon from the array using $key
echo "Deleted coupon code $myCoupon!"."<br>";
unset($coupons[$key]);
file_put_contents('my_file.txt', implode("\n", $coupons));
}
else {
$hasdiscount = 0;
echo 'Invalid coupon code: "' . $myCoupon . '"<br>';
}
 
?>

Re: Help comparing variable with lines from a file..

Posted: Wed Jan 13, 2010 1:24 pm
by AbraCadaver
I assume you are opening/viewing in notepad? notepad doesn't display newlines as newlines. Either view it in notepad++ or wordpad or change the \n to \r\n that notepad will display.

Re: Help comparing variable with lines from a file..

Posted: Wed Jan 13, 2010 1:25 pm
by ShadowIce
i used the \r\n. It's PERFECT now! CASE: CLOSED! This time for real!