Forum Moderators: coopster

Message Too Old, No Replies

More efficient way of doing this

         

hawkerz

3:22 pm on Jul 13, 2007 (gmt 0)

10+ Year Member



Hey all,

I'm writing a php script that is opening a series of textfiles and pulling part numbers out of them. These pages are titled a1.php -> z999.php and index the part numbers. I need a more efficient way of doing this than opening the files over and over and over again, so any ideas are helpful.

<?php
// Get file information from config
include('config.php');
// pull header contents//
$header = file_get_contents("staticcontents.txt");
$header .= $page_init;
$filecontents = $header;
for ($i=0; $i<$num_files; $i++) {
// open file to get part numbers from
$handle = fopen($fname[$i], "rb");
// While not at the end of the file, read each line and increment the counter (counts the total # of lines)
while (!feof($handle)) {
fgets($handle);
$partnumcounter++;
}
// close the file
fclose($handle);
}
echo $partnumcounter . "\n";
$num_index_pages = round($partnumcounter / $parts_per_page); // Determine the number of pages required for these parts
$num_letters = round($num_index_pages / $pages_per_letter); // determine the number of letters that will be used based on the num of pages
// $startingval and $endingval are counter values to determine which part to read
$pcounter = 0;
for($i=0;$i<=$num_letters;$i++) {
echo $i;
for ($pagenum=1;$pagenum<=$pages_per_letter;$pagenum++) {
$filename = $letters[$i];
$filename .= $pagenum;
$filename .= ".php";
for($partcounter=1;$partcounter<=$endingval;$partcounter++) {
$c = 0;
for ($c=0; $c<$num_files; $c++) {
// open file to get part numbers from
$h2 = fopen($fname[$c], "rb");
while (!feof($h2)) {
$partnum = fgets($h2);
$partnumcounter++;
if ($partcounter == $startingval) { // we need to create (initialize) the table this way
$filecontents = $table_init;
$filecontents .= "<a class=normal href='../parts/" . rtrim($partnum) . ".html'>" . rtrim($partnum) . "</a><br>";
}
elseif ($partcounter >= $startingval && $partcounter <= $endingval) {
// echo "Wrote to File\n";
$val1 = $startingval;
$val2 = $startingval + $column_size; // manipulation to generate pages in columns
$val3 = $startingval + (2 * $column_size);
$val4 = $startingval + $column_size - 1;
$val5 = $startingval + (2 * $column_size) - 1;
$val6 = $startingval + (2 * $column_size) + $last_col - 1;
if ($partcounter == $val2 ¦¦ $partcounter == $val3) {
$filecontents .= "<a class=normal href='../parts/" . rtrim($partnum) . ".html'>" . rtrim($partnum) . "</a><br>";
}
elseif ($partcounter == $val4 ¦¦ $partcounter == $val5) {
$filecontents .= "<a class=normal href='../parts/" . rtrim($partnum) . ".html'>" . rtrim($partnum) . "</a><br>";
$filecontents .= "</td><td width=33% bgcolor=white align=left>";
}
elseif ($partcounter == $val6) {
$filecontents .= "<a class=normal href='../parts/" . rtrim($partnum) . ".html'>" . rtrim($partnum) . "</a><br>";
$filecontents .= "</td>";
}
else {
$filecontents .= "<a class=normal href='../parts/" . rtrim($partnum) . ".html'>" . rtrim($partnum) . "</a><br>";
}
}
}
// close the file
fclose($h2);
}
}
}
}
?>

dreamcatcher

9:31 am on Jul 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi hawkerz,

Did you have any success working out a better method? How about reading the contents into an array using file [php.net] instead of looping through each one?

dc

hawkerz

12:45 pm on Jul 17, 2007 (gmt 0)

10+ Year Member



The issue with reading into an array is that an array would not b e able to store as much data as is in the file, I think. What I ended up doing was cycling through all the files and parsing the contents into a new file, thereby reducing down to only one file. Still needs a bit of work, but it's getting there.