首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多个查询和数据数组编写PHP

使用多个查询和数据数组编写PHP
EN

Stack Overflow用户
提问于 2012-02-29 16:58:27
回答 1查看 1.5K关注 0票数 0

嘿,伙计们,我只是想让这个愚蠢的东西发挥作用,我可以从那里调整它,我从一个数组开始,它很可能会通过$_POST传输进来,

代码语言:javascript
复制
$cleanArray = array( array('type'=>'berries', 'numb'=>'5555'), 
                     array('type'=>'melons', 'numb'=>'686') );

然后,我声明了一些提供结构的数据数组(如果有更好的方法来完成这一特定步骤,请告诉我!),这是三种类型,总共有六种。查询是唯一的查询,每个类型都需要返回正确的信息,而大小是字段大小,用于pdf中的该项。

代码语言:javascript
复制
$dataShape = array(
    'berries'   => array(   'sizes' => array( 0 => 25, 1 => 20, 3 => 10, 4 => 15, 5 => 10, 6 => 20, 7 => 20, 8 => 10, 9 => 15, 10 => 10, 11 => 15, 12 => 10, 13 => 10, 14 => 10, 15 => 10, 16 => 10, 17 => 10, 18 => 10, 19 => 200 ),
                            'query' => array( 'shipper, po, commodity as comm, count, size, label, variety, pack_date AS date, grower_lot AS lot, CONCAT(color1, "-", color2) AS color, CONCAT(sizing1, "-", sizing2, " / ", "sizing3", "-", sizing4 ) AS size, CONCAT(firmness1, "-", firmness2) AS firmness, CONCAT(scars_count1, "-", scars_count2) AS scars_count, CONCAT(bruise_count2, "-", bruise_count2) AS bruise_count, CONCAT(decay_count1, "-", decay_count2) AS green, CONCAT(sugar_brix1, "-", sugar_brix2) brix, rating, comments')),
    'citrus'    => array(   'sizes' => array( 0 => 25, 1 => 20, 3 => 10, 4 => 15, 5 => 10, 6 => 20, 7 => 20, 8 => 10, 9 => 15, 10 => 10, 11 => 15, 12 => 10, 13 => 10, 14 => 10, 15 => 10, 16 => 10, 17 => 10, 18 => 10, 19 => 200 ),
                            'query' => array('shipper, po, commodity as comm, count, size, label, variety, pack_date AS date, grower_lot AS lot, CONCAT(color1, "-", color2) AS color, CONCAT(texture1, "-", texture2) AS texture, CONCAT(puff1, "-", puff2) AS puff, CONCAT(scar1, "-", scar2) AS scars, CONCAT(solidity1, "-", solidity2) AS solidity, CONCAT(green1, "-", green2) AS green, CONCAT(sugar_brix1, "-", sugar_brix2) brix, rating, comments')),
    'melons'    => array(   'sizes' => array( 0 => 25, 1 => 20, 3 => 10, 4 => 15, 5 => 10, 6 => 20, 7 => 20, 8 => 10, 9 => 15, 10 => 10, 11 => 15, 12 => 10, 13 => 10, 14 => 10, 15 => 10, 16 => 10, 17 => 10, 18 => 200 ),
                            'query' => array('shipper, po, commodity as comm, count, size, label, variety, pack_date AS date, grower_lot AS lot, CONCAT(color1, "-", color2) AS color, CONCAT(ground_color1, "-", ground_color2) AS ground_color, CONCAT(texture1, "-", texture2) AS texture, CONCAT(scar1, "-", scar2) AS scars, CONCAT(solidity1, "-", solidity2) AS solidity, CONCAT(cut1, "-", cut2) AS cut, CONCAT(sugar_brix1, "-", sugar_brix2) brix, rating, comments')),
);

然后,我尝试根据类型和类型的结构进行查询,它将将每个数组命名为type名称,并将每个部分转换为子数组。你可以在这里看到。

代码语言:javascript
复制
foreach($cleanArray as $key=>$val) { echo $key;
    $result[$cleanArray[$key]['type']]
    = $dbc->fetch_array("SELECT ".implode(',',$dataShape[ $cleanArray[$key]['type'] ]['query'])." FROM `"
                        .$cleanArray[$key]['type']."` WHERE (status = '0' OR status = '1') AND `report_key` = "
                        .$cleanArray[$key]['numb']);
}

然后,我尝试添加页面,并迭代一些内容,这是行不通的。它应该使用键一次获得列名,然后迭代出键值,直到花掉为止。如果值小于12行(总共24行,因为注释占用自己的行),则迭代空白行,直到有12行为止。

