<?php
// input the filename
$filename = "random.txt";
// # of lines
$number_lines = 10;
/***** Do not edit below this line *****/
// read file and explode via new line
$file = getcwd()."/$filename";
$fp = fopen($file, 'rb');
$contents = fread($fp, filesize($file));
fclose($fp);
$cont_explode = preg_split('/\r\n|\r|\n/', $contents);
// if array <= # of lines, then just echo
if (count($cont_explode)-2 <= $number_lines) {
$result = $cont_explode;
} else {
$result = array();
// if array > # of lines, random
for($a=1; $a<=$number_lines; $a++){
// if less than 2, change rand
if (count($cont_explode) <= 2) {
$rand = mt_rand(0, count($cont_explode)-1);
// else keep rand the same
} else {
$rand = mt_rand(0, count($cont_explode)-2);
}
$result[] = $cont_explode[$rand];
unset($cont_explode[$rand]);
$cont_explode = array_merge($cont_explode);
}
}
// echo the results
$body= "";
foreach ($result as $val) {
$body .= $val."<br />";
}
$body = substr($body,0,-6);
echo $body;
?>
Harley<br />Carl<br />Pheona<br />Richard<br />Test<br />Parry<br />Amelia<br />Hamish<br />katy<br />Kane
OR
Harley
Carl
Pheona
Richard
Test
Parry
Amelia
Hamish
katy
Kane
<?php
$file = "file.txt";
$ah = fopen($file, 'r');
$contents = fread($ah, filesize($file));
$lines = explode("\n",$contents);
echo $lines[array_rand($lines)]."<br />";
?>
<?php
$dir = "myfiles/content/";
$files = array();
$i = 0;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file != ".." && $file != "." && substr($file, 0, 1) != ".") {
$files[$i] = $file;
$i++;
}
}
closedir($dh);
}
}
$rand = rand(0, count($files) - 1);
$file = fopen($files[$rand], "r");
$cont = fread($file, filesize($dir.$files[$rand]));
fclose($file);
echo $cont;
?>
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "pass";
$db_name = "databasename";
mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$content = mysql_query("SELECT `id` FROM content") or die(mysql_error());;
$count = mysql_num_rows($content);
if($count != 0){
$post = (int)rand(0,$count);
$content = mysql_query("SELECT * FROM content WHERE `id`='$post'");
print_r(mysql_fetch_array($content));
}else{
echo("There is no content to display.");
}
mysql_close();
?>
<?php
$db_host = "localhost";
$db_user = "databaseuser";
$db_pass = "databasepassword";
$db_name = "databasename";
mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$content = mysql_query("SELECT * FROM `content` ORDER BY RAND() LIMIT 0,1;") or die(mysql_error());;
$count = mysql_num_rows($content);
if($count != 0){
print_r(mysql_fetch_array($content));
}else{
echo("There is no content to display.");
}
mysql_close();
?>
@All the above, if you read the title:
"PHP/mysql help need"