阿拉伯语检索数据

时间:2015-07-14 23:00:08

标签: php mysql pdf

我正在尝试使用TCPDF库来获得支持阿拉伯字母的PDF文件。我创建了一个表并从MYSQL数据库中获取数据。

 <?php

 // Include the main TCPDF library (search for installation path).
 require_once('TCPDF/config/tcpdf_config.php');

 require_once('TCPDF/tcpdf.php');


class PDF extends TCPDF{ 

var $tablewidths; 
 var $headerset; 
 var $footerset; 



 function morepagestable($lineheight=8) { 
// some things to set and 'remember' 
$l = $this->lMargin*2; 
$startheight = $h = $this->GetY(); 
$startpage = $currpage = $this->page; 

// calculate the whole width 
foreach($this->tablewidths as $width) { 
    $fullwidth += $width; 
} 

// Now let's start to write the table 
$row = 0; 
while($data=mysql_fetch_row($this->results)) { 
    $this->page = $currpage; 
    // write the horizontal borders 
    $this->Line($l,$h,$fullwidth+$l,$h); 
    // write the content and remember the height of the highest col 
    foreach($data as $col => $txt) { 

        $this->page = $currpage; 
        $this->SetXY($l,$h); 
        $this->MultiCell($this->tablewidths[$col],$lineheight,$txt,0,$this->colAlign[$col]); 

        $l += $this->tablewidths[$col]; 

        if($tmpheight[$row.'-'.$this->page] < $this->GetY()) { 
            $tmpheight[$row.'-'.$this->page] = $this->GetY(); 
        } 
        if($this->page > $maxpage) 
            $maxpage = $this->page; 
        unset($data[$col]); 
    } 
    // get the height we were in the last used page 
    $h = $tmpheight[$row.'-'.$maxpage]; 
    // set the "pointer" to the left margin 
    $l = $this->lMargin*2; 
    // set the $currpage to the last page 
    $currpage = $maxpage; 
    unset($datas[$row]); 
    $row++ ; 
} 
// draw the borders 
// we start adding a horizontal line on the last page 
$this->page = $maxpage; 
$this->Line($l,$h,$fullwidth+$l,$h); 
// now we start at the top of the document and walk down 
for($i = $startpage; $i <= $maxpage; $i++) { 
    $this->page = $i; 
    $l = $this->lMargin*2; 
    $t = ($i == $startpage) ? $startheight : $this->tMargin; 
    $lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin; 
    $this->Line($l,$t,$l,$lh); 
    foreach($this->tablewidths as $width) { 
        $l += $width; 
        $this->Line($l,$t,$l,$lh); 
    } 
} 
// set it to the last page, if not it'll cause some problems 
$this->page = $maxpage; 
} 



 connect($host='xxxx',$username='xxxx',$passwd='xxxx',$db='xxxxx') 
{ 
$this->conn = mysql_connect($host,$username,$passwd) or die( mysql_error() ); 
mysql_select_db($db,$this->conn) or die( mysql_error() ); 
return true; 
   } 

   function query($query){ 
$this->results = mysql_query($query,$this->conn); 
$this->numFields = mysql_num_fields($this->results); 
   } 

