Jump to content

How do you select a random word from an array so that its different each time?


jon121

Recommended Posts

basically im writing some content which is already coded to be randomly distributed onto different pages, but to help make the content abit more unique on each page, i want to be able to write piece of code that can select a different word from an array each time the vaiable is called.

 

i would make an array for each list of say adjectives, adverbs, nouns etc. so that within the content i can pull them in randomly.

 

for example, if my sentence was: "My name is $name1"

 

i would have an array something like this:

 

$name1 = array();

 

$name1[] = "John";

$name1[] = "Adam ";

$name1[] = "James ";

$name1[] = "Peter ";

$name1[] = "Sam ";

$name1[] = "Bob ";

 

but how do i incorporate the randomness into it, so that when im on different pages i get different results? so on 1 page i get "my name is John" but then on another i would get "my name is James".

(bad example coz i wouldnt be doing this with names but the principle is the same. itd be more like for decriptive words within a sentence, say like: best, greatest, most excellent etc.)

 

any ideas on this would be much appreciated. thanks

 

Link to comment
Share on other sites

yeah thats exactly wht i need thanks. works perfectly for what i asked for, but now i have another issue. is there a way of making it so tht once a word has been randomly selected on a page, each time tht particular page is loaded, its always that same word? so that if 10 people are looking at a certian page theyre all seeing the same content even though it was originally randomly put together. cheers

Link to comment
Share on other sites

This would be possible, but you'd need to be able to generate the page and save it somewhere for future uses. 

 

If you imagine 10 users on 10 different systems trying to access the same page, each time someone requested the page you would need to refer back to what the 1st user did.

 

As each page was called, the script would run and select another random selection. 

 

The easiest way to do this, would be to use a DB.

 

So the 1st user accesses the page and $random is set to John

 

You would then write this into a DB table with columns for the page and each random word.

 

page_id  |  word1  |  word2  |  word3  | ....................................................

 

You would then need to run a script in the following order each time the page was accessed

 

[*]Is there an entry for the page in the DB table?  If so, SELECT the relevant information

[*]If no entry for the page, run random function

[*]Once random function has been run, INSERT values into DB table for next view of page

 

Hope this makes sense

 

 

 

Link to comment
Share on other sites

I would opt for a database, especially if this will grow, but here is something simple:  http://php.net/manual/en/function.parse-ini-file.php

 

It would be easy to write a couple functions or a short little class that would make it comparable to ASP application variables, though I've never used them.

 

Well, I had forgotten that there isn't a built-in function to write the ini data back out, so an alternative might be (needs error checking, file creation etc...):

 

function getAppVar($name) {
   $vars = unserialize(file_get_contents('app.vars'));
   if(isset($vars[$name])) {
      return $vars[$name];
   } else {
      return false;
   }
}

function setAppVar($name, $value=null) {
   $vars = unserialize(file_get_contents('app.vars'));
   if($value === null) {
      unset($vars[$name]);
   } else {
      $vars[$name] = $value;
   }
   return file_put_contents(serialize($vars), 'app.vars');
}

You could also use json_encode()/decode.

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.