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!
Hi all. how can I use $myvar to compare it to every string in my_file.txt and return whether or not it matches the text in the file? I'm doing this so I can create a coupon code for my customers.
<?php
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('my_file.txt');
$myvar = "b";
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
if($myvar = htmlspecialchars($line)."<br \>\n"){
// echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
//echo $myvar." "."matches ".htmlspecialchars($line)."<br \>\n";
echo trim($myvar." "."matches ".htmlspecialchars($line)."<br />\n");
}else{
//echo $myvar." doesn't match ".htmlspecialchars($line)."<br />\n";
}
}
// Another example, let's get a web page into a string. See also file_get_contents().
$html = implode('', file('my_file.txt'));
// Using the optional flags parameter since PHP 5
$trimmed = file('my_file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>
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.
I have no idea what you're trying to do now. What's with the array and the implode?
If 'w4t8uw4tj3w8ru8w3ur8q3ujr83w' exists on its own line in 'my_file.txt', then the following code will display 'w4t8uw4tj3w8ru8w3ur8q3ujr83w is valid':
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
// Why are you doing this???
// $myCoupon = array('w4t8uw4tj3w8ru8w3ur8q3ujr83w', 'w4t8uw4tj3w8ru8w3ur8q3ujr83w');
// $comma_separated = implode(",", $myCoupon);
// echo $comma_seperated;
// Just do this!
$myCoupon = 'w4t8uw4tj3w8ru8w3ur8q3ujr83w';
if(in_array($myCoupon, $coupons) == true) {
$validCoupon = true;
}
else {
$validCoupon = false;
}
if($validCoupon == true) {
echo $myCoupon . ' is valid';
}
else {
echo 'Invalid coupon code: "' . $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.
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
$myCoupon = array('a', 'b');
$myCoupon = array_unique($myCoupon); // Remove duplicates from array
foreach($myCoupon as $value) {
if(in_array($value, $coupons)) {
echo $value . ' is valid';
}
else {
echo 'Invalid coupon code: "' . $value . '"';
}
}
Last edited by AbraCadaver on Tue Jan 12, 2010 2:51 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.
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
$myCoupon = array('a', 'b');
$myCoupon = array_unique($myCoupon); // Remove duplicates from array
foreach($myCoupon as $value) {
if(in_array($value, $coupons)) {
$validCoupon = true;
}
else {
$validCoupon = false;
break;
}
if($validCoupon == true) {
echo implode(',', $myCoupon) . ' are valid';
}
else {
echo 'Invalid coupon code: ' . $value;
}
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.
How can I make it so that when a user submits the code, $myCoupon checks the array of file strings and returns if it's a valid string. And how can I make it so the user can only use that code ONCE?
How can I make it so that when a user submits the code, $myCoupon checks the array of file strings and returns if it's a valid string. And how can I make it so the user can only use that code ONCE?
You're quickly moving into database territory.
Your first question is already solved by the code that has been posted, as far as I can tell.
As for your second question, there is no way to guarantee this, however you can have the user logon and then store their user id and coupon in a db and check it when they submit another one. Depending upon how the user is signed up, they could always register again with a different username or email or whatever and use the coupon again.
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.
Not if I have it read a file that tells it which ones NOT to use I'll simply have it log which coupon they use, and then have it go into another file. if that coupon is in there, then disable then from using it. thanks man! and yes i got ALL that from ur short, yet simple sentence man! Thanks!
Thanks ALL of you! I couldn't have done it w/o you! PROBLEM SOLVED!
ok, new problem. I've decided that i want to delete the coupon code in the file that the user has already used. that way, it is not used twice. how do i accomplish this?
<?php
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
$myCoupon = array('a', 'b');
foreach($myCoupon as $value) {
if(in_array($value, $coupons)) {
echo $value . ' is valid'.'<br>';
}
else {
echo 'Invalid coupon code: "' . $value . '"<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);
}
}
?>