 function mysql_report($query,$dump=false,$attr=array()){

foreach($attr as $key=>$val){ 
    $this->$key = $val ; 
} 

$this->query($query); 

// if column widths not set 
if(!isset($this->tablewidths)){ 

    // starting col width 
    $this->sColWidth = (($this->w-$this->lMargin*2-$this->rMargin))/$this->numFields; 

    // loop through results header and set initial col widths/ titles/ alignment 
    // if a col title is less than the starting col width / reduce that column size 
    for($i=0;$i<$this->numFields;$i++){ 
        $stringWidth = $this->getstringwidth(mysql_field_name($this->results,$i)) + 8 ; 
        if( ($stringWidth) < $this->sColWidth){ 
            $colFits[$i] = $stringWidth ; 
            // set any column titles less than the start width to the column title width 
        } 
        $this->colTitles[$i] = mysql_field_name($this->results,$i) ; 
        switch (mysql_field_type($this->results,$i)){ 
            case 'int': 
                $this->colAlign[$i] = 'L'; 
                break; 
            default: 
                $this->colAlign[$i] = 'L'; 
        } 
    } 

    // loop through the data, any column whose contents is bigger that the col size is 
    // resized 
    while($row=mysql_fetch_row($this->results)){ 
        foreach($colFits as $key=>$val){ 
            $stringWidth = $this->getstringwidth($row[$key]) + 6 ; 
            if( ($stringWidth) > $this->sColWidth ){ 
                // any col where row is bigger than the start width is now discarded 
                unset($colFits[$key]); 
            }else{ 
                // if text is not bigger than the current column width setting enlarge the column 
                if( ($stringWidth) > $val ){ 
                    $colFits[$key] = ($stringWidth) ; 
                } 
            } 
        } 
    } 

    foreach($colFits as $key=>$val){ 
        // set fitted columns to smallest size 
        $this->tablewidths[$key] = $val; 
        // to work out how much (if any) space has been freed up 
        $totAlreadyFitted += $val; 
    } 

    $surplus = (sizeof($colFits)*$this->sColWidth) - ($totAlreadyFitted); 
    for($i=0;$i<$this->numFields;$i++){ 
        if(!in_array($i,array_keys($colFits))){ 
            $this->tablewidths[$i] = $this->sColWidth + ($surplus/(($this->numFields)-sizeof($colFits))); 
        } 
    } 

    ksort($this->tablewidths); 

    if($dump){ 
        Header('Content-type: text/plain'); 
        for($i=0;$i<$this->numFields;$i++){ 
            if(strlen(mysql_field_name($this->results,$i))>$flength){ 
                $flength = strlen(mysql_field_name($this->results,$i)); 
            } 
        } 
        switch($this->k){ 
            case 72/25.4: 
                $unit = 'millimeters'; 
                break; 
            case 72/2.54: 
                $unit = 'centimeters'; 
                break; 
            case 72: 
                $unit = 'inches'; 
                break; 
            default: 
                $unit = 'points'; 
        } 
        print "All measurements in $unit\n\n"; 
        for($i=0;$i<$this->numFields;$i++){ 
            printf("%-{$flength}s : %-10s : %10f\n", 
                mysql_field_name($this->results,$i), 
                mysql_field_type($this->results,$i), 
                $this->tablewidths[$i] ); 
        } 
        print "\n\n"; 
        print "\$pdf->tablewidths=\n\tarray(\n\t\t"; 
        for($i=0;$i<$this->numFields;$i++){ 
            ($i<($this->numFields-1)) ? 
            print $this->tablewidths[$i].", /* ".mysql_field_name($this->results,$i)." */ \n\t\t": 
            print $this->tablewidths[$i]." /* ".mysql_field_name($this->results,$i)." */\n\t\t"; 
        } 
        print "\n\t);\n"; 
        exit; 
    } 

} else { // end of if tablewidths not defined 

    for($i=0;$i<$this->numFields;$i++){ 
        $this->colTitles[$i] = mysql_field_name($this->results,$i) ; 
        switch (mysql_field_type($this->results,$i)){ 
            case 'int': 
                $this->colAlign[$i] = 'R'; 
                break; 
            default: 
                $this->colAlign[$i] = 'L'; 
        } 
    } 
} 

mysql_data_seek($this->results,0); 
$this->Open(); 
$this->setY($this->tMargin); 
$this->AddPage(); 
$this->morepagestable($this->FontSizePt); 
$this->Output(); 
 } 

 } 

   $pdf = new PDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-   8', false); 
   $pdf->SetFont('aealarabiya', '', 14, '', false);


 $pdf->connect('xxxxxxxxxx','xxxxxxxxxxxxxx','xxxxxxxxxxxxx','xxxxxxxxxxxxx'); 
  $attr=array('titleFontSize'=>24,'titleText'=>'THIS IS MY PDF FILE'); 
   $pdf->mysql_report("SELECT * FROM Student_Table",false,$attr); 


  ?>

我可以使用我的代码创建的表。检索到的阿拉伯数据如下:我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的问题非常简单,您需要选择正确的数据库编码并使用它进行查询,但在此之前,我认为我有责任告诉您您不必使用“mysql”你必须使用“mysqli”,现在让我们来解决问题:

  1. 将您的数据库编码设置为( utf8_general_ci )。备份不要忘记如果更改数据库的字符集将会销毁所有旧的东西如果它是新的或空的
  2. 在mysqli connect之后执行此操作 $ db-&gt; set_charset(“utf8”); 或者如果你仍然 想使用mysql使用这个 mysql_set_charset(“utf8”);

    以下是编辑过的代码:

        connect($host='xxxx',$username='xxxx',$passwd='xxxx',$db='xxxxx') 
            { 
         $this->conn = mysql_connect($host,$username,$passwd) or die( mysql_error() ); 
         mysql_select_db($db,$this->conn) or die( mysql_error() ); 
    
    
          mysql_set_charset("utf8");
    
         return true; 
         } 
    
  3. 但是,就像我告诉你的那样,你是以错误的方式做到的,使用mysqli并且在没有时间的情况下使用完整的OOP并且在其他时间没有!

相关问题