2016-11-25 4 views
0

MySQLからpdfへテーブルをエクスポートしようとしています。MySQLからPDFへのテーブルの生成(PHPを使用)

ここでの主な問題は、すべての結果を印刷するのではなく、最後の結果のみをリストに印刷することです。

私は他のコードを試しましたが、私はまだ同じ結果を得ています。私のコードで何が問題なのか分かりません。

<?php 
include("configsample.php"); 
?> 
<?php 

$string=$_GET['string']; 
$course=$_GET['course']; 
$category=$_GET['category']; 
$from=$_GET['from']; 
$to=$_GET['to']; 

if ($_REQUEST["string"]<>'') { 
$search_string = " AND restu_title LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%'"; 
} 
if ($_REQUEST["course"]<>'') { 
$search_course = " AND restu_course='".mysql_real_escape_string($_REQUEST["course"])."'"; 
} 
if ($_REQUEST["category"]<>'') { 
$search_category = " AND category='".mysql_real_escape_string($_REQUEST["category"])."'"; 
} 


if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'') { 
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year BETWEEN '".mysql_real_escape_string($_REQUEST["from"])."' AND '".mysql_real_escape_string($_REQUEST["to"])."'".$search_string.$search_course; 
} else if ($_REQUEST["from"]<>'') { 
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year = '".mysql_real_escape_string($_REQUEST["from"])."'".$search_string.$search_course; 
} else if ($_REQUEST["to"]<>'') { 
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year = '".mysql_real_escape_string($_REQUEST["to"])."'".$search_string.$search_course; 
} else { 
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_id>0".$search_string.$search_course.$search_category; 
} 
$html='<table> 
<tr style="color:#010101; font:bold 13px Times New Roman; height:40px;"> 
<th>Research Title</th> 
<th>Year</th> 
<th>Proponent(s)</th> 
<th>Adviser</th> 
<th>Research Panel</th> 
</tr>'; 
$sql_result = mysql_query ($sql, $connection) or die ('request "Could not execute SQL query" '.$sql); 
if (mysql_num_rows($sql_result)>0) { 
while ($row = mysql_fetch_assoc($sql_result)){ 
$html2='<tr> 
<td style="padding:10px;">'.$row['restu_title'].'</td> 
<td style="padding:10px;">'.$row['restu_year'].'</td> 
<td style="padding:10px;">'.$row['restu_by'].'</td> 
<td style="padding:10px;">'.$row['restu_ad'].'</td> 
<td style="padding:10px;">'.$row['restu_panel'].'</td> 
</tr> 
</table>'; 
} 
} 

include("mpdf60/mpdf.php"); 
$mpdf=new mPDF('c','Letter','','',20,20,18,16,9,9,'L'); 
$mpdf->SetDisplayMode('fullpage'); 
$mpdf->AddPage('L'); 
$mpdf->WriteHTML($html); 
$mpdf->WriteHTML($html2); 
$mpdf->Output(); 
exit; 
?> 
+0

あなたはwhile文での$ HTML2を上書きします。別のレコードがある限り、それはフェッチされ、html2は常に最後のレコードの値を含みます。 – Twinfriends

答えて

0

append $ html2あなたは最後のレコードだけを表示するようにループ内の値を割り当てています。

は次のように使用します。..

$html2=$html2.'<tr> 
    <td style="padding:10px;">'.$row['restu_title'].'</td> 
    <td style="padding:10px;">'.$row['restu_year'].'</td> 
    <td style="padding:10px;">'.$row['restu_by'].'</td> 
    <td style="padding:10px;">'.$row['restu_ad'].'</td> 
    <td style="padding:10px;">'.$row['restu_panel'].'</td> 
    </tr>'; 
+0

提案していただきありがとうございます。 "mPDFエラー:一部のデータはすでにブラウザに出力されていて、PDFファイルを送信できません: –

2

をあなたは次の値によって上書きされる$html2を避けるために、あなたのテキスト文字列をconcatenateする必要があります。

ループが実行されるたびに、$html2が新しい値で上書きされます。あなたのケースでは、あなただけの同じシンボルの前にドットを追加する必要があります。

while ($row = mysql_fetch_assoc($sql_result)){ 
    $html .='<tr> 
      <td style="padding:10px;">'.$row['restu_title'].'</td> 
      <td style="padding:10px;">'.$row['restu_year'].'</td> 
      <td style="padding:10px;">'.$row['restu_by'].'</td> 
      <td style="padding:10px;">'.$row['restu_ad'].'</td> 
      <td style="padding:10px;">'.$row['restu_panel'].'</td> 
    </tr>'; 
} 

は正しく</table>を配置するように注意してください。 SQLリクエストに結果がない場合、テーブルはクローズされず、HTMLが無効になります。

あなたはこのようにそれを実行する必要があります。

$html='<table> 
    <tr style="color:#010101; font:bold 13px Times New Roman; height:40px;"> 
    <th>Research Title</th> 
    <th>Year</th> 
    <th>Proponent(s)</th> 
    <th>Adviser</th> 
    <th>Research Panel</th> 
    </tr>'; 
$sql_result = mysql_query ($sql, $connection) or die ('request "Could not execute SQL query" '.$sql); 
if (mysql_num_rows($sql_result)>0) { 
    while ($row = mysql_fetch_assoc($sql_result)){ 
     $html .='<tr> 
      <td style="padding:10px;">'.$row['restu_title'].'</td> 
      <td style="padding:10px;">'.$row['restu_year'].'</td> 
      <td style="padding:10px;">'.$row['restu_by'].'</td> 
      <td style="padding:10px;">'.$row['restu_ad'].'</td> 
      <td style="padding:10px;">'.$row['restu_panel'].'</td> 
      </tr>'; 
    } 
} 
$html .= '</table>'; 
+0

提案していただきありがとうございます。コードを置き換えようとしましたが、このエラーが表示されます: "mPDFエラー:一部のデータはすでにブラウザに出力されており、PDFファイルを送信できません: –

+0

このメッセージは、$ mpdf->の前にvar_dumpまたは何らかのエラーがある場合に発生します。 pdfにエラーがないかどうかを確認せずに、var_dump($ html)を試してみてください。 – Benoti

関連する問題