我正在用一个for填充一个数组,这将他发送给一个角色,她将接收并使用foreach我将数组的值分配给我的变量并打印,但是,只有值的第一个数组和其他节目被忽略。
此代码填充数组
$return_arr = array();
for ($i=0;$i<$cont-1;$i++)
{
/* the mysql code for insert multiple data*/
//The array
$row_array[$i]['idTicket'] = $idTicket;
$row_array[$i]['porcionSubtotal'] = $porcionSubtotal[$i];
$row_array[$i]['cajero'] = $cajero;
$row_array[$i]['porcionCant'] = $porcionCant[$i];
$row_array[$i]['tipoPago'] = $tipoPago;
$row_array[$i]['marca'] = $marca;
$row_array[$i]['descuento'] = $descuento;
$row_array[$i]['nombreProducto'] = $nombreProducto;
}
array_push($return_arr, $row_array);
crearTicketVenta($return_arr, $total);
我的pdf函数是
function crearTicketVenta($return_arr, $totalVenta){
$total = $totalVenta;
foreach($return_arr as $index => $value){
$nombre = $value[$index]['nombreProducto'] .'<br>';
$cantidad = $value[$index]['porcionCant'] .'<br>';
$importe = $value[$index]['porcionSubtotal'] .'<br>';
$subtotal = $value[$index]['porcionSubtotal'] .'<br>';
$idTicket = $value[$index]['idTicket'];
$cajero = $value[$index]['cajero'];
$porcionSubtotal = $value[$index]['porcionSubtotal'];
//unset($return_arr[$index + 1]);
}
}
并且var_dump数组是
array(1) { [0]=> array(2) { [0]=> array(8) { ["idTicket"]=> int(160) ["porcionSubtotal"]=> string(2) "21" ["cajero"]=> string(9) "undefined" ["porcionCant"]=> string(1) "1" ["tipoPago"]=> string(9) "undefined" ["marca"]=> string(5) "ADATA" ["descuento"]=> string(1) "%" ["nombreProducto"]=> string(7) "Carrito" } [1]=> array(8) { ["idTicket"]=> int(160) ["porcionSubtotal"]=> string(3) "135" ["cajero"]=> string(9) "undefined" ["porcionCant"]=> string(1) "1" ["tipoPago"]=> string(9) "undefined" ["marca"]=> string(12) "LIDeditorial" ["descuento"]=> string(1) "%" ["nombreProducto"]=> string(30) "Las cuatro Vidas de Steve Jobs" } } }
答案 0 :(得分:0)
仔细查看您的数据:
array(1) { [0]=> array(2) { [0]=> array(8) { ["idTicket"]=> int(160) ["porcionSubtotal"]=> string(2) "21" ["cajero"]=> string(9) "undefined" ["porcionCant"]=> string(1) "1" ["tipoPago"]=> string(9) "undefined" ["marca"]=> string(5) "ADATA" ["descuento"]=> string(1) "%" ["nombreProducto"]=> string(7) "Carrito" } [1]=> array(8) { ["idTicket"]=> int(160) ["porcionSubtotal"]=> string(3) "135" ["cajero"]=> string(9) "undefined" ["porcionCant"]=> string(1) "1" ["tipoPago"]=> string(9) "undefined" ["marca"]=> string(12) "LIDeditorial" ["descuento"]=> string(1) "%" ["nombreProducto"]=> string(30) "Las cuatro Vidas de Steve Jobs" } } }
array(1)
在显示数据时,您正在这样做
foreach($return_arr as $index => $value) {
$nombre = $value[$index]['nombreProducto'] .'<br>';
$cantidad = $value[$index]['porcionCant'] .'<br>';
$importe = $value[$index]['porcionSubtotal'] .'<br>';
$subtotal = $value[$index]['porcionSubtotal'] .'<br>';
$idTicket = $value[$index]['idTicket'];
$cajero = $value[$index]['cajero'];
$porcionSubtotal = $value[$index]['porcionSubtotal'];
//unset($return_arr[$index + 1]);
}
$return_arr
只有一个值,这就是foreach显示一个条目的原因。
用作:foreach($return_arr[0] as $index => $value) {
或更改
array_push($return_arr, $row_array);
至$return_arr = $row_array;
答案 1 :(得分:0)
我解决了这个问题,谢谢你的答案。
这是我的代码的一部分,这部分会生成一个数组。
for ($i=0;$i<$cont-1;$i++){
//the mysql query
//the array
$row_array[$i]['idTicket'] = $idTicket;
$row_array[$i]['porcionSubtotal'] = $porcionSubtotal[$i];
$row_array[$i]['cajero'] = $cajero;
$row_array[$i]['porcionCant'] = $porcionCant[$i];
$row_array[$i]['tipoPago'] = $tipoPago;
$row_array[$i]['marca'] = $marca;
$row_array[$i]['descuento'] = $descuento;
$row_array[$i]['nombreProducto'] = $nombreProducto;
}
//push the array
array_push($return_arr, $row_array);
//send array and value
crearTicketVenta($return_arr, $total);
这是我的完整代码,此代码生成PDF凭证。
<?php
function crearTicketVenta($return_arr, $totalVenta){
// Include the main TCPDF library (search for installation path).
require_once('includes/tcpdf/tcpdf.php');
//Define format date
date_default_timezone_set('America/Mexico_City');
$hoy = getdate();
$fechaActual= $hoy['mday'].'-'.$hoy['mon'].'-'.$hoy['year'];
$horaActual= $hoy['hours'].':'.$hoy['minutes'];
//Define variables
$total = $totalVenta;
$nombre = '';
$cantidad = '';
$importe = '';
$subtotal = '';
$idTicket = '';
$cajero = '';
$porcionSubtotal = '';
//The array
foreach($return_arr as $index => $value){
if(is_array($value)) {
foreach ($value as $key => $values) {
$nombre .= $values['nombreProducto'] .'<br>';
$cantidad .= $values['porcionCant'] .'<br>';
$importe .= $values['porcionSubtotal'] .'<br>';
$subtotal .= $values['porcionSubtotal'] .'<br>';
$idTicket = $values['idTicket'];//Don't repeat
$cajero = $values['cajero'];//Don't repeat
$porcionSubtotal .= $values['porcionSubtotal'];
}
}
}
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Manfred');
$pdf->SetTitle('Comprobante de compra');
$pdf->SetSubject('Comprobante de compra');
// remove default header/footer
//$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, "SOLARA", "No. de Ticket: ".$idTicket."\nLe atendio: ".$cajero);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', 'B', 20);
// add a page
$pdf->AddPage();
$pdf->SetFont('times', 'B', 16);
$pdf->MultiCell(0, 0, 'Comprobante de compra', 0, 'C', 0, 1, '', '', true, 0);
$pdf->Ln();
$pdf->SetFont('helvetica', '', 10);
//Define table
$tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="0" >
<thead>
<tr>
<td width="20%"; align="center">Cantidad </td>
<td width="40%"; align="center">Producto</td>
<td width="20%"; align="center">Importe</td>
<td width="20%"; align="center">Subtotal</td>
</tr>
</thead>
<tbody>
<tr>
<td width="20%"; align="center">$cantidad<br></td>
<td width="40%"; align="center">$nombre<br></td>
<td width="20%"; align="center">$importe<br></td>
<td width="20%"; align="center">$subtotal<br></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td width="20%"; align="center">Total<br>
$total
</td>
</tr>
</tbody>
</table>
<center><p align="center">Fecha y hora de compra:</p></center>
<center><p align="center">$fechaActual a las $horaActual</p></center>
<center><p align="center">Gracias por su preferencia</p></center>
EOD;
$pdf->writeHTML($tbl, true, false, false, false, '');
//Close and output PDF document
$pdf->Output('Ticket No - '.$idTicket.'.pdf', 'D');
//============================================================+
// END OF FILE
//============================================================+
}
?>