Hi I have a list of 100 coupon codes (e.g. abcd-1234) that I would like to randomly show to my users but as a hyperlink. So on page load it shows abcd-1234 or bcde-2345 or bcde-3456 but that code is clickable, when clicked goes to another page. How do I do this? Thanks everyone.
How does that differ from any other hyperlink you generate on your site. echo "<a href='{$random_code}'>{$random_code}</a>"; Code (markup):
Are you storing your list in a text file? If so, try something like this... <?php // get contents of a file into a string $filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $lines = explode("\n",$contents); $max = count($lines)-1; $random_number = rand(0,$max); $random_code = $lines[$random_number]; ?> <h1>Get your codes here</h1> <p>We've selected this just for you</p> <p><strong><?php echo $random_code; ?></strong></p> Code (markup):
If you don't have a database, how are your coupon codes stored? That's the question you have to answer before anyone can help you. If I understood your request correctly, @sarahk's solution does the job perfectly if you have the codes stored in a text file separated by a return for each new code.
The problem with having them stored in a flat file is that there is no way to avoid them being reused - so no need for unique values?
If you use the random generator code you include the list of the abcde_1234 bcdef_5678 etc. then the $random_code will grab one of them and place it into the hyperlink. <?php $input = array("abcde_1234", "bdcse_56473", "Cdget_25142");$rand_keys = array_rand($input, 1); $random_code = $input[$rand_keys[0]] ;?> something like that - but there are better basics than that ..thats a quick one