代码语言:javascript
复制
foreach($result as $fruitType=>$fruitTypeArray) {
  $nameKeys = array_keys($fruitTypeArray[0]);
  $i=0; $pdf->AddPage('L');  

  foreach($dataShape[$fruitType]['sizes'] as $dataShapevalue) { 
    $pdf->Cell($dataShapevalue6,ucwords(str_replace('_',' ',$nameKeys[$i++])),1,0,'L',1); 
  }

  // go to next row
  $y_axis = $y_axis + $row_height;

  foreach($dataShape[$fruitType]['sizes'] as $dataShapeValue) { 
  foreach($fruitTypeArray as $queryName=>$queryValue) { 
  if(!$queryName=='comments') {
    $pdf->Cell($dataShapeValue,6,$queryValue,1,0,'L',1);
  } else {

  // go to next row
  $y_axis = $y_axis + $row_height;

    $pdf->Cell($dataShapeValue,6,ucwords(str_replace('_',' ',$nameKeys[$i++])),1,0,'L',1); 

  // go to next row
  $y_axis = $y_axis + $row_height;
  }

 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-29 23:40:52

我不得不放弃并重写所有的东西,这是那些关心的人的代码。

代码语言:javascript
复制
<?php

    // temp array
    $cleanArray = array( array('type'=>'berries', 'numb'=>'55555'), 
                         array('type'=>'melons', 'numb'=>'686') );

require('class/pdfwriter/report.php');
require('class/dbconnect.class.php');
require('class/perms.class.php');

include('class/structure.php');

$dbc = Database::obtain(DB_HOST, DB_USER, DB_PASS, DB_NAME);

$pdf = new PDF('P','mm','A4');

    $pdf->AliasNbPages();
    $pdf->AddPage('L');
    $pdf->SetFillColor(255,255,0);
    $pdf->SetAutoPageBreak('margin','1');

    $pad = 277;

    $dbc->connect();

    foreach($cleanArray as $key) {
        $data[] = getSlice($key['type']); // returns types query and cellpad settings
    }

    $i=0;
    foreach($data as $key) {
        $product[$cleanArray[$i]['type']] = $dbc->fetch_array(" SELECT ".implode(',',$key['query'])." FROM `{$cleanArray[$i]['type']}` WHERE (status = '0' OR status = '1') AND `report_key` = {$cleanArray[$i++]['numb']}");
    }

    $current=$i=0;
    foreach($product as $key) {

        $empty = 15 - count($key);
        $margin = $pad / count($key[0])+1;
        $comment = $margin * (count($key[0])-1);
        $total = $margin * (count($key[0])-5);

        $pdf->SetFont('Helvetica','',15);
        $pdf->Cell(0,0,ucwords(str_replace('_',' ',$cleanArray[$i]['type'])),0,0,'C');
        $pdf->SetFont('Arial','B',7);
        $pdf->Ln(5);

    // column names
    foreach($key[0] as $name=>$value) {
            if($name!='comments') { 
                $pdf->Cell($margin,5,strtoupper(str_replace('_',' ',$name)),1,0,'C',1);
            } else { $pdf->Cell($comment,5,'',0,1,'L'); }
    }


    // row values
    $pdf->SetFont('Times','',7);
    foreach($key as $array) {
        foreach($array as $name=>$value) {
            if($name!='comments') { 
                $pdf->Cell($margin,5,$value,1,0,'C',0);
            } else { 
                $pdf->Cell(0,5,'',0,1,'L');
                $pdf->SetFont('Arial','B',7);
                $pdf->Cell($comment,5,'COMMENTS:',1,1,'L',0); 
                $pdf->SetFont('Times','',7);
            }
        }

    }

    while($empty) {
        $innerCount = count($key[0])-1;
        while($innerCount) { 
            $pdf->Cell($margin,5,'',1,0,'C',0);
            $innerCount--;
        }
            $pdf->Cell(0,5,'',0,1,'L');
            $pdf->SetFont('Arial','B',7);
            $pdf->Cell($comment,5,'COMMENTS:',1,1,'L',0); 
            $pdf->SetFont('Times','',7);
        $empty--;
    }

        $pdf->SetFont('Arial','B',7);
        $pdf->Cell($margin,5,'',0,0,'L',0);
        $pdf->Cell($margin,5,'',0,0,'L',0);
        $pdf->Cell($margin,6,'TOTAL',0,0,'C',0);
        $pdf->Cell($margin,5,'',1,0,'L',0);

        $pdf->Cell($total,5,'',1,1,'4',0);

        $pdf->Ln(3);

        $pdf->SetFont('Times','',9);
        $pdf->Cell($pad,5,'This report (including any attachments) contains confidential information for a specific individual and purpose. If you are not the intended recipient',0,1,'C',0);
        $pdf->Cell($pad,5,'you should delete this message and are hearby notified that any disclosure, copying, or distribution of this report is strictly prohibited',0,1,'C',0);

    if(++$current!=count($product)){$pdf->AddPage('L');}
    $i++;
}

$pdf->Output();

?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9503320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档