473,511 Members | 12,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create statistics from random array ouput

2 New Member
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.

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

Have tried things like

Expand|Select|Wrap|Line Numbers
  1. if ($random = 1)
  2. {
  3.     $a = 0;
  4.     $a++;
  5. }
  6.  
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!!!
Jun 7 '10 #1
3 2079
Atli
5,058 Recognized Expert Expert
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.
    Expand|Select|Wrap|Line Numbers
    1. // Create a random number
    2. $random_number = rand(0, 9);
    3.  
    4. // Initialize the session field if it doesn't exists
    5. // already. If you don't, you will get a warning later
    6. // when you try to use it.
    7. if(!isset($_SESSION['stats'][$random_number])) {
    8.     $_SESSION['rand_num'][$random_number] = 0;
    9. }
    10.  
    11. // Increment to value for the random number.
    12. $_SESSION['stats'][$random_number] ++;
  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.
Jun 7 '10 #2
keegan90
2 New Member
@Atli
Thanks heaps Atli, that worked perfectly
Jun 7 '10 #3
Atli
5,058 Recognized Expert Expert
Your welcome.
Glad I could help :)
Jun 7 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
30941
by: Angus Comber | last post by:
Hello I want to create a lookup table where I can store string keys: For example: 192.168.0.1 -> Purple 192.168.0.2 -> Blue 192.168.0.3 -> Red
6
24579
by: Christian Blackburn | last post by:
Hi Gang, I can't seem to figure out how to create an object array in VB.NET 2003. In VB6 you would just select an object copy and paste it and that was that. I don't know why in the heck they...
2
1953
by: mikeoley | last post by:
Ok I have a Javascript slideshow working. Every image is linked to a another page in the site. Problem is...The link wont refresh to the next link once the second images rollovers in the slideshow....
23
7366
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
5
2960
by: atxc212 | last post by:
Hi, I'm trying to create a 2d array of class location which has x and y members. The compiler keep giving me these 2 errors: error C2059: syntax error : '{' error C2334: unexpected token(s)...
1
1001
by: thaond | last post by:
Hi ! I want to create a Random number be in the Around, but not same the numbers created before ! Exam : My around : 0000,......,9999 Random number was created : 1234, 2134, 0001, 9998...
47
3959
by: Energizer100 | last post by:
import java.util; public class Compact { public static int randomArray (int n) { int a = new int; for (int i = 0; i < a.length; i++) { a = Math.random();
2
1312
by: readnlearn | last post by:
hii i hav a problem with thsi code check it once if (!this.IsPostBack) // Create a random code and store it in the Session object. this.Session = GenerateRandomCode(); else
3
1814
Kosal
by: Kosal | last post by:
Dear Sir/Madam I would like you to help to give me the example about create textbox like array but I want to create textbox depend on record in database if have 2 record textbox have 2 and display...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.