Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I want to create a php include for pages on my website and for it to call a RANDOM line from a text file .
This text file will have 250 one line comments in it.
Thanks Ian

sure, but the longer the list the better for you to use MySQL.
to get random lines from quotes.txt
<?php
// Open & Read
$opFile = fopen ("quotes.txt", "r");
$handle = fread ($opFile, filesize ("quotes.txt"));
fclose ($opFile);// Explode by lines
$lines = explode ("\n", $handle);// echo '<pre>'.print_r ($lines, TRUE).'</pre>';
// uncomment the above, to check that everything's correct// Shuffle
shuffle ($lines);
reset ($lines);$randomLine = $lines[0]; // ready for inclusion
?>I haven't tested the above so there could be some slight mistakes/typos.

Why's it better, surely accessing a database is slower than a text file?
Am thinking of about 250 lines MAX (If I'm lucky)
Ian

I think (I could be wrong) accessing an SQL server is faster than accessing a flatfile database or MS-Access typed database, _especially_ on large databases.
In your case above, you will first read the whole file content, puts them to array, shuffle it, then pick an array element. When you put those lines in MySQL, when you "ask for a single row" and have it "ordered by random", it'll be faster (at least that's what I have in mind :P)
Another sample. MS-Access typed databases (similar with flatfile databases) is slower than an SQL server. I'm not sure how to explain because it's also not very clear to me :D I read this on an ASP book that explains how SQL works in contrast with traditional "list". Maybe someone can explain better.

I think it's something like this:
Performance
With Access all tables involved in a form, report or a query are copied across the network from the server to the client's machine. The tables are then processed and filtered to generate the required recordset. For example if looking up details for one particular order from an orders table containing, say, 50,000 records then the whole table (all 50,000 records) is dragged over the network and then 49,999 of these records are thrown away (this is an over-simplification since indexing can be used to mitigate this to some extent). Contrast this with SQL Server where the filtering takes place on the server (if designed properly) and only 1 record is transmitted over the network.
This can affect performance in two ways. Firstly SQL Server is highly optimised and can usually perform the required filtering much more quickly than the client machine and secondly the amount of data sent across the network link is vastly reduced. For most databases the main performance bottleneck is data transmission over the network hence reducing this can give a really dramatic improvement in performance.
From here:
http://www.cypressinland.com/access_vs_sql.htm

I believe you can simplify the above code.
<?php
$quotes = file("quotes.txt");
$total = count($quotes);
srand(time());
$chosen = rand(0, $total);
echo($quotes[$chosen]);?>

Sorry to double-post, but I was just thinking. If you have PHP 5, it has built-in SQLite database support. This would be perfect for this use.
<?php
$db = sqlite_open("quotes.db", 0666, $db_error) or die($db_error);
$quotes = sqlite_query($db, "SELECT quote FROM quotes");$total = sqlite_num_rows($quotes) - 1;
$chosen = rand(0, $total);sqlite_seek($quotes, $chosen);
$quote = sqlite_fetch_single($quotes);echo($quote);
sqlite_close($db);?>
Just to show you it works, I have this running here:
http://jjcomputers.co.uk/test/quotes.php
You can also download the database file: quotes.db.

Thanks for all your tips and feedback I know I don't want to use ACCESS I stopped using that a long time back.
I came up with this:
<?
$textfile ="file.txt";
$quotes = file("$textfile");
$quote = rand(0, sizeof($quotes)-1);
echo $quotes[$quote]; ?>James, I'm on php (4.something)
Ian

Nice solution. But if you're still interested in using an SQLite database, you can get the SQLite package for PHP 4.x.

I knew they was a reason why I wanted a text file, my current host charges and extra £50 for sql....Will be changing next month!
Will try the sql versions then.
thanks everyone.
Ian

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |