Answer the question
In order to leave comments, you need to log in
How to output multiple lines randomly in php?
There is a phrase generator script. It includes three files:
1. comments.php - there are lines with phrases in it
2. index.php - phrase output on the page
3. script.php - random phrase generation
The bottom line is that on the page the phrase is generated randomly one at a time. How to output multiple lines randomly? Can use for or while or foreach condition?
script.php file
<?php
$famtxt = file('comments.php');
$famstr = $famtxt[ array_rand($famtxt) ];
header("Cache-Control: no-cache");
$prefixes = array($famstr);
$k = mt_rand (0, 99);
mt_srand (time()+(double)microtime()*$k*95987);
echo $prefixes[mt_rand(0,count($prefixes)-1)];
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/JavaScript">
$(document).ready(function(){
$("#generate").click(function(){
$("#quote p").load("script.php");
});
});
</script>
<div id="quote"><p>примерный текст</p></div>
<input type="submit" id="generate" value="Generate!"><br />
<?php
фраза 01
фраза02
фраза03
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question