Results 1 to 11 of 11

Thread: Display code randomly?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Display code randomly?

    Hi guy's i've created a points script, which users can randomly earn points.

    and then spend them on certain parts of my website. Im stuck on one part though and was wondering if anyone can help?

    I need some sort of code that will display a submit button i've got - randomly..


    Like so that, if a user, goes on a page, theres like a 25 % chance that it will display my submit button.


    Thanks,

    Jamie

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Display code randomly?

    Not sure about how to make sure its a 25% chance and I don't know much about PHP, but I think you could simply make a random between 4 different values and if it happens to be the right one then display the button.

    All you need is to use rand and then an if statement to check which value was selected.

    I think that would be a good start.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: Display code randomly?

    well 25% is simply make a random that will display 1 out of 4 times.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Display code randomly?

    Hiya, Thanks for the replys!

    By anychance, do you know what code it would be to use, for the rand function to do so?

    Thanks,
    Jamie

  5. #5
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Display code randomly?

    Something like this should work:

    PHP Code:

    $randint 
    rand(1,4);

    if (
    $randint == 4) {
           
    // button code here

    But you may want to keep a log of who it displays too. For example, you may only want them to see this button once ever, or once a week/day/hour etc. Other wise, every page refresh, they will have 1/4th chance of seeing it.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Display code randomly?

    Thanks dclamp!

    Quick question what would i edit, To make it so the user would only see it once, and never again?

    Thanks,
    Jamie

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Display code randomly?

    Code:
    <?php
    
    $randint = rand(1,4);
    
    if ($randint == 4) {
           // button code here
    
         <form action="http://vitalradio.co.uk/scripts/points/100.php" method="post" name="myform" id="myform">
    
              <input name="doSave" type="submit" id="doSave" value="Redeem Points">
    
    } 
    
    ?>
    Im using the code above - But i keep getting a syntax error, Unexpected T_VARIABLE .

    Any idea why?

    Thanks

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Display code randomly?

    It's all cool now Done that, the codes now working! Thanks!

  9. #9
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Display code randomly?

    If you only wanted them to see it once, you would either need to use a database and log some sort of user id. Alternatively you could use cookies but cookies can be manipulated or deleted in order to gain more points.

  10. #10
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Display code randomly?

    Quote Originally Posted by JamieWarren09 View Post
    [CODE]
    Im using the code above - But i keep getting a syntax error, Unexpected T_VARIABLE .

    Any idea why?
    What was the solution to that problem?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  11. #11
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Display code randomly?

    Quote Originally Posted by Nightwalker83 View Post
    What was the solution to that problem?
    If you read the code he posted you should quickly find out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width