jon121 Posted July 12, 2011 Share Posted July 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241786-how-do-you-select-a-random-word-from-an-array-so-that-its-different-each-time/ Share on other sites More sharing options...
Nodral Posted July 12, 2011 Share Posted July 12, 2011 Hi You could use something along these lines <?php $random=rand(0, array_count($name1)); echo $name1[$random]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/241786-how-do-you-select-a-random-word-from-an-array-so-that-its-different-each-time/#findComment-1241748 Share on other sites More sharing options...
jon121 Posted July 12, 2011 Author Share Posted July 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241786-how-do-you-select-a-random-word-from-an-array-so-that-its-different-each-time/#findComment-1241754 Share on other sites More sharing options...
Nodral Posted July 12, 2011 Share Posted July 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241786-how-do-you-select-a-random-word-from-an-array-so-that-its-different-each-time/#findComment-1241759 Share on other sites More sharing options...
spiderwell Posted July 12, 2011 Share Posted July 12, 2011 its a real shame php doesnt have application variables like asp, that would have been ideal for this Quote Link to comment https://forums.phpfreaks.com/topic/241786-how-do-you-select-a-random-word-from-an-array-so-that-its-different-each-time/#findComment-1241792 Share on other sites More sharing options...
AbraCadaver Posted July 12, 2011 Share Posted July 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241786-how-do-you-select-a-random-word-from-an-array-so-that-its-different-each-time/#findComment-1241884 Share on other sites More sharing options...
AbraCadaver Posted July 12, 2011 Share Posted July 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241786-how-do-you-select-a-random-word-from-an-array-so-that-its-different-each-time/#findComment-1241892 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.