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 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名