PHP/mysql help need

kkvsam

Senior Member
Joined
Oct 11, 2009
Messages
938
Reaction score
570
I just need to rotate the content for each user visit. need only 10-15 lines. Does any one know how to do this. I can get the content form excel or txt file.
 
Rotate? Are you referring to a marquee, or a random selection from the text file?
 
how do you want to rotate?

is there line read randomly from text file?
 
The below will random from a text file using a new line as the delimiter.
It will make it so there are no duplicates, and will output the results with an html line break.

PHP:
<?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;

?>

The result will look like the following:

Code:
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
 
Last edited:
This version is a bit more lightweight:
PHP:
<?php
$file = "file.txt";
$ah = fopen($file, 'r');
$contents = fread($ah, filesize($file));
$lines = explode("\n",$contents);
echo $lines[array_rand($lines)]."<br />";
?>
 
more short lines :)
PHP:
$file = file("file.txt");
$count = count($file);
echo $file[rand(0,$count-1)];
 
more short lines:
Code:
$file = file("file.txt");
echo $file[rand(0,count($file)-1)];
 
Now heres something that will rotate your content, if you have multiple files, it doesn't matter what they are named, it will choose one randomly. You need to change $dir variable to your directory.

PHP:
<?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;

?>
 
@All the above, if you read the title:
"PHP/mysql help need"

The fact that he also needs mysql helps, to me implies that the data is stored in a database, not in a text file.

Code:
<?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();
?>

Dont know if it works, but it should.
Wrote this in 3 minutes exact, so havent tested it.

Better version:
Code:
<?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();
?>
 
Last edited:
@All the above, if you read the title:
"PHP/mysql help need"

Seems that you didn't read the post that good, he clearly says:
"I can get the content form excel or txt file."
 
Back
Top