PDA

View Full Version : Splitting Large Files Into Smaller Files


aerojad
01-13-2004, 10:07 PM
Let's say I have one huge file with lots of links - 1 link per line. Instead of displaying it on one page, which causes lots of bandwidth to be used, I'd like to split it up on a number of pages. So instead of using a simple <?php include("../php_includes/exp_09.php"); ?> is there any way to define which line numbers of that file you would like to be extracted?

Viper007Bond
01-14-2004, 04:04 AM
Personally, I'd use MySQL and then this tutorial:

http://www.phpfreaks.com/tutorials/43/0.php

or

http://www.phpfreaks.com/tutorials/73/0.php

aerojad
01-14-2004, 04:11 AM
Thank you very much. Looks like I'll be learning things too :)

Viper007Bond
01-14-2004, 04:21 AM
MySQL is VERY handy. :)

You just make your page return results from the database (sort them by anything you want) and you're done.

Then when you want to add another link, just do it to the database and it will appear on your page. :)

If you need any help with MySQL, feel free to come back, or..

http://www.phpfreaks.com/tutorials/33/0.php

Tons more at:

http://www.phpfreaks.com/tutorial_index.php

TomD
01-14-2004, 11:55 AM
While I would agree that a MySQL answer is probably best, if your file is fairly static you could just open and read it a line at a time and display the lines you want using a variety of PHP functions.

Viper007Bond
01-14-2004, 10:00 PM
Yeah, I didn't think of that until later. :)

But then again, if it was static, he could just manually split it up. ;)

aerojad
01-15-2004, 09:27 PM
Originally posted by TomD
While I would agree that a MySQL answer is probably best, if your file is fairly static you could just open and read it a line at a time and display the lines you want using a variety of PHP functions.

Yeah, how would I do that instead? It's static, only changes about once a month. The SQL bit is making my head spin :/

aerojad
01-15-2004, 11:15 PM
Okay, I'm giving this SQL bit another shot using this tutorial: http://www.phpfreaks.com/tutorials/73/0.php

So far I've made the db.php... <? // Create database connection and select database mysql_select_db('theaero_expressions', mysql_pconnect('localhost','theaero_aerojad','******')) or die (mysql_error()); ?> and then a populate page: <?php include 'db.php'; for($i = 1; $i <= 408; $i++){ mysql_query("INSERT INTO pages (title) VALUES ('Item $i')"); echo "Item $i inserted into db <br />"; } ?> with a bunch of data below to enter, but when I go to run the populate.php, I get this: Access denied for user: 'theaero_aerojad@localhost' to database 'theaero_expressions'

:(

TomD
01-16-2004, 01:25 AM
Try:
$dbh=mysql_connect ("localhost", 'theaero_aerojad', "<PASSWORD HERE>")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ('theaero_expressions');Make sure you have assigned the user theaero_aerojad to the database theaero_expressions.

As for the rest try something like this:<?php
include 'db.php';

for($i = 1; $i <= count($Item); $i++){
$ThisItem = Item[$i];
$query = "INSERT INTO pages (title) VALUES ('$ThisItem')";
mysql_query($query)
or die('Error on query: ' . $query . ' with error: ' . mysql_error());
echo "$ThisItem inserted into db <br />";
}

?>I am guessing (hoping) that $Item is an array of what you are trying to load into the database. If not we will need to work on that.

aerojad
01-16-2004, 02:09 AM
Make sure you have assigned the user theaero_aerojad to the database theaero_expressions.

Hmm, that might be my stumbling block. How does one do that? I merely just created the user theaero_aerojad.

TomD
01-16-2004, 02:21 AM
Originally posted by aerojad
Hmm, that might be my stumbling block. How does one do that? I merely just created the user theaero_aerojad. It is all right there on the database and database user creation screen. Read the FAQ (http://support.hostrocket.com/index.php?page=index_v2&id=46&c=15) .

aerojad
01-16-2004, 02:26 AM
Talk about an answer staring at me in the face...

alright, on with the rest of this :)

aerojad
01-16-2004, 03:30 AM
Next holdup...

Alright, so now I got pages that say "item blah"... so now how do I get actual data in there, save from running a couple ten thousand insert queries? Everything I want to insert is already in a neat one-link-per-line format like so: http://www.theaerozone.com/php_includes/exp_09.php

TomD
01-16-2004, 03:58 AM
Do you have a file with a list of the links? Is exp_09.php reading a file?

TomD
01-16-2004, 04:16 AM
Once you have the links in a file with one link per line you can try the following to get them into the database: <?php
include 'db.php';

$FileName = '/home/username/public_html/pathtofile/link.txt';

if file_exists($FileName){
$Item = file($FileName);
} else {
echo "Fatal error. Could not find link file: $FileName";
exit();
}

for($i = 1; $i <= count($Item); $i++){
$ThisItem = Item[$i];
$query = "INSERT INTO pages (title) VALUES ('$ThisItem')";
mysql_query($query)
or die('Error on query: ' . $query . ' with error: ' . mysql_error());
echo "$ThisItem inserted into db <br />";
}

?>

Viper007Bond
01-16-2004, 10:10 AM
Make sure to check out this tutorial (http://www.phpfreaks.com/tutorials/29/0.php) - it will help you to set up a cron job that, at certain intervals (once a day or whatever), run the PHP file and generate a static HTML page off it since your page is rarely updated.

So instead of having [database queries in document] x [pageviews] = a lot of queries, you get just one page view per static page refresh. :)

(Only worry about this if you are going to have a lot of page views.)

aerojad
01-16-2004, 01:53 PM
I'd probably be better off instead of running a cron, just doing it myself... but I suppose once a week isn't a biggy.

And that might be a good idea as well because I'm surging past something like 20k page views/day