michaelsacca Posted August 11, 2008 Share Posted August 11, 2008 Hello, I'm new to this board but I had a question about inserting a rotating php insert script where the script could post text information (say a name) that would change each time the page is refreshed. Link to comment https://forums.phpfreaks.com/topic/119231-rotating-php-insert-text/ Share on other sites More sharing options...
pocobueno1388 Posted August 11, 2008 Share Posted August 11, 2008 You could do this <?php $query = mysql_query("SELECT name FROM users ORDER BY RAND() LIMIT 1")or die(mysql_error()); $row = mysql_fetch_assoc($query); echo $row['user']; ?> Link to comment https://forums.phpfreaks.com/topic/119231-rotating-php-insert-text/#findComment-614108 Share on other sites More sharing options...
Fadion Posted August 12, 2008 Share Posted August 12, 2008 If you aren't aiming for a database approach (which in that case the above will work just great), u can use: Array Approach <?php $names = array('php', 'mysql', 'html', 'xml', 'javascript'); //the array that stores all the names $rand = rand(0, count($names) - 1); //generate a random index from 0 to the number of array items - 1 echo $names[$rand]; ?> or a file approach, supposing u have your text names comma separated in names.txt. The text file should look like: "php,mysql,html,xml,javascript" <?php $names = explode(',', file_get_contents('names.txt')); //get file content of 'names.txt' and explode the comma to pass all the names in an array $rand = rand(0, count($names) - 1); //generate a random index, same concept as the above snippet echo $names[$rand]; ?> Link to comment https://forums.phpfreaks.com/topic/119231-rotating-php-insert-text/#findComment-614320 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.