Computing.Net > Forums > Web Development > Is this possible with PHP

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Is this possible with PHP

Reply to Message Icon

Name: ian_ok
Date: July 17, 2005 at 04:02:10 Pacific
OS: win xp 98 & 2k
CPU/Ram: p3 512
Comment:

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

Venta Sanlucar Sales



Sponsored Link
Ads by Google

Response Number 1
Name: Laler
Date: July 17, 2005 at 04:13:23 Pacific
Reply:

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.

---
Site of the Day


0

Response Number 2
Name: ian_ok
Date: July 17, 2005 at 07:19:26 Pacific
Reply:

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

Venta Sanlucar Sales


0

Response Number 3
Name: Laler
Date: July 17, 2005 at 07:39:20 Pacific
Reply:

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.

---
Site of the Day


0

Response Number 4
Name: Laler
Date: July 17, 2005 at 07:46:56 Pacific
Reply:

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

---
Site of the Day


0

Response Number 5
Name: jam14online
Date: July 17, 2005 at 08:52:32 Pacific
Reply:

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]);

?>


0

Related Posts

See More



Response Number 6
Name: jam14online
Date: July 17, 2005 at 09:23:01 Pacific
Reply:

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.


0

Response Number 7
Name: ian_ok
Date: July 17, 2005 at 10:19:03 Pacific
Reply:

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

Venta Sanlucar Sales


0

Response Number 8
Name: jam14online
Date: July 17, 2005 at 11:07:03 Pacific
Reply:

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


0

Response Number 9
Name: ian_ok
Date: July 17, 2005 at 15:56:14 Pacific
Reply:

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

Venta Sanlucar Sales


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Is this possible with PHP

Is this really how PHP works?? www.computing.net/answers/webdevel/is-this-really-how-php-works/714.html

mail merge with php and mysql? www.computing.net/answers/webdevel/mail-merge-with-php-and-mysql/3975.html

Is this possible? www.computing.net/answers/webdevel/is-this-possible/750.html