开发者

Export to excel from PHP, mysql (mutiple query in page),code not functioning correctly?

Hi below is the code I used to export data from php page to excel file.

$con = mysql_connect("localhost","root","testp");
mysql_select_db("dbone1", $con);

$ts = date("d/m/y : H:i:s", time()) ;

$datav=$_GET["datav"];
$pathogen=$_GET["pathogen"];
$topic1 = $_GET['toc1'];
$testvar1 = unserialize(rawurldecode($_GET['ind1']));
$subindg1 = $_GET['isg1'];
$topic2 = $_GET['toc2'];
$testvar2 = unserialize(rawurldecode($_GET['ind2']));
$subindg2 = $_GET['isg2'];
$topic3 = $_GET['toc3'];
$testvar3 = unserialize(rawurldecode($_GET['ind3']));
$subindg3 = $_GET['isg3'];
$topic4 = $_GET['toc4'];
$testvar4 = unserialize(rawurldecode($_GET['ind4']));
$subindg4 = $_GET['isg4'];
$topic5 = $_GET['toc5'];
$testvar5 = unserialize(rawurldecode($_GET['ind5']));
$subindg5 = $_GET['isg5'];
$testvar6 = unserialize(rawurldecode($_GET['cnty']));

function addWrapper (&$value, $key, $wrapper) {
$value = $wrapper.$value.$wrapper;
//no return, passed by reference
}

if(empty($testvar6))
{
}
else
{
array_walk($testvar6, 'addWrapper', "");
$sql_cntys = implode("','", $testvar6);
//echo $sql_cntys;
}

$num1 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic1' && Indicator='$ind1' && IndicatorSubGroup='$subindg1' && (Country IN ('$sql_cntys') OR  Location_Who IN ('$sql_cntys')) ");
$num2 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic2' && Indicator='$ind2' && IndicatorSubGroup='$subindg2' && (Country IN ('$sql_cntys') OR  Location_Who IN ('$sql_cntys')) ");
$num3 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic3' && Indicator='$ind3' && IndicatorSubGroup='$subindg3' && (Country IN ('$sql_cntys') OR  Location_Who IN ('$sql_cntys')) ");
$num4 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic4' && Indicator='$ind4' && IndicatorSubGroup='$subindg4' && (Country IN ('$sql_cntys') OR  Location_Who IN ('$sql_cntys')) ");
$num5 = mysql_query("SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic5' && Indicator='$ind5' && IndicatorSubGroup='$subindg5' && (Country IN ('$sql_cntys') OR  Location_Who IN ('$sql_cntys')) ");

$data = array();

while($row = mysql_fetch_assoc($num1))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $data[$c]['MidEstimate1'] = $row['MidEstimate'];
}
while($row = mysql_fetch_assoc($num2))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $开发者_运维技巧data[$c]['MidEstimate2'] = $row['MidEstimate'];
}

while($row = mysql_fetch_assoc($num3))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $data[$c]['MidEstimate3'] = $row['MidEstimate'];
}

while($row = mysql_fetch_assoc($num4))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $data[$c]['MidEstimate4'] = $row['MidEstimate'];
}

while($row = mysql_fetch_assoc($num5))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $data[$c]['MidEstimate5'] = $row['MidEstimate'];
}

require_once '/Classes/PHPExcel.php';

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Country')
            ->setCellValue('B1', 'MidEstimate1')
            ->setCellValue('C1', 'MidEstimate2')
            ->setCellValue('D1', 'MidEstimate3')
            ->setCellValue('E1', 'MidEstimate4')
            ->setCellValue('F1', 'MidEstimate5');

$r=2;

$i = 0;

foreach ($data as $row)
{
    ($i % 5);

            $a="A".$r;
        $b="B".$r;
        $c="C".$r;
        $d="D".$r;
        $e="E".$r;
        $f="F".$r;

        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue($a, $row['Country'])
                ->setCellValue($b, $row['MidEstimate1'])
                ->setCellValue($c, $row['MidEstimate2'])
                ->setCellValue($d, $row['MidEstimate3'])
                ->setCellValue($e, $row['MidEstimate4'])
                ->setCellValue($f, $row['MidEstimate5']);

        $r++;

}



$objPHPExcel->setActiveSheetIndex(0);

header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=$ts.xls");
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

while export, there is only header(column titles) and no data ? please help me to correct this code.


You've just dropped your complete code here, so I assume you don't have the faintest idea of where it's failing. Well, that's the most basic troubleshooting technique: isolating the part of the code that doesn't work as expected. In your case, the interesting points are:

1. SQL queries

First, while debugging you have to print the SQL code on screen and run it in your favourite MySQL client to make sure that it returns the data you want.

Second, you always have to check the return value of mysql_query() and take actions if they fail (for instance, log the mysql_error() and abort the script).

2. Variables

Use var_dump() to inspect your variables and make sure they have the information you think they have.

3. PHPExcel

Don't even try to create an Excel spreadsheet until you've populated $data successfully.


Edit: If you followed my advice and printed your SQL query:

var_dump(
    "SELECT * FROM mtable WHERE DataVersionDate='$datav' && Pathogen='$pathogen' && Topic='$topic1' && Indicator='$ind1' && IndicatorSubGroup='$subindg1' && (Country IN ('$sql_cntys') OR  Location_Who IN ('$sql_cntys')) "
);

You would already know whether $datav has the right value or not. If the value is not correct, then you know the issue is here:

$datav=$_GET["datav"];

Otherwise, you can forget about the URL. And so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