PHP-MySQL : Not able to fetch multiple record from database into word file

标签 php mysql ms-word

我必须将记录从数据库下载到 word 文件中。但是我无法这样做,因为每当我在表格周围添加 while 循环时,它都会给我错误。

当我获取单个记录时,我的代码工作正常。 谁能给我提供获取多条记录的方法。

这是我的代码:

  <?php


   include('dbcon.php');

   if($_GET['id']!='')

$id=$_GET['id'];

    $party_name=mysql_query("select * FROM tb_party Where party_id='".$id."' ");
    $party_name_row=mysql_fetch_array($party_name);


    $sample_text1 = $party_name_row['party_name'];




        $party_details="select * from tb_party_rate_entry
                    where party_id='$id'";
                    $query_details=mysql_query($party_details);
                    $row_details=mysql_fetch_array($query_details);



                    $word_xmlns = "xmlns:o='urn:schemas-microsoft-com:office:office'
                    xmlns:w='urn:schemas-microsoft-com:office:word'
                    xmlns='http://www.w3.org/TR/REC-html40′";

                   $word_xml_settings = "<xml><w:WordDocument>
                   <w:View>Print</w:View><w:Zoom>100</w:Zoom></w:WordDocument></xml>";

                   $word_landscape_style = 
                   "@page {size:8.5in 11.0in; margin:0.5in 0.31in 0.42in 0.25in;
                    }  div.Section1{page:Section1;}";

                   $word_landscape_headinh="{font-size:15px; }";


                  $word_landscape_div_imf='
                 <img  src="http://silverevents.net/user_image/' .$sample_text1 . '"  
                       height="150" width="150"/>';



                     $word_landscape_div_start = "<div class='Section1′>";

                  $word_landscape_div_end = "</div>";

                  $content = '<html '.$word_xmlns.'>
        <head>
                     <title>Party Rate Report</title>'
                     .$word_xml_settings.'<style type="text/css">
        '.$word_landscape_style.' table,td 
                     {border:1px solid #FFFFFF; width:200px; color:#CCC;} </style>
        </head>
        <body>'.$word_landscape_div_start  . 
                    '<table style="width:500px; color:#000000; 
                    margin:0 0 50px 100px; font-family: Arial Black, Gadget, 
                    sans-serif; font-size:24px;"><tr><td> Party Rate Report </td>
                    </tr></table></br>

        <table style="width:700px;font-size:15px;font-family: 
                     Arial Black, Gadget, sans-serif;">

        <tr>

        <td width="180">
            Party Name : '.  $party_name_row['party_name'] . '
        </td>

        <td width="250">
        Address : '.  $party_name_row['party_address'] . '
        </td>

        <td width="150">
        Phone : '.   $party_name_row['phone_no']. '
        </td>
        </tr>


        </table><br/>   
        <table style="width:700px; color:#00000; margin:22px 0 0 0px; 
                     font- family:Helvetica, sans-serif, Verdana; 
                    font-size:15px;  border1px solid #666;">
        <tr>
        <td style=" border:1px solid #666; width:300px;"> Zone</td>
        <td style=" border:1px solid #666; width:300px;"> 
                     Delivery Mode          
                     </td>
        <td style=" border:1px solid #666; width:300px;"> Doc Type </td>
        <td style=" border:1px solid #666; width:300px;"> Weight slot </td>
        <td style=" border:1px solid #666; width:300px;"> Weight Unit </td>
        <td style=" border:1px solid #666; width:300px;"> Flat </td>
        <td style=" border:1px solid #666; width:300px;"> Price </td>
        </tr>

        <tr>
         <td style=" border:1px solid #666; width:300px;">' .
                     $row_details['zone_id']. '</td>
                   <td style=" border:1px solid #666; width:300px;">'.
                    $row_details['delivery_mode_id'].'
                   </td>
                 <td style=" border:1px solid #666; width:300px;">'. 
                  $row_details['doc_type_id'].'</td>    
                 <td style=" border:1px solid #666; width:300px;">'. 
                   $row_details['weight_slot_id'].'</td>    
                 <td style=" border:1px solid #666; width:300px;">'.
                 $row_details['weight_measure_id'].'</td>   
           <td style=" border:1px solid #666; width:300px;">'.
              $row_details['flat'].'</td>   
           <td style=" border:1px solid #666; width:300px;">'.   
            $row_details['price' ] . '</td>         
        </tr>


        </table></br>' . $word_xml_settings. $word_landscape_div_end.'
        </body>
        </html>';

 @header('Content-Type: application/msword');
 @header('Content-Length: '.strlen($content));
 @header('Content-disposition: inline; filename="Party Rate Report.doc"');
 echo $content;

  ?>

这里是我的数据库:

enter image description here

最佳答案

快速重新格式化,添加一个循环(并假设 id 是一个整数):-

<?php


    include('dbcon.php');

    if($_GET['id']!='')
    {
        $id=intval($_GET['id']);

        $party_name=mysql_query("select * FROM tb_party Where party_id=$id ");
        if ($party_name_row=mysql_fetch_array($party_name))
        {
            $sample_text1 = $party_name_row['party_name'];
        }

        $word_xmlns = "xmlns:o='urn:schemas-microsoft-com:office:office'
        xmlns:w='urn:schemas-microsoft-com:office:word'
        xmlns='http://www.w3.org/TR/REC-html40'";

        $word_xml_settings = "<xml><w:WordDocument>
        <w:View>Print</w:View><w:Zoom>100</w:Zoom></w:WordDocument></xml>";

        $word_landscape_style = 
        "@page {size:8.5in 11.0in; margin:0.5in 0.31in 0.42in 0.25in;
        }  div.Section1{page:Section1;}";

        $word_landscape_headinh="{font-size:15px; }";

        $word_landscape_div_imf='
        <img  src="http://silverevents.net/user_image/' .$sample_text1 . '"  
        height="150" width="150"/>';

        $word_landscape_div_start = "<div class='Section1'> ";

        $word_landscape_div_end = "</div>";

        $content = '<html '.$word_xmlns.'>
            <head>
                         <title>Party Rate Report</title>'
                         .$word_xml_settings.'<style type="text/css">
            '.$word_landscape_style.' table,td 
                         {border:1px solid #FFFFFF; width:200px; color:#CCC;} </style>
            </head>
            <body>'.$word_landscape_div_start  . 
                        '<table style="width:500px; color:#000000; 
                        margin:0 0 50px 100px; font-family: Arial Black, Gadget, 
                        sans-serif; font-size:24px;"><tr><td> Party Rate Report </td>
                        </tr></table></br>

            <table style="width:700px;font-size:15px;font-family: 
                         Arial Black, Gadget, sans-serif;">

            <tr>

            <td width="180">
                Party Name : '.  $party_name_row['party_name'] . '
            </td>

            <td width="250">
            Address : '.  $party_name_row['party_address'] . '
            </td>

            <td width="150">
            Phone : '.   $party_name_row['phone_no']. '
            </td>
            </tr>


            </table><br/>   
            <table style="width:700px; color:#00000; margin:22px 0 0 0px; 
                         font- family:Helvetica, sans-serif, Verdana; 
                        font-size:15px;  border1px solid #666;">
            <tr>
            <td style=" border:1px solid #666; width:300px;"> Zone</td>
            <td style=" border:1px solid #666; width:300px;"> 
                         Delivery Mode          
                         </td>
            <td style=" border:1px solid #666; width:300px;"> Doc Type </td>
            <td style=" border:1px solid #666; width:300px;"> Weight slot </td>
            <td style=" border:1px solid #666; width:300px;"> Weight Unit </td>
            <td style=" border:1px solid #666; width:300px;"> Flat </td>
            <td style=" border:1px solid #666; width:300px;"> Price </td>
            </tr>';


        $party_details="select * 
                        from tb_party_rate_entry
                        where party_id=$id";
        $query_details=mysql_query($party_details);
        while($row_details=mysql_fetch_array($query_details))
        {
            $content .= '<tr>
             <td style=" border:1px solid #666; width:300px;">' .
                         $row_details['zone_id']. '</td>
                       <td style=" border:1px solid #666; width:300px;">'.
                        $row_details['delivery_mode_id'].'
                       </td>
                     <td style=" border:1px solid #666; width:300px;">'. 
                      $row_details['doc_type_id'].'</td>    
                     <td style=" border:1px solid #666; width:300px;">'. 
                       $row_details['weight_slot_id'].'</td>    
                     <td style=" border:1px solid #666; width:300px;">'.
                     $row_details['weight_measure_id'].'</td>   
               <td style=" border:1px solid #666; width:300px;">'.
                  (($row_details['flat'] != '') ? $row_details['flat'] : ' ').'</td>   
               <td style=" border:1px solid #666; width:300px;">'.   
                $row_details['price' ] . '</td>         
            </tr>';


        }
        $content .=     '</table></br>' . $word_xml_settings. $word_landscape_div_end.'
            </body>
            </html>';

        @header('Content-Type: application/msword');
        @header('Content-Length: '.strlen($content));
        @header('Content-disposition: inline; filename="Party Rate Report.doc"');
        echo $content;
    }

  ?>

注意新代码,您可能应该切换到使用 mysqli 而不是 php mysql 驱动程序

关于PHP-MySQL : Not able to fetch multiple record from database into word file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20652287/

相关文章:

asp.net - 我可以在我的 html 文件中放置什么元标记,以便它自动在 Microsoft Word 中打开该页面?

documentation - 有没有办法同步不同 Word 文档的部分?

vba - 获取VBA中的子目录列表

javascript - 如何正确地从我的 Controller 返回注释并在 .done() AJAX 函数中使用它?

php - 在 php 中查找丢失的日期

mysql - 如何从使用聚合函数分组的多个表中返回多列

"SELECT * FROM System"的 MySQL 语法错误 1064

javascript - 如何使用 PHP 和 jQuery 发送安全的 AJAX 请求

php - 如何将数组插入数据库

sql - 分布式数据库解决方案?