将页面输出打印到文件

时间:2017-02-17 15:44:44

标签: php html sql output

我有一个reviewfile.php,我根据我编码的条件阅读上传的文件并打印结果。

我想知道如何将我的页面结果放入文件中。 这样我以后可以下载pdf。

以下是我的代码

if(isset($_POST['reviewFile'])){

    if (isset($_POST['fileSelected'])){

        $SelFile = $_POST['fileSelected'];

        echo "<b>Filename:</b> $SelFile";

        $query=$con->prepare("SELECT * from file where file_name = '$SelFile'");
        $query->bind_result($file_id, $uid, $file_name, $file_size);
        $query->execute();
        $query->fetch();
        $query->close();

        $path = 'C:/xampp/htdocs/fwrule/fw/fileuploads/';
        $file_to_get = $path.$SelFile;

        $_SESSION['tmpfname'] = $file_to_get;
        $_SESSION['excelReader'] = PHPExcel_IOFactory::createReaderForFile($_SESSION['tmpfname']);
        $_SESSION['excelObj'] = $_SESSION['excelReader']->load($_SESSION['tmpfname']);

        $_SESSION['worksheet'] = $_SESSION['excelObj']->getSheet(0);
        $_SESSION['lastRow'] =  $_SESSION['worksheet']->getHighestRow();
        $_SESSION['highestColumn'] = $_SESSION['worksheet']->getHighestColumn();

        //loop through each row of the worksheet in turn
        for ($_SESSION['row'] = 3; $_SESSION['row'] <= $_SESSION['lastRow']; ++$_SESSION['row']) {

            //read a row of data into an array
            $rowData = $_SESSION['worksheet']->rangeToArray('A'.$_SESSION['row']. ':'.$_SESSION['highestColumn'] . $_SESSION['lastRow'], NULL, TRUE, FALSE);

            //insert rows into database
            $InsertData = $con->prepare("INSERT INTO file_data(id, file_id, no, name, source, destination, hit_counts, service, action, track, install_on, time, comment)
            VALUES('', '$file_id', '".$rowData[0][0]."', '".$rowData[0][1]."', '".$rowData[0][2]."', '".$rowData[0][3]."', '".$rowData[0][4]."', '".$rowData[0][5]."', '".$rowData[0][6]."', '".$rowData[0][7]."', '".$rowData[0][8]."', '".$rowData[0][9]."', '".$rowData[0][10]."')") or die($con->error);

            $InsertData->execute() or die ($InsertData->error);
            $InsertData->store_result();
            $InsertData->close();   
        }


        if ($DuplicatedRows = $con->prepare("SELECT id 
            from file_data a
            join (SELECT source, destination, hit_counts, service
            from file_data
            group by source, destination, hit_counts, service
            having count(*) > 1 ) b
            on a.source = b.source
            and a.destination = b.destination
            and a.hit_counts = b.hit_counts
            and a.service = b.service;"))
        {

            $DuplicatedRows->bind_result($id);
            $DuplicatedRows->execute();
            $DuplicatedRows->store_result();

            while($DuplicatedRows->fetch())
            {

            $rowNumforDuplicates = ($id+2);
            $info1 = "Rule is duplicated";

            echo "<tr>";
            echo "<td>".$rowNumforDuplicates."</td>";
            echo "<td>".$info1."</td>";
            echo "</tr>";

            }

            $numRow = $DuplicatedRows->num_rows;//Checking rows in database
            if ($numRow == 0)
            {
                echo "all perfect!";
            }
            //echo $numRow;
            $DuplicatedRows->free_result();
            $DuplicatedRows->close();
        }
        else{
                ?>
                <html>
                    <script>
                        window.alert("Error message:: %s\n", $con->error");
                    </script>
                </html>
                <?php
            }

        if ($checkforAny = $con->prepare("SELECT id FROM file_data WHERE (source collate latin1_swedish_ci = 'any') or (destination collate latin1_swedish_ci = 'any')"))

            {

                $checkforAny->bind_result($id);
                $checkforAny->execute();
                $checkforAny->store_result();

                while($checkforAny->fetch())
                {

                    //row number in excel as the file is reviewed from line 3 onwards, hence 2 is added to the ID value
                    $rowNumforAny = ($id+2);
                    $info2 = "Source or Destination contains 'Any'";

                    echo "<tr>";
                    echo "<td>".$rowNumforAny."</td>";
                    echo "<td>".$info2."</td>";
                    echo "</tr>";

                    //echo "<br>";
                    //echo "The one with Any: $rowNumforAny";
                }

                $checkforAny->free_result();
                $checkforAny->close();

            }
            else
            {   
                ?>
                <html>
                    <script>
                        window.alert("Error message:: %s\n", $con->error");
                    </script>
                </html>
                <?php
            }

        if ($checkforComments = $con->prepare("SELECT id FROM file_data WHERE comment = '' "))
        {
            $checkforComments->bind_result($id);
            $checkforComments->execute();
            $checkforComments->store_result();

            while($checkforComments->fetch())
            {

                $rowNumforComments = ($id+2);
                $info3 = "Comment for rule is missing";

                echo "<tr>";
                echo "<td>".$rowNumforComments."</td>";
                echo "<td>".$info3."</td>";
                echo "</tr>";

            }
            echo "</table>";
            //echo '<a href="http://pdfmyurl.com?url=www.mypage.com">Save results as PDF</a>';

            $checkforComments->free_result();
            $checkforComments->close();
        }

        else 
        {

            ?>
            <html>
                <script>
                    window.alert("Error message:: %s\n", $con->error");
                </script>
            </html>
            <?php
        }


        if($DuplicatedRows == true && $checkforAny == true && $checkforComments == true)
        {

            //Truncate table "file_data" after each review so that data will not overlap. 
            $delAllData = $con->prepare("TRUNCATE file_data");

            if ($delAllData->execute())
            {

            }
            $delAllData->close();
        }
        else 
            {
                ?>
                <html>
                    <script>
                        window.alert("Error message:: %s\n", $con->error");
                    </script>
                </html>
                <?php
            }

    }

    if (!isset($_POST['fileSelected']))
    {
        ?>
        <html>
            <script>
                window.alert("Please select a file to review!");
                window.location = 'https://localhost/fwrule/fw/filerecords.php';
            </script>
        </html>
        <?php
    }
}

1 个答案:

答案 0 :(得分:0)

您已经有查询可以找到存在问题的地方,那么为什么不使用NOT EXISTS ...

select fd1.id 
from file_data fd1
where not exists 
  (
  SELECT 1 FROM file_data fd2 WHERE (source collate latin1_swedish_ci = 'any' or destination collate latin1_swedish_ci ='any') and fd2.id = fd1.id
  )
and not exists
  (
  SELECT 1 FROM file_data fd3 WHERE comment = '' and fd3.id = fd1.id
  )
and not exists 
  (
  SELECT 1
  from file_data a
  join ( SELECT source, destination, hit_counts, service
      from file_data
      group by source, destination, hit_counts, service
      having count(*) > 1 ) b
    on a.source = b.source
  and a.destination = b.destination
  and a.hit_counts = b.hit_counts
  and a.service = b.service
  and a.id = fd1.id
  )