简体   繁体   中英

multidimentional array printing in a php class

There is a FPDF class and I am passing an array in the class with a function which name is productList .

$myclass->productList($table_rows);

The array has some rows from a MySql table

like

Array (
[0] => Array ( [id] => 170 [product] => 10211 [qty] => 1 [price] => 50 )
[1] => Array ( [id] => 171 [product] => 10211 [qty] => 1 [price] => 50 ) 1

I am using an extra function to resolve the array but it show only first row. Additionally It show table column name with data

eg id1,data1##somedata

My desired ouput is

1,#somedata,#some 2,#somedata,#some

Desired Output image:

所需的输出图像

Current Output image:

当前输出图像

First I am posting array print function

function makeNestedList(array $Array){
$w = array(90, 25, 20, 25,30);
foreach($Array as $Key => $Value){
    $Output = $Key;
    if(is_array($Value)){
        $Output .= $this->makeNestedList($Value);
    }else{
        $Output .= $Value;

    }
   //Product List
    $this->Cell($w[0],6,$Output,'LR',0,'L',1);
}

return $Output;
}

And the product list function which is responsible to print

function productList($pdata)
{
//Function to print product table 

// Product Table Header
$header = array('Product', 'Qty', 'Rate', 'Discount','Subtotal'); 
//cell width $w
$w = array(90, 25, 20, 25,30);
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
$this->Ln();

// Product Data  <===== Here is problem

$this->makeNestedList($pdata);

// Closing line and extra
$this->Cell(array_sum($w),0,'','T');
$this->Ln();
}

After tries of long hard methods I got this simple solution

//get $pdata
$this->productList($tablerows);


function productList($pdata)
{
and set a loop
foreach($table_rows as $key => $value)
{
$this->Cell($w[0],6,$value['product'],'LR',0,'L',$fill);
$this->Cell($w[1],6,$value['qty'],'LR',0,'C',$fill);
$this->Cell($w[2],6,$value['price'],'LR',0,'C',$fill);
$this->Cell($w[3],6,$value['discount'],'LR',0,'C',$fill);
$this->Cell($w[4],6,$value['subtotal'],'LR',0,'C',$fill);
$this->Ln();
$fill = !$fill;    
}

if ever you array data looks like this:

array (
            array ( 'product' => 10211, 'qty' => 1, 'rate' => 50, 'discount' => 50 , 'subtotal' => 50 ),
            array ( 'product' => 10212, 'qty' => 1, 'rate' => 100, 'discount' => 100 , 'subtotal' => 100 )
        );

This code may work for you. I saw that you concatenate all the value of the array into the $Output and put into one cell. You shouldn't do that if you wanted to produce the table.

function makeNestedList(array $Array){
$w = array(90, 25, 20, 25,30);
    for ($i=0; $i < count($Array) ; $i++) { 
    $ii = 0;
        foreach ($Array[$i] as $key => $value) {
            $this->Cell($w[$ii],6,$Output,'LR',0,'L',1);
            $ii++;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM