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

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 »

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:

Code: Select all

$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.
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 »

Um mate, u fgt. the variable $myCoupon. Also, I need to be able to use either one coupon code, or many in an array.

Also, the above code did NOT delete the coupon code in the file my_file.txt when used one time.

Code: Select all

<?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');
?>
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 »

this is the final bit of what i need. if u can do the above, then this topic is solved.
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 »

My bad. I was getting tired:

Code: Select all

$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
Forum Commoner
Posts: 75
Joined: Tue Jan 12, 2010 8:43 am

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

Post by ShadowIce »

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.
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 »

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.
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 »

then why when i open my_file.txt does it still show the same coupon code?
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 »

ShadowIce wrote:then why when i open my_file.txt does it still show the same coupon code?
Read the entire post, not just the code.

viewtopic.php?f=1&t=111394&p=587775#p587667

And for future reference, when developing:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');
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 »

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?
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 »

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.
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 »

Here's what I have for submit form.php:

Code: Select all

<form method="POST" name="theform" action="testbed.php">
<center>
<tr>
<td>Enter your discount code if you have one:<br><br><input type="text" name="cc_0001" id="cc_0001" size="30" value="000-000-00000" maxlength="13"></td>
</tr>
<br><br>
<input type="submit" value="Submit">
</center>
</form>
Here's what I have for the coupon code - 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 = 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 ?
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 »

Nevermind. I figured it out.

Now the problem is I'm STILL seeing the code that I submit inside of the text file after validating it.

What is going on w/ this code? :banghead:

submitform.php:

Code: Select all

<form method="POST" name="theform" action="testbed.php">
<center>
<tr>
<td>Enter your discount code if you have one:<br><br><input type="text" name="cc_0001" id="cc_0001" size="30" maxlength="64"></td>
</tr>
<br><br>
<input type="submit" value="Submit">
</center>
</form>
testbed.php:

Code: Select all

<?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);
         }
  
 }
 
?>
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 »

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
Forum Commoner
Posts: 75
Joined: Tue Jan 12, 2010 8:43 am

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

Post by ShadowIce »

It's the code u posted earlier. I decided to use only one variable instead of an array
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 »

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.
Post Reply