It's certainly good to move code into functions so that it can be easily reused but that has not impact on output buffering. Besides, I don't think output buffering is really going to help in this situation. Let's look at the issue step by step.
Problem: The page is taking to long to load.
Solution: Well, the first step to finding out how to solve this problem is finding out exactly where all the time is being spent. I think we can safely bet it's going to be in the query but never guess at optimisation. It usefull to remember a couple of old addages here, "premature optimisation is the root of all evil" and "assumtion is the mother of all f*** ups".
The process of finding out how long different parts of a program take is called profiling (there is another type of profiling called memory profiling but we won't go there now). There are a number of excellent tools available to help with profilling, most notably xdebug (in conjunction with wincachegrind or kcachegrind) which I strongly recomend you look into, however, xdebug would need to be installed and set up so I'll show you how to profile your program manually. First we're going to need a function for timing.
function timer( $name ) {
// static variable to store timers
static $timers = array();
if( !isset( $timers[ $name ] ) ) {
// if the timer's not been set then set it to the current microtime
$timers[ $name ] = array_sum( explode( ' ', microtime() ) );
} else {
// if it has been set then get the time difference
$time = array_sum( explode( ' ', microtime() ) ) - $timers[ $name ];
// unset the entry so the name can be reused
unset( $times[ $name ] );
// return the time difference
return $time;
}
}
then we're going to need to pick some points to time and add our calls to timer.
<?php
timer( 'whole page' );
$time_start = microtime(true);
// set some query limits
timer( 'build query' );
$sql_search = " WHERE `FUND`='{$_SESSION['fund']}'";
if(isset($_SESSION['local']) && $_SESSION['local'] != "0")
{
$sql_search .= " AND `LOCAL`='{$_SESSION['local']}'";
}
if(isset($_GET['searchSSN']) && $_GET['searchSSN'] != "")
{
$sql_search .= " AND `SSN` LIKE '%". str_replace('-', '', cleaninput($_GET['SSN'])) ."%'";
$_GET['LNAME'] = ""; // unset other search term
}
elseif(isset($_GET['LNAME']) && $_GET['LNAME'] != "")
{
$sql_search .= " AND `LASTNAME` LIKE '". cleaninput($_GET['LNAME']) ."%'";
$_GET['SSN'] = ""; // unset other search term
}
// append any sort order
$sql_sort = "";
if(isset($_GET['sort']) && $_GET['sort'] != "")
{
$sql_sort = " ORDER BY ". cleaninput($_GET['sort']) ." ASC";
$_GET['sort'] = "";
}
// get the start page
$sql_rowstart = "0";
if (isset($_GET['page']) && $_GET['page'] != "") {
$sql_rowstart = cleaninput($_GET['page'] * 30);
}
$times['build_query1'] = timer( 'build query' );
// get result count for paging
timer( 'run query' );
$result = mysql_query("SELECT COUNT(*) AS 'count' FROM `webextract` $sql_search");
$times['run_query1'] = timer( 'run query' );
timer( 'fetch query' );
$row = mysql_fetch_assoc($result);
$numrows = $row['count'];
if ($result == false || $row == false) {
exit('Error member reading table count.');
}
mysql_free_result($result);
$times['fetch_query1'] = timer( 'fetch query' );
// get row results
$sql = "SELECT $sql_terms FROM `mytable` $sql_search $sql_sort LIMIT $sql_rowstart, 30";
timer( 'run query' );
$result = mysql_query($sql);
$run_query2_time = timer( 'run query' );
if ($result == false) {
exit('Error member reading tables.');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<?php
timer( 'include header' );
include('../includes/header.php');
$times['include_header'] = timer( 'include header' );
?>
</head>
<body>
<div align="right"><?php
timer( 'show_page_links' );
show_page_links($numrows, $_GET['page']);
$times['show_page_links'] = timer( 'show_page_links' );
?></div>
<div><?php
timer( 'show_result_table' );
show_result_table($result);
$times['show_result_table'] = timer( 'show_result_table' );
?></div>
<?php
$times['whole page'] = timer( 'whole page' );
foreach( $times as $key => $value ) {
echo $key . '=>' . $value . " (" . (($value/$times['whole_page'])*100) . "%)<br />\n";
}
?>
Once we know exactly where the time is being spent we can take a closer look at solving the problem.