Help comparing variable with lines from a file..

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

ShadowIce
Forum Commoner
Posts: 75
Joined: Tue Jan 12, 2010 8:43 am

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

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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().
Last edited by AbraCadaver on Wed Jan 13, 2010 12:37 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ShadowIce
Forum Commoner
Posts: 75
Joined: Tue Jan 12, 2010 8:43 am

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

Post by ShadowIce »

ur an EFFING genius! it WORKED! thank u SO much man! =D
ShadowIce
Forum Commoner
Posts: 75
Joined: Tue Jan 12, 2010 8:43 am

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

Post by ShadowIce »

Forgot to say.

PROBLEM SOLVED!
ShadowIce
Forum Commoner
Posts: 75
Joined: Tue Jan 12, 2010 8:43 am

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

Post 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>';
}
 
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ShadowIce
Forum Commoner
Posts: 75
Joined: Tue Jan 12, 2010 8:43 am

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

Post by ShadowIce »

i used the \r\n. It's PERFECT now! CASE: CLOSED! This time for real!
Post Reply