Page 1 of 1

Pulling data based on line number from text file, how?

Posted: Thu Sep 22, 2005 6:33 pm
by netpro25
Thanks for taking the time to read my post. I usually am able to find my answer without posting but I cannot find this anywhere. I would consider my self a novice PHP'er.

So here is what I am trying to do, I have these 15 digit liscense keys for software that I am selling through my website, so instead of manually emailing them I would like to setup a script to do it automatically. The trick is, after a key is sold it needs to be marked or removed so it does not get sold again.

If anyone has any ideas on how to do this please just give me a little help. I have been searching google for days and have not found anything.

Thanks a bunch,
Marcel

Posted: Thu Sep 22, 2005 6:36 pm
by Charles256
off the top of my head... store them in a mysql database (make sure it's secure of course). when the buy the key have it display the key on the screen or display something like, "thank you, your key has been e-mailed to you." and then use php mail() function to e-mail them and then immediatly have it delete the key from the database. or perhaps write the key to a different table, perhaps, "used keys" and then delete from "unused keys".. that help?

edited the post, sorry about the incomplete answer;)

Posted: Thu Sep 22, 2005 6:49 pm
by netpro25
I appreciate the response, that gives me the basic idea, where I am stuck is writing the code to pull the single key from the database rather then every key. I want to store them all in one database field for simplicity.

Posted: Thu Sep 22, 2005 6:53 pm
by Charles256
i recommend two tables. be the easiest way. one for new keys, one for used keys. either way to select a key. SELECT KEY FROM KEY_TABLE LIMIT 1 . LIMIT 1 limits you to one key being pulled:-D

Posted: Thu Sep 22, 2005 6:57 pm
by Chris Corbyn
Ideally you want these keys in a MySQL database like Charles says. I think the best thing to would be to have something simple like an extra column for "used" or similar and if it's set to "1" then it's unusable anymore. So you'd email them the key and update the record so that used=1.

If you really do want to go with the text file approach something like this would work...

I'll assume the file you contains a list of keys all on new lines like this

Code: Select all

XXX-YYY-ZZZ
AAA-BBB-CCC
DDD-EEE-FFF
HHH-AAA-BBB

Code: Select all

$lines = file($path_to_text_file); //Open the file and collect the lines as an array
$code = trim($lines[0]); //Get the first line (and remove any trailing whitespace)

/*
 Something to email or display the code here
 mail() ?
 */

array_shift($lines); //Remove the code we just used
$data = implode('', $lines); //Glue it back together as a string

$handle = fopen($path_to_text_file, 'w'); //Open the file again for writing this time
fwrite($data, $handle); //Write back to the file with a missing line
fclose($handle); //Close the file

Posted: Thu Sep 22, 2005 7:24 pm
by netpro25
The original idea was to use a MySQL database which I use for everything else, but I did not think it was possible. Stupid me ;-p

I bet anything is possible with PHP and MySQL. So yes give me an idea for doing it with MySQL.

I have found out how to do it with a flat file using the following script:

<?php
$handle = fopen("test.txt", "r");

$buffer = fgets($handle, 4096);
echo $buffer;

fclose($handle);
?>

Maybe someone can fill me in on how to post PHP script in the nifty little boxes.

Thanks so much for the overwhelming response!!

Marcel

Posted: Thu Sep 22, 2005 7:39 pm
by Charles256
grrrrr..the idea is for you to go do the coding and tell us if you have an error but...I'm bored so here's the code
NOTE: NOT TESTED
Conventions: Assuming we have a table called keys and two fields, one key and one used.
I'm also assuming you got their e-mail address in the form they submitted too:-D

Code: Select all

<?php
$email=$_POST['email'];
$sql="SELECT * FROM keys WHERE used == '0' LIMIT 1";
$query=mysql_query($sql);
$result=mysql_fetch_object($query);

echo ("Thank you for purchasing a CD key your key is $result->key . You will also recieve an e-mail with this cd key, thank you again.");

$message="Your cd key is $result->key . Thank you again.";
$headers='From: myself@now.com'."\r\n" . ' Reply-To: stillme@now.com' ."\r\n";
$subject="Thank you for your purchase.";

mail($email,$subject,$message,$headers);

$secondsql="UPDATE keys SET used='1' WHERE key=$result->key";
$secondquery=mysql_query($secondsql);
?>
that should do it..
but that is off the top of my head so ya know.. :-D
i had a typo:)

Posted: Thu Sep 22, 2005 7:41 pm
by netpro25
Obviously I need some help, can you reccomend some resources? Books or websites? I really appreciate this!

Posted: Thu Sep 22, 2005 7:43 pm
by Charles256
the php manual:-D PHP and MySQL web development also is a good book. along with teach yourself php by sams is good. those are the two main ones I used, along with reading the manual:-D
And i'm still an idiot;)

Posted: Thu Sep 22, 2005 7:44 pm
by feyd
we are also not above answering the ~random newb questions here! ;)

Posted: Thu Sep 22, 2005 7:46 pm
by Charles256
oh yeah. I forgot :oops: this is a great resource, this forum alone. :-D