我有一个使用fpdf从mysql查询创建pdf的php文件。在表单的原始输入上有四个类别的动态下拉列表。类别1表单显示问题Categoryquestion1,Categoryquestion2等,并写入数据库。我一次在查询中提取一条记录,并且总是有一些空白字段(如果他们选择Category1,则Category 2,3等的字段将是空白。在我的pdf中,我将显示问题和答案。如果查询中返回的值为空,有没有办法不显示问题和答案?
这是一些可能更清楚的代码: $result = mysql_query("SELECT
title
,abstr
,presenters
,keywords
,comments
,outcomes
,CategorySelection
,research3
,research4
,innovation3
,innovation4
,references
,从organization
。database
哪里table
='$ id'LIMIT 1;“);
$ data = mysql_fetch_assoc($ result);
$ id = $ data ['id'];
$ title = $ data ['title'];
$ abstract = $ data ['abstr'];
$ presenters = $ data ['presenters'];
$ outcomes = $ data ['outcomes'];
$ keywords = $ data ['keywords'];
$ CategorySelection = $ data ['CategorySelection']; //如果不为空
$ research3 = $ data ['research3']; //如果不为空
$ research4 = $ data ['research4']; //如果不为空
$ innovation3 = $ data ['innovation3']; //如果不为空
$ innovation4 = $ data ['innovation4']; //如果不为空
$ references = $ data ['references']; //如果不为空
$ organization = $ data ['organization']; //如果不为空
$ comments = $ data ['comments']; id
//create pdf
require ('/opt/webapps/fpdf/fpdf.php');
$pdf=new FPDF('P', 'mm', 'Letter');
$pdf->AddPage('P');
$pdf->SetFont('Arial','B',12);
$pdf->Write(5,$title);
$pdf->SetXY(10,25);
$pdf->Cell(20,10,"Abstract:",10,10);
$pdf->SetFont('Arial','',12);
$pdf->Write(5,$abstract);
$pdf->SetXY(10,60);
$pdf->SetFont('Arial','B',12);
$pdf->Cell(20,20,"Presenters:",10,10);
$pdf->SetFont('Arial','',12);
$pdf->Write(5,$presenters);
$pdf->SetFont('Arial','B',12);
$pdf->Cell(20,10,"Outcomes:",10,10);
$pdf->SetFont('Arial','',12);
$pdf->Write(5,$outcomes);
$pdf->Cell(20,10,"Category: $CategorySelection",10,10);
$pdf->SetFont('Arial','B',12);
$pdf->MultiCell(180,6,"Indicate your teaching and learning project. ",10,10);
$pdf->SetFont('Arial','',12);
$pdf->Write(5,$research3);
$pdf->SetFont('Arial','B',12);
$pdf->MultiCell(180,6,"If your project involved a particular course, briefly describe the course, its students, and its place in the curriculum.",10,10);
$pdf->SetFont('Arial','',12);
$pdf->Write(5,$research4);
$pdf->SetFont('Arial','B',12);
$pdf->MultiCell(180,6,"Describe the planned innovation addressed in your paper. ",10,10);
$pdf->SetFont('Arial','',12);
$pdf->Write(5,$innovation3);
$pdf->SetFont('Arial','B',12);
$pdf->MultiCell(180,6,"If your innovation involves a particular course, briefly describe the course, its students, and its place in the curriculum.",10,10);
$pdf->SetFont('Arial','',12);
$pdf->Write(5,$innovation4Raw);
$pdf->SetFont('Arial','B',12);
$pdf->MultiCell(180,6,"How is your innovation different from ones that others have tried?",10,10);
$pdf->SetFont('Arial','',12);
$pdf->Write(5,$innovation5);
</code>
在此示例中,如果CategorySelection的值是research且这些字段具有内容,则上面的问题以及Research3和Research4的数据将进入pdf。如果他们选择了Innovation,则在pdf文件中会找到有关Innovation3和Innovation4的问题和答案。在某些情况下,他们甚至不必选择类别,因此它确实需要基于查询中返回的空字段。我希望我能正确解释。 最佳答案
我假设您不想在没有问题数据的地方留下空白行。
因此,例如,如果没有问题“描述论文中解决的计划中的创新”的答案,则您希望跳过该问题和答案而不留空白。做这个:
if($innovation3)
{
$pdf->SetFont('Arial','B',12);
$pdf->Cell(180,6,"Describe the planned innovation addressed in your paper. ",0,1);
$pdf->SetFont('Arial','',12);
$pdf->Cell(180,6,$innovation3, 0, 1);
}
此处的区别在于,我使用的是Cell
而不是MultiCell
和Write
。
Write
将光标留在文本的末尾,无论在哪里。 Cell
方法的最后一个参数将光标向下移动到下一行的开头,因此您可以在此处继续写入。这样,FPDF将跟踪下一个文本的编写位置。关于php - 从mysql查询创建fpdf-如何不向pdf添加空值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22975347/