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!
Well I would use array_search() instead of in_array() now because array_search() will return the key which you can unset() and then write the file again.
This might get you on the right track. Make sure the file is writable by the web server:
$coupons = file('my_file.txt', FILE_SKIP_EMPTY_LINES);
$coupons = array_map('trim', $coupons);
foreach($myCoupon as $value) {
// if found return the position in the array as $key
if(($key = array_search($value, $coupons)) !==false) {
echo $value . ' is valid'.'<br>';
// delete the coupon from the array using $key
unset($myCoupon[$key]);
}
else {
echo 'Invalid coupon code: "' . $value . '"<br>';
}
}
// join the array elements into a string and write back to the file
file_put_contents(implode('', $myCoupon), 'my_file.txt');
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.
<?php
$coupons = file('my_file.txt', FILE_SKIP_EMPTY_LINES);
$coupons = array_map('trim', $coupons);
$myCoupon = array('4wjtg84wtjw4tjw48ujtw84jt83wujr8wj4t8rujw3t8ujhw48t8w4twj48t','b');
foreach($myCoupon as $value) {
// if found return the position in the array as $key
if(($key = array_search($value, $coupons)) !==false) {
echo $value . ' is valid'.'<br>';
// delete the coupon from the array using $key
unset($myCoupon[$key]);
}
else {
echo 'Invalid coupon code: "' . $value . '"<br>';
}
}
// join the array elements into a string and write back to the file
file_put_contents(implode('', $myCoupon), 'my_file.txt');
?>
$coupons = file('my_file.txt', FILE_SKIP_EMPTY_LINES);
$coupons = array_map('trim', $coupons);
$myCoupon = array('4wjtg84wtjw4tjw48ujtw84jt83wujr8wj4t8rujw3t8ujhw48t8w4twj48t','b');
foreach($myCoupon as $value) {
// if found return the position in the array as $key
if(($key = array_search($value, $coupons)) !== false) {
echo $value . ' is valid'.'<br>';
// delete the coupon from the array using $key
unset($coupons[$key]);
}
else {
echo 'Invalid coupon code: "' . $value . '"<br>';
}
}
// join the array elements into a string and write back to the file
file_put_contents(implode("\n", $coupons), 'my_file.txt');
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 wrote:it still doesn't delete the coupon code after that certain code is used once and i still cant use either an array OR one code.
Yes it does, I tested it. As for the array or one value, you have two options that I'll leave to you as a learning exercise:
1. always define $myCoupons as an array, even if there is only one coupon
--or--
2. check if $myCoupons is an array and if not then replicate the code without the loop
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.
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.
so ur saying if i have an input box for my customers, and they enter the correct code, when it sends, it will check each line in the coupon codes file, and match it w/ the string they entered, and ONLY allow it to be used ONCE if its been entered and sent to the form?
ShadowIce wrote:so ur saying if i have an input box for my customers, and they enter the correct code, when it sends, it will check each line in the coupon codes file, and match it w/ the string they entered, and ONLY allow it to be used ONCE if its been entered and sent to the form?
If everything else that you've described is setup properly, then yes.
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.
<?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 = array('4wjtg84wtjw4tjw48ujtw84jt83wujr8wj4t8rujw3t8ujhw48t8w4twj48t','b');
foreach($myCoupon as $value) {
// if found return the position in the array as $key
if(($key = array_search($value, $coupons)) !== false) {
echo $value . ' is valid'.'<br>';
// delete the coupon from the array using $key
unset($coupons[$key]);
}
else {
echo 'Invalid coupon code: "' . $value . '"<br>';
}
}
// join the array elements into a string and write back to the file
file_put_contents(implode("\n", $coupons), 'my_file.txt');
?>
How can I link the two together? How can I change the array to match what the user inputs into the box in submitform.php ?
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
$myCoupon = $_POST['cc_0001'];
$sdiscount = $myCoupon;
if(in_array($myCoupon, $coupons) == true) {
$validCoupon = true;
}
else {
$validCoupon = false;
}
if($validCoupon == true) {
echo $myCoupon . ' is valid'.'<br>';
$hasdiscount=$validCoupon;
if($sdiscount != "" && $sdiscount != "0" && $sdiscount != "000-000-00000" && $sdiscount != null && $hasdiscount != 0 && $hasdiscount != null && $hasdiscount == 1){
echo "Deleted coupon code $myCoupon!"."<br>";
unset($myCoupon);
}
}
else {
echo 'Invalid coupon code: "' . $myCoupon . '"<br>';
}
// the line to delete
// $lineNum = 87;
// delLineFromFile('my_file.txt', $lineNum);
function delLineFromFile($fileName, $lineNum){
// check the file exists
if(!is_writable($fileName))
{
// print an error
print "The file $fileName is not writable";
// exit the function
exit;
}
else
{
// read the file into an array
$arr = file($fileName);
}
// the line to delete is the line number minus 1, because arrays begin at zero
$lineToDelete = $lineNum-1;
// check if the line to delete is greater than the length of the file
if($lineToDelete > sizeof($arr))
{
// print an error
print "You have chosen a line number, <b>[$lineNum]</b>, higher than the length of the file.";
// exit the function
exit;
}
//remove the line
unset($arr["$lineToDelete"]);
// open the file for reading
if (!$fp = fopen($fileName, 'w+'))
{
// print an error
print "Cannot open file ($fileName)";
// exit the function
exit;
}
// if $fp is valid
if($fp)
{
// write the array to the file
foreach($arr as $line) { fwrite($fp,$line); }
// close the file
fclose($fp);
}
}
?>
Dunno, that's not the code that I posted and it is riddled with problems.
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 wrote:It's the code u posted earlier. I decided to use only one variable instead of an array
Use my most recent code, just remove the foreach() and replace all occurrences of $value with $myCoupon.
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.