gpt4 book ai didi

php - 阿拉伯语检索数据

转载 作者:行者123 更新时间:2023-11-29 03:30:25 29 4
gpt4 key购买 nike

我正在尝试使用 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);


?>

我对用我的代码创建的表没意见。检索到的阿拉伯语数据如下?如何解决?

最佳答案

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

  1. 将您的数据库编码设置为 (utf8_general_ci)。进行备份不要忘记如果您更改数据库的字符集,所有旧的东西都会被销毁如果它是新的或空的,请执行此操作
  2. mysqli 连接后执行此操作 $db->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;
    }

但是,就像我告诉过你的那样,你做错了,使用 mysqli 并用完整的 OOP 来做,不是某个时间,而是其他时间不是!

关于php - 阿拉伯语检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31418844/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com