dsa122870 2014-04-09 22:42
浏览 33
已采纳

从mysql查询创建fpdf - 如何不向pdf添加空值

I have a php file that creates a pdf from a mysql query using fpdf. On the original input to the form is a dynamic drop down with four categories. Category 1 the form displays questions Categoryquestion1,Categoryquestion2, etc. and writes to the db. I am pulling one record at a time in the query and there will always be some blank fields (if they choose Category1 the fields for Category 2,3, etc will be blank. In my pdf, I show the question and the answer. If there a way to not show the question and answer if the value returned in the query is blank? Here is some code that might make it clearer: $result = mysql_query("SELECT title,abstr,presenters,keywords,comments,outcomes,CategorySelection,research3,research4,innovation3,innovation4,references,organization FROM database.table WHERE id='$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']; //if not blank $research3 =$data['research3']; //if not blank $research4 =$data['research4']; //if not blank $innovation3 =$data['innovation3']; //if not blank $innovation4 =$data['innovation4']; //if not blank $references =$data['references']; //if not blank $organization =$data['organization']; //if not blank $comments = $data['comments'];

 //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>

In this example, if the value for CategorySelection is research and those fields had contents, the question above and the data for Research3 an Research4 would go into the pdf. If they chose Innovation, the questions and answers for innovation3 and innovation4 would go in the pdf. In some cases, they don't even have to choose a Category, so it really needs to be based on empty fields returned in the query. I hope I explained this correctly.

  • 写回答

1条回答 默认 最新

  • douyan1927 2014-04-09 23:00
    关注

    I'm assuming that you do not want to leave blank lines where there is no data for a question.

    So, for example, if there is no answer for the question "Describe the planned innovation addressed in your paper. ", you want to skip over the question AND the answer without leaving a blank line. Do this:

    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);
    }
    

    The difference here is that I'm using Cell instead of MultiCell and Write.

    Write leaves the cursor at the end of the text, wherever that is. The last argument to the Cell method moves the cursor down and to the beginning of the next line, so you can continue writing there. In this way, FPDF will keep track of where to write the next text.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理