mysql — phpexcel Упорядочить данные внутри переполнения стека

Код:

<?php require_once('Connections/valesilveira.php'); ?>
<?php
mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$colname_dados = "-1";
if (isset($_GET['id_proposta'])) {
$colname_dados = $_GET['id_proposta'];
}
mysql_select_db($database_valesilveira, $valesilveira);
$query_dados = sprintf("SELECT posicao, dados.desc, quantidade, unitario FROM propostas, clientes, dados WHERE propostas.id_proposta = dados.id_proposta AND propostas.id_cliente = clientes.id_cliente AND propostas.id_proposta = %s ORDER BY posicao ASC", GetSQLValueString($colname_dados, "int"));
$dados = mysql_query($query_dados, $valesilveira) or die(mysql_error());
$row_dados = mysql_fetch_assoc($dados);
$totalRows_dados = mysql_num_rows($dados);

$colname_propostas = "-1";
if (isset($_GET['id_proposta'])) {
$colname_propostas = $_GET['id_proposta'];
}
mysql_select_db($database_valesilveira, $valesilveira);
$query_propostas = sprintf("SELECT id_proposta, data FROM propostas WHERE id_proposta = %s", GetSQLValueString($colname_propostas, "int"));
$propostas = mysql_query($query_propostas, $valesilveira) or die(mysql_error());
$row_propostas = mysql_fetch_assoc($propostas);
$totalRows_propostas = mysql_num_rows($propostas);

$data = $row_propostas['data'];
setlocale (LC_ALL, "pt_BR", "ptb");
$data_formatada = strftime("%d de %B de %Y", strtotime($data));
?>
<?php// require the PHPExcel file
require 'Classes/PHPExcel.php';

// simple query

$headings = array('posicao','desc','quantidade','unitario','iliquido','total');

if ($result = mysql_query($query_dados) or die(mysql_error())) {
// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('List of Data');

$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}

// Loop through the result set
$rowNumber = 2;
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}// Freeze pane so that the heading line won't scroll
$objPHPExcel->getActiveSheet()->freezePane('A2');

// Save as an Excel BIFF (xls) file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xls"');
header('Cache-Control: max-age=0');

$objWriter->save('php://output');
exit();
}
echo 'a problem has occurred... no data retrieved from the database';
?>
<?php
mysql_free_result($dados);

mysql_free_result($propostas);

mysql_free_result($data);
?>

Хорошо, код, который я сделал до сих пор, но он только получает данные из базы данных и зацикливает их, и я хотел бы, чтобы последние 2 столбца приняли некоторые меры.

Я получаю данные для posicao, desc, quantidade, unitario, но для iliquido и итоговых данных я бы хотел сделать несколько расчетов.

Для iliquido я хотел бы сделать unitario * quantidade и показать все результаты в столбце.

И для всего я хотел бы сделать одну строку с суммой значений iliquido, таких как = sum (2ndrow: lastrow).

0

Решение

Вы можете просто вставить в качестве значения ячейки PHPExcel формулы, которые будут вести себя как обычные формулы Excel. Так что просто поставьте:

=A$row*C$row

в столбце вы предпочитаете получить результат unitario * quantidade для этой строки. То же самое для суммы.

Например, пока вы делаете этот цикл:

while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
//add this line to get the unitario*quantidade result
//in the last column
//here I'm assuming that quantitade and unitario are placed
//in column A and C in your excel, but this may be wrong, so
//replace them with the proper values
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,"=A$row*C$row");
$rowNumber++;
}
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector