I want make words change after user reload the website
Asked
Active
Viewed 97 times
1
-
you'll need an array of words and a random 'generator' like `rand()` – Jeff Jul 28 '18 at 09:27
-
Possible duplicate of [Getting a random value from a JavaScript array](https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array) – NonameSL Jul 28 '18 at 09:30
2 Answers
1
Server-side word's generator isn't the best idea. Use instead client-side. You can use server to get list of words or any other source of them. Below two samples for your project:
- first one based on array of words as a source
- second one based on string
// 1. Array
var words = ['One', 'Morning', 'Star'];
// 2. String
var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum euismod nisl a malesuada laoreet. Vivamus consequat, mi vel fermentum ultricies, dui libero ullamcorper velit, eu hendrerit lacus lorem a lacus. Suspendisse id diam quam. In tristique risus non turpis feugiat sagittis. Nam vitae turpis tincidunt leo faucibus ultrices. Suspendisse tristique pharetra ante, sit amet luctus odio. Pellentesque libero tortor, lacinia in mattis ut, ullamcorper a enim. Donec gravida, neque eget lacinia porta, quam ex rhoncus sem, ac semper ligula lacus non ligula. Vivamus id risus convallis, rhoncus nisi et, efficitur magna. In sit amet dolor ut dui accumsan dignissim. Curabitur lobortis nisi eu sapien accumsan luctus. Nullam blandit turpis ut massa ultricies cursus.";
var wordFromArr = words[Math.floor(Math.random() * words.length)];
var wordFromString = text.split(" ")[Math.floor(Math.random() * words.length)];
document.getElementById("output").innerText = wordFromArr;
<div id="output"></div>

Kas Elvirov
- 7,394
- 4
- 40
- 62
-
-
-
actually its worked xD (its code from net but yeah its worked) + i not have logs.txt in my website hoster – Jul 29 '18 at 12:16
0
$words = ["Bonjourr", "Holaa", "Hi! Mister", "Hello World", "Nice!"];
$word_count = count($words);
$selected_number = rand(0, ($word_count-1));
$selected_word = $words[$selected_number];
echo "{$selected_word}";
Let's try this

Hello World
- 2,673
- 7
- 28
- 60
-
-
actually its worked xD (its code from net but yeah its worked) + i not have logs.txt in my website hoster – Jul 29 '18 at 12:16