My code:
echo "<table width='200px' cellpadding='5' cellspacing='0'><tbody>";
$total = 500;
for ($j = 0; $j < $total;$j++) {
echo "<tr>";
echo "<td>" . ($j+1) . "</td>";
echo "<td>" . get_coupon_code() . "</td>";
echo "</tr>";
}
function get_coupon_code() {
$characters = 'ABCDEFGHJKLMNPQRSTUZWXYZ';
$random_string_length = 8;
$string = '';
for ($i = 0; $i < $random_string_length; $i++) {
$string .= $characters[rand(0, strlen($characters) - 1)];
}
return $string;
}
echo "</tbody></table>";
What would be the way to ensure that each code that’s generated is unique?
use static array variable to store your coupon codes and before returning the code ensure that it is not available in the array. Or more simpler, store the codes in an array handled in a session.
Edited adding return to inner call of get_coupon_code()