igo Posted June 3, 2013 Share Posted June 3, 2013 Hi, I need to create a bunch of random unique coupon codes that are stored in a MySQL database and can be exported to give to the printer. My thinking is that I would use a PHP script that was ran from the command line to create, store, and export the newly created codes in a CSV. I would like to specify the number of codes it creates as a variable. That way I could create 100 or 1000 at a time depending on what I needed. So I came up with the following code, but it only creates, inserts, and exports one row. Which makes sense because I haven't specified anywhere in the code to create X number of coupons codes, i don't know where to do that at or how. I need to know how to get it to create whatever number of rows I specify(100, 1000, 5000, etc). Any ideas? Thanks in advance! <?PHP // Random String Function function random_string($length='12') { $ran_string = ""; $chars = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "A", "b", "B", "c", "C", "d", "D", "e", "E", "f", "F", "g", "G", "h", "H", "i", "I", "j", "J", "k", "K", "l", "L", "m", "M", "n", "N", "o", "O", "p", "P", "q", "Q", "r", "R", "s", "S", "t", "T", "u", "U", "v", "V", "w", "W", "x", "X", "y", "Y", "z", "Z"); $count = count($chars) - 1; srand((double)microtime() * 1000000); for ($i = 0; $i < $length; $i++) { $ran_string .= $chars[rand(0, $count)]; } return($ran_string); } //Variables $limitnumber = $argv[1]; $date = date('mdYHi'); $CouponCode = random_string(); //Connect to DB $link = mysql_connect('localhost','user','pass'); if (!$link) { die('Could not connect: ' . mysql_error()); } //Upload Coupon codes $query = mysql_query("INSERT INTO mediahub.Coupons (CouponCode, Date) VALUES (\"$CouponCode\", \"$date\");"); if (!$query) { echo "Could not successfully run query against the DB: " . mysql_error(); exit; }; //Select Coupon Codes Outfile $result2 = mysql_query("SELECT CouponCode into outfile '/tmp/CouponCodes_$date.csv' fields terminated by ',' lines terminated by '\n' FROM mediahub.Coupons WHERE date =$date"); if (!$result2) { echo "Could not successfully run query against the DB: " . mysql_error(); exit; } printf("Number of Coupon Codes Successfully Generated: %d\n", mysql_affected_rows()); ?> Quote Link to comment Share on other sites More sharing options...
kicken Posted June 3, 2013 Share Posted June 3, 2013 (edited) Just wrap the bit of code that generates and saves the coupon code in a loop. Then run your export after everything is generated. $limit = $_SERVER['argv'][1]; for ($i=0; $i<$limit; $i++){ $code = random_string(); $sql = 'INSERT INTO ...'; $query = mysql_query($sql); ... } //Then export $sql = 'SELECT ...'; $result2 = mysql_query($sql); ... Edited June 3, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
igo Posted June 3, 2013 Author Share Posted June 3, 2013 Worked perfectly! Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.