Create statistics from random array ouput

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • keegan90
    New Member
    • Jun 2010
    • 2

    #1

    Create statistics from random array ouput

    Hi there, first post so hope it goes well :)

    Im trying to generate statistics on which quote will be displayed each time i refresh the page.

    I've stored 10 famous quotes in the $famous_quotes array but have no idea on how to find out which 1 has been selected and then to generate the stats for that quote (if its even possible!!!)

    Have been stuck on this problem for 4 days now!! So any help would be appreciated.

    This is the code used for storing and randomising my quotes.

    Code:
    $famous_quotes=array("Imagination is more important than knowledge - Albert Einstein", 
    "If music be the food of love, play on - Shakespeare", 
    "Never let your sense of morals get in the way of doing what's right - Isaac Asimov",
    "Obstacles are those frightful things you see when you take your eyes off the goal - Henry Ford",
    "When you come to a fork in the road, take it - Yogi Berra",
    "We may affirm absolutely that nothing great in the world has been accomplished without passion - Hegel",
    "The life which is unexamined is not worth living - Socrates",
    "Live as if you were to die tomorrow. Learn as if you were to live forever - M.K. Gandhi",
    "What you get by achieving your goals is not as important as what you become by achieving your goals - Zig Ziglar",
    "A lie gets halfway around the world before the truth has a chance to get its pants on - Sir Winston Churchill");
    
    print_r($famous_quotes);
    
    
    $random = rand(0, 9); 
    
    
    echo $famous_quotes[$random];

    Have tried things like

    Code:
    if ($random = 1)
    {
    	$a = 0;
    	$a++;
    }
    to try store which quote its printing out and then create stats using $a for first quote, $b for second etc etc...

    But nothing works!!!
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    So you want to store which quote is printed each time?
    Normal PHP variables won't allow you to do that; they are destroyed each time the script ends. To retain the data across multiple requests we typically use Sessions.

    You can create a session for each visitor to your page, which will store any data you need stored so it can be accessed by every PHP request the user makes. Note, however, that by default the session is lost when a user closes his browser! If you need to store the data longer, or have all visitors access the same array of data, you need to use other methods, like databases or simple a file on the server hard drive.

    The actually script could follow the following steps:
    1. Start the session, or connect to your database. In case of sessions, you simply need to call the session_start function.
    2. Generate a random number.
    3. Increment the value for that number in your session or database. For a session, you could simply create an array element for each number and add on to the random number.
      [code=php]
      // Create a random number
      $random_number = rand(0, 9);

      // Initialize the session field if it doesn't exists
      // already. If you don't, you will get a warning later
      // when you try to use it.
      if(!isset($_SES SION['stats'][$random_number])) {
      $_SESSION['rand_num'][$random_number] = 0;
      }

      // Increment to value for the random number.
      $_SESSION['stats'][$random_number] ++;[/code]
    4. Display the stats. You can do this like you do in your code, by using print_r or var_dump on the $_SESSION array.

    Comment

    • keegan90
      New Member
      • Jun 2010
      • 2

      #3
      Originally posted by Atli
      Hey.

      So you want to store which quote is printed each time?
      Normal PHP variables won't allow you to do that; they are destroyed each time the script ends. To retain the data across multiple requests we typically use Sessions.

      You can create a session for each visitor to your page, which will store any data you need stored so it can be accessed by every PHP request the user makes. Note, however, that by default the session is lost when a user closes his browser! If you need to store the data longer, or have all visitors access the same array of data, you need to use other methods, like databases or simple a file on the server hard drive.

      The actually script could follow the following steps:
      1. Start the session, or connect to your database. In case of sessions, you simply need to call the session_start function.
      2. Generate a random number.
      3. Increment the value for that number in your session or database. For a session, you could simply create an array element for each number and add on to the random number.
        [code=php]
        // Create a random number
        $random_number = rand(0, 9);

        // Initialize the session field if it doesn't exists
        // already. If you don't, you will get a warning later
        // when you try to use it.
        if(!isset($_SES SION['stats'][$random_number])) {
        $_SESSION['rand_num'][$random_number] = 0;
        }

        // Increment to value for the random number.
        $_SESSION['stats'][$random_number] ++;[/code]
      4. Display the stats. You can do this like you do in your code, by using print_r or var_dump on the $_SESSION array.
      Thanks heaps Atli, that worked perfectly

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Your welcome.
        Glad I could help :)

        Comment

        Working...