[HELP!] Can a randomized ECHO output never change a second time? I need that!!

Discussion in 'PHP' started by Cyrus255, Oct 30, 2010.

  1. #1
    My goal is to say, put out a random quote on each page via a php echo, but NOT to have it change again when the page is reloaded (basically for it to be RANDOM yet still STATIC???). Is there any way to assign a random value to each page that DOESN'T change. This is for thousands of pages in one site so I can't do each individually. If there is, can you give me an example code?

    Thank you for your time in advance!! This is super important.
     
    Cyrus255, Oct 30, 2010 IP
  2. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #2
    Is it specific to each user? Or a set quote for each day?
     
    Grit., Oct 30, 2010 IP
  3. Blue Link Media

    Blue Link Media Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Use sessions.
     
    Blue Link Media, Oct 30, 2010 IP
  4. Coranti

    Coranti Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Are you able to use the filesystem or a database? Then you could store the session with a randomly generated quote and serve it only to that session...
     
    Coranti, Oct 30, 2010 IP
  5. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #5
    Yes to both, but unfamiliar with the process. I'd rather not use a sessionID though. The platform is wordpress if that helps. And I don't want it to change each day or for user. I can already do a "random" string, I just want it to NOT change on reload, to dish out the randomness only one time, permanently. Like how sometimes you see a page with a quote for each page that doesn't change, but I'd rather not set it manually on each page like other people seem to do (because it's thousands of pages).
     
    Cyrus255, Oct 30, 2010 IP
  6. Coranti

    Coranti Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You could also attach it to the IP address.

    You would need to create a table where you store the IP address and the ID/text of the quote and retreive it with each page load.
     
    Coranti, Oct 30, 2010 IP
  7. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #7
    That's not what I want to do. I want each page to display the same random quote to every user that sees THAT page and never change. Is there any way to set a random attribute that stays static instead of dynamically changing upon reload? that's the basic issue.
     
    Cyrus255, Oct 30, 2010 IP
  8. Coranti

    Coranti Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If it only is per page, then you could store in a table the ID of the post with the ID of the quote.
     
    Coranti, Oct 30, 2010 IP
  9. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #9
    Me no comprehende. I don't want to have to manually assign each. And I don't have a table of the post id's. I'd like this code to work on multiple sites. This shouldn't be that complicated. I sorta dislike that only new users seem to be using this thread to inflate their post count. Someone post a working code of what I'm asking for instead of non-answers. It's simple... php.

    <? 
     //Chooses a random number 
     $num = Rand (1,6); 
     //Based on the random number, gives a quote 
     switch ($num)
     {
     case 1:
     echo "Time is money";
     break;
     case 2:
     echo "An apple a day keeps the doctor away";
     break;
     case 3:
     echo "Elmo loves dorthy";
     break;
     case 4:
     echo "Off to see the wizard";
     break;
     case 5:
     echo "Tomorrow is another day";
     break;
     case 6:
     echo "PHP is cool!";
     }
     ?> 
    Code (markup):
    Like that, but so it doesn't change every time the page loads.
     
    Cyrus255, Oct 30, 2010 IP
  10. Coranti

    Coranti Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Don't need to be rude, I'll ignore it for now.

    You don't need to manually assign, if you script it to store the ID of the quote each time a page is loaded and retrieve it after each first load, it will go automagically.
     
    Coranti, Oct 30, 2010 IP
  11. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #11
    So how would I script it like that? All I need is so that it doesn't change on refresh... anybody? please help.
     
    Cyrus255, Nov 2, 2010 IP
  12. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #12
    Your logic itself is conflicting..
    You say you want to display a random string (1 out of 6).. but it has to be unique to each user (on every page) and forever...

    In this case Randomness does not have any effect.. Just assign a single string which you prefer to show it to all user, on all pages and forever.
    And yes you could place the code in your wordpress template file (as in posts template or sidebar etc...)
     
    krishmk, Nov 2, 2010 IP
  13. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #13
    NOT to each user. JUST unique PER page, it should show the SAME randomized output.

    AKA, page #1 has quote 2 (and displays the same for all users)
    page#2 has quote 4, page #3 has quote1, etc. etc.

    All randomized, but not to change upon refresh. PHP is not client side, but a server side script so it should be possible.
    The existing code works, but changes on refresh. I simply want it to not change on refresh and to be different on each page (but the same on each of those pages if viewed by a different person). I already found a partial solution myself (ppl don't answer if you don't understand code, I'm a novice but even I've been able to do part of the code myself) but it involves using srand and setting a time limit, but the quote is the same on every page which is why I want to find a way in php for it to generate a different code for each page.

    Let me explain it for dummies:

    Page #1: quote 3, never changes on refresh, or per user, but this was random output.
    Page #2: quote 5, never changes on refresh, or per user, but this was random output.

    etc. etc. get it now?

    Is there any sort of $page variable to specify the code separately for each page?
     
    Last edited: Nov 3, 2010
    Cyrus255, Nov 3, 2010 IP
  14. GNi33

    GNi33 Peon

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    and there is no way you want to solve this problem with a database? Because it would be quite easy like that
     
    GNi33, Nov 3, 2010 IP
  15. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #15
    Then give me an example code if it's so easy. If it works I'll use it. At this point I'll accept anything since nobody here has helped me yet. Instead they ask me questions instead of answering. :confused:
     
    Cyrus255, Nov 3, 2010 IP
  16. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #16
    In your database you have a table called 'Quotes' with 2 fields: 1 is some sort of text field with the actual quote (quotetext), the other is a number field with an autonumber attribute (quoteid). Every quote now has a number.

    Now you need to assign a unique number to every page that a quote will appear on--these numbers do not have to correspond to the numbers in the database. That means if you have 23 pages and 14 quotes, your page numbers should go 1-23 and not go 1-14 and restart at 1. Make the page numbers unique. On that page you should have something like this:

    $PAGENUM=17;
    PHP:
    Wherever you want your quote to appear on that page, place a function call like this:

    echo get_quote($PAGENUM);
    PHP:
    Next, build the get_quote function in a file that will be accessible by every page. This function should do a few things:

    1. Determine how many quotes are in the database. You can hard code this or use a COUNT/MAX query. Let's call this $QUOTES.

    2. Use that number to modulus the $PAGENUM value ( $QNUM=$PAGENUM % $QUOTES; ). This makes sure when you request a quote it is within range of an actual quote (i.e. if page #23 needs a quote and you only have 14 quotes, it doesn't go looking for one that isn't there, instead it looks for quote #9).

    3. Pull the quote using $QNUM and the 'LIMIT' keyword like so:

    $query="SELECT quotetext FROM Quotes ORDER BY quoteid ASC LIMIT ".$QNUM.", 1;" 
    PHP:
    4. Run the query and return the quote.

    That should do it. Now, just to head off any issues you may have with this not being 'random' enough; if it makes you feel any better you can run the $PAGENUM variable through srand() prior to or during step 2 like this:

    $PAGENUM=srand($PAGENUM);
    PHP:
    Just be sure to modulus it with $QUOTES afterward.
     
    plog, Nov 3, 2010 IP
    Cyrus255 likes this.
  17. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #17
    That will not work. As I explained this is THOUSANDS OF PAGES and it'd take forever to assign each individually (which is why I called it a randomized echo, not manually assigned). Anyways, I can't use a database for this project so let's just get that cleared as I've said before. It all has to be done in one go with one PHP script. I do appreciate your actually giving some code though, thanks Plog (gave you a rep).

    If there's any way to do this WITHOUT a database or manually assigning each page, I'm all ears. It would make me a very happy person. I'd accept ANY server-side language at this point even non PHP. PHP preferred, but ANY server side script that can do a randomized quote PER page that doesn't change on refresh, I'd be all for.
     
    Cyrus255, Nov 3, 2010 IP
  18. Blue Link Media

    Blue Link Media Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    config.php
    <?php
    
    function getQuote()
    {
    	$quote = "";
    	
    	$file = str_replace("/", "", $_SERVER['PHP_SELF']);
    	$quoteDir = "quotes/";
    	
    	$fileHandler = @fopen($quoteDir . $file . ".txt", "r") or die("Quote not available!");
    	
    	while (!feof($fileHandler))
    	{
    		$quote .= fgets($fileHandler);
    	}
    	
    	fclose($fileHandler);
    	return $quote;
    }
    	
    ?>
    PHP:
    whatever.php
    <?php
    
    include("config.php");
    
    echo getQuote();
    
    ?>
    PHP:
    The idea ?

    Directory quotes:
    whatever.php.txt
    something-else.php.txt
    public.php.txt
    ...
    Code (markup):
    If someone opens domain.com/whatever.php, it'll display a quote from quotes/whatever.php.txt, etc. :)

    ** Can't help you any further than this as you haven't said a word about the current location and format of your quotes.

    *** You said you can't edit all the files manually .. well, how would you add the rand function then ( if we assume it would do the job ) ? :confused:
     
    Last edited: Nov 3, 2010
    Blue Link Media, Nov 3, 2010 IP
  19. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #19
    You say that you don't want to assign each individual page a quote (or number), so how do you plan on getting each of these thousands of pages to display this quote? Do you have a template system you want to add a line of code into and it generates each and every page of the site? Or is it suppose to work in another manner? How does this yet to be written quote randomization code get called by each page?

    Also, clarify how the quote display is to work. Suppose I open up Page A of your site and Quote 7 appears on it. From what you've already stated, if I refresh Page A, then Quote 7 will reappear. What happens if:

    -2 days later I clear out my cache, and browse back to Page A, will Quote 7 appear or could it be a different one?

    -My 3 friends browse to Page A from their computers at the same time I am on it, will Quote 7 appear for them and all subsequent refreshes, or could it be any quote?

    -A month after that a 4th friend browses to Page A on his computer, will Quote 7 appear for him or could it be any quote?

    Essentially, is a quote always assigned to a certain page (from now until the end of the internet), or is it based on a user's visit to that page or possibly the day it is viewed?
     
    plog, Nov 3, 2010 IP
  20. backlinkneeded

    backlinkneeded Member

    Messages:
    285
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #20
    I am starting to like digital point. this is one of the good advice I got today. Thank you all for posting nice suggestions.
     
    backlinkneeded, Dec 22, 2010 IP