Help With Showing Random Number

Discussion in 'PHP' started by NITRO23456, Oct 28, 2013.

  1. #1
    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.
     
    NITRO23456, Oct 28, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,875
    Likes Received:
    4,547
    Best Answers:
    123
    Trophy Points:
    665
    #2
    How does that differ from any other hyperlink you generate on your site.
    echo "<a href='{$random_code}'>{$random_code}</a>";
    Code (markup):
     
    sarahk, Oct 28, 2013 IP
  3. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    516
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Its nore, how do I get it to show a random selection from 100 possibilities without a database?
     
    NITRO23456, Oct 28, 2013 IP
  4. NITRO23456

    NITRO23456 Well-Known Member

    Messages:
    516
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    to clarify, its how to display a random selection from a list of 100 that I'm not sure of.
     
    NITRO23456, Oct 28, 2013 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,875
    Likes Received:
    4,547
    Best Answers:
    123
    Trophy Points:
    665
    #5
    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):
     
    sarahk, Oct 28, 2013 IP
    Arick unirow and ryan_uk like this.
  6. Jonchun

    Jonchun Greenhorn Affiliate Manager

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    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.
     
    Jonchun, Oct 28, 2013 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    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?
     
    PoPSiCLe, Oct 29, 2013 IP
  8. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #8
    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
     
    ezprint2008, Oct 30, 2013 IP