php报告很慢并且在firefox中崩溃了

时间:2012-01-10 12:17:07

标签: php firefox crash

我有一个运行并返回366条记录的报告,每条记录包含一个104 x 80像素的缩略图。问题是即使我增加了内存大小,报告运行速度也非常慢。

ini_set('memory_limit', '128M');
ini_set('max_execution_time','600');

编写SQL查询后,我在这里生成表项

generate_table_items($query_all_items);

然后运行并检查列中的图像

function generate_table_items($query){
        $columns = array();
        $resultset = array();

        $scriptname = array();
        $scriptname[0] = "/reports/all_items.php";
        $scriptname[1] = "/reports/all_items_by_value.php";
        $columncount = 0;

        $rowcost = 0;
        $rowsale = 0;

        while ($row = mssql_fetch_assoc($query)) {

            if (empty($columns)) {

                $columns = array_keys($row);
                echo '<tr><th scope="col" >'.implode('</th><th scope="col" >',get_column_name($columns)).'</th></tr>';
                $columncount = sizeof(array_keys($row));
            }
            $resultset[] = $row;

            echo '<tr><td>'.implode('</td><td>',report_image_check($row)).'</td></tr>';

            if(in_array($_SERVER['SCRIPT_NAME'],$scriptname)){
                $colspan = (count($columns)-2);
                echo "<tr><th scope='row'>Documents</th><td colspan='$colspan' >";
                    $PKID = $row['ID'];

                    if($row['SumOfTotalCost'] || $row['SumOfSalePrice']){
                        $rowcost += $row['SumOfTotalCost'];
                        $rowsale += $row['SumOfSalePrice'];
                        $get_total = true;
                    }

                    $query_docs = mssql_query("select documents.* from dbo.documents where documents.Antiquities_id = $PKID") or die ('get docs query failed ' . mssql_get_last_message());
                    while($row_docs = mssql_fetch_assoc($query_docs)){
                        $document = "../documents/" . $row_docs['document'];
                        echo "<a href='$document' title='opens in a new window' target='_blank' >" . $row_docs['document'] . "</a> | ";
                    } // End while (list docs)
                    mssql_free_result($query_docs);         
                echo "</td></tr>";
                myflush();
            } // End if all items and all items by value report

        } // End While
        echo '<tr>';
        for($i=0;$i < $columncount-4;$i++){
            echo '<td>&nbsp;</td>';
        }
        echo '<td>Total Cost '. $rowcost.'</td>';
        echo '<td>Total Sale '. $rowsale.'</td>';
        echo '<td>Total Calculated Difference '. ($rowsale-$rowcost).'</td></tr>';

} // End function

function get_column_name($columns){
$newcol = array();
$scriptname = array();
$scriptname[0] = "/reports/all_items.php";
$scriptname[1] = "/reports/all_items_by_value.php";
$thecount = 0;
foreach($columns as $col) {
    if($thecount == 1 && in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
        // Don't list the PK
    } else {
        $newcol[] = '<img id="'.$col.'" src="../images/icons/arrow_down.png" alt="click to sort by" onclick="sortby(\''.$col.'\');"  />' . $col;
    }
$thecount++;    
}

/*if(in_array($_SERVER['SCRIPT_NAME'],$scriptname)){
    $newcol[] = "documents";
}*/

return $newcol;
}

function report_image_check($row){
global $base_url, $uploaded_images_folder;
$newrow = array();
$imageext = array();

$imageext[0] = ".jpg";
$imageext[1] = ".png";
$imageext[2] = ".gif";
$imageext[3] = ".tiff";

$scriptname = array();
$scriptname[0] = "/reports/all_items.php";
$scriptname[1] = "/reports/all_items_by_value.php";
$PKID = 0;
$thecount = 0;

foreach($row as $rn) {

    if(in_array(strtolower(substr($rn,-4)),$imageext)){
        $small_img_ext = substr($rn,-4);
        $small_img = substr($rn,0,strripos($rn,"."));
        $small_img = $small_img . '_140_105' . $small_img_ext;
        $newrow[] = '<a href="' . $base_url . $uploaded_images_folder . '/' . $small_img . '" title="click to zoom on image" target="_blank" ><img src="' . $base_url . $uploaded_images_folder . '/' . $rn . '" alt="" width="50px" height="50px" /></a>';
    } elseif($thecount == 1 && in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
        $PKID = $rn;
    } elseif($thecount == 2 && in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
        $newrow[] = "<a href='../index.php?template=10&amp;PKID=$PKID' target='_blank' >$PKID (click to view)</a>";
    } else {
        $newrow[] = $rn;
    }

    $thecount++;
    myflush();
}

/*if (in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
    $newrow[] = "<a href='#&amp;PKID=$PKID' target='_blank' >Documents (click to view)</a>";
}*/

return $newrow;

} // End function

//// Flushing function
function myflush(){

ob_implicit_flush(); 
ignore_user_abort();
}

任何人都可以看到此代码的问题,或者看看它为什么需要这么长时间或为什么它会崩溃Firefox?打印到pdf功能会更好吗?

1 个答案:

答案 0 :(得分:0)

这需要很长时间,因为您正在嵌套SQL查询...为第一个查询返回的每个结果执行第二个SQL查询....使用JOIN执行单个查询应该有助于提高性能显著。

打印到PDF几乎肯定会慢一些:您需要自己编写大量代码才能在报告中正确定位所有内容,或者使用其中一个可以获取HTML并将其呈现为PDF的库(就像您一样) “目前已经生成了HTML,这将是额外的处理”