Export list CRUD with MPDF + OB_start() - php

Good morning guys, developing a CRM using the CodeIgniter framework and I'm not managing to export some CRUD lists using an MPDF library.
I'm trying to use an ob_start () function as indicated in the library's own manual, but when exporting, I get the following
formatting error as a return:
error formatting
When in fact a pdf spreadsheet would return
Below, the codes used:
view:
<li><i class="fas fa-download"></i></li>
<div class="conteudo tabelaMovCaixa">
<div class="col-xs-12 usuarioTitulo">
<div class="input entrada1">
<input type="checkbox" id="checkboxAll" onclick="selecionaTodosCheckbox()" value="Selecione">
</div>
<div class="conta">
Conta
</div>
<div class="descricao">
Descrição
</div>
<div class="data">
Vencimento
</div>
<div class="tipo">
Tipo
</div>
<div class="valor">
Valor
</div>
</div>
</form>
<form action="<?php echo base_url('configuracoes/excContasCheckbox') ?>" method="POST">
<?php if ($dados_caixa_filtro == null) { ?>
<?php
$data = date('d/m/Y');
$data2 = date('d/m/Y', strtotime('-1 days', strtotime(date('Y-m-d'))));
$data3 = date('d/m/Y', strtotime('-2 days', strtotime(date('Y-m-d'))));
?>
<div class="col-xs-12 vencimento">
<h5>Vencimento: <?php echo $data ?></h5>
</div>
<?php
if (!empty($dados_caixa)) {
foreach ($dados_caixa as $dados) {
$checkbox = [];
$checkbox[] = $dados->id_contas_pagar_receber;
foreach ($checkbox as $checkbox => $value) {
//echo "ID da conta : ".$value."<br />";
}
if (date("d/m/Y", strtotime($dados->vencimento)) == $data) {
$urlExcluir = base_url('configuracoes/excContaPR') . "/" . $dados->id_contas_pagar_receber;
if ($dados->tipo_pessoa == '1') {
echo "<div class='col-xs-12 usuario saida'>";
} else if ($dados->tipo_pessoa == '2') {
echo "<div class='col-xs-12 usuario entrada'>";
}
echo "<div class='input entrada1'>"
. "<input type='checkbox' name='emp_id[]' value='$value' id='emp_id' onclick=verificarCheckBox() >"
. "</div>"
. "<div class='conta'>"
. "$dados->nome"
. "</div>"
. "<div class='descricao'>"
. $dados->descricao
. "</div>"
. "<div class='data'>";
if ($dados->vencimento == '') {
echo "----------";
} else {
echo date('d/m/Y', strtotime($dados->vencimento));
}
echo "</div>"
. "<div class='tipo'>"
. $dados->forma_pagamento
. "</div>"
. "<div class='valor'>"
. "<span class='entrada2'>" . 'R$ ' . number_format($dados->valor, 2, ',', '.') . "</span>"
. "</div>"
. "<div class='actions2'>"
. "<li><a onclick='pagar2($dados->id_contas_pagar_receber, $dados->tipo_pessoa)' title='Editar'><i class='fas fa-pencil-alt'></i></a></li>"
. "<li><a onclick='pagar($dados->id_contas_pagar_receber, $dados->tipo_pessoa)' title='Pagar'><i class='fas fa-file-invoice-dollar'></i></a></li>"
. "<li><a href='#' onclick = \"confirmExclusaoContaPR(" . '\'0' . $dados->id_contas_pagar_receber . '\',' . '\'' . $urlExcluir . '\'' . ");\" title='Excluir'><i class='fa fa-trash'></i></a></li>"
. "</div>"
. "</div>";
for ($i = 0; $i < count(['checkbox']); $i++) {
$id = $value;
//echo $value;
}
}
}
}
?>
I am placing the ob_start () function right at the beginning of the view containing the HTML of the page.
Controler:
function gerarPdfRelatorio()
{
if ($this->session->userdata('logado') == true) {
date_default_timezone_set('America/Fortaleza');
setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');
$dadosC['dados_caixa'] = $this->financeiro->retornaListaMovimentacaoCaixa();
$dadosC['dados_caixa_filtro'] = null;
if ($this->session->userdata('tipo_filtro_movcaixa')) {
$this->session->unset_userdata("tipo_filtro_movcaixa");
}
$this->load->view('admin/head');
$dadosC['lista_cliente'] = $this->relatorio->buscaClienteFornecedor();
$dadosC['lista_centro_custo'] = $this->financeiro->listaCentroCusto();
$this->load->view('admin/head');
$this->load->view('admin/relatorio', $dadosC);
} else {
$dados['mensagem'] = null;
$dados['mensagem_tipo'] = 'error';
$this->paginas($dados);
}
}
This is where I generate the PDF:
<?php
set_time_limit(0);
include_once __DIR__ . '/../../../vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->debug = true;
$conteudoPDF = $this->load->view('admin/relatorios/relatorio_geral.php', '', true);
$header = '<div><img src="assets/img/logo_contrato2.jpeg" width="800"></div>';
$conteudoPDF = mb_convert_encoding($conteudoPDF, 'UTF-8', 'UTF-8');
$mpdf->WriteHTML($conteudoPDF);
ob_clean();
$mpdf->WriteHTML($conteudoPDF, 0);
ob_clean();
$mpdf->Output();
This is the list I'm trying to generate
tabela
I would like to know how I can format this data to appear in spreadsheet format in pdf

Related

add CCS style which depends on php IF statement inside mysqli_fetch_array

I want to add CCS style which depends on php IF statement. I have found something: changing the style inside "if" statement, but somehow it doesn`t work, maybe because of WHILE statement in my code.
<?php
$possible_lang_list = mysqli_query($con,
'SELECT * FROM page WHERE title = "lang" ORDER BY name1 ASC');
while ($row = mysqli_fetch_array($possible_lang_list)) {
echo '
<div class="language" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
';
}
?>
I want to display only div class="language" where $row['name2'] == $x, with possibility to display whole class "language" with JavaScript. Could anyone help?
I have tried:
while ($row = mysqli_fetch_array($possible_lang_list)) {
if ($row['name2'] == $x) {
?>
<div style="display:block;">
<?php } else {
?>
<div style="display:none;">
<?php
echo '
<div class="" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
</div>';
}
}
now it is working:
while ($row = mysqli_fetch_array($possible_lang_list)) {
if ($row['name2'] == $x) {
?>
<div style="display:block;">
<?php } else {
?>
<div style="display:none;">
<?php }
echo '
<div class="" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
</div>';
}
Try this code :
while ($row = mysqli_fetch_array($possible_lang_list))
{
if($row['name2'] == $x)
{
echo '<div class="language" id="' . $row['name2'] . '"><p>' . $row['name1'] . '</p>
</div>';
}
else
{
echo '<div class="" id="' . $row['name2'] . '"><p>' . $row['name1'] . '</p>
</div>';
}
}
Hope it will work
Assign a style element with a true/false ternary, like below. Then just output it!
<?php
while ($row = mysqli_fetch_array($possible_lang_list)) {
$class = ($row['name2'] == $x) ? 'language' : null;
echo '
<div class="'.$class.'" id="' . $row['name2'] . '">
<p>' . $row['name1'] . '</p>
</div>
';
}
And then:

Adding A Div For Every 4th MySQL Result With Pagination

I have a script that displays images from a database.
Every 4 pictures is contained in a <div class="row">.
I added a pagination script to this page to limit the number of images and I ran into a problem.
When there are 4 or 8 images on a page the script works fine, but if there are only 1 or 7 images on the page, the closing </div> for the <div class="row"> doesn't get added.
This is my entire script:
$conn = getConnected("lucycypher");
$img_start=0;
$img_limit=8;
if(isset($_GET['page'])) {
$page=$_GET['page'];
$img_start=($page-1)*$img_limit;
}
else { $page = 1; }
$img_total=mysqli_num_rows(mysqli_query($conn, "select * from gallery_img"));
$img_total_count=ceil($img_total/$img_limit);
echo '<nav aria-label="Page navigation">
<ul class="pagination">' . PHP_EOL;
if($page>1) {
echo '<li><span aria-hidden="true">Previous</span></li>' . PHP_EOL;
}
for($i=1;$i<=$img_total_count;$i++) {
if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
}
if($page!=$img_total_count) {
if(!isset($page)) { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
else { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
}
echo '</ul>
</nav>' . PHP_EOL;
?>
</div>
</div>
<?php
if($_COOKIE['age_verification'] == "adult") {
$img_query = "SELECT img_name, img_category, img_location FROM gallery_img order by img_time desc LIMIT $img_start, $img_limit;";
}
else if($_COOKIE['age_verification'] == "child") {
$img_query = "SELECT img_name, img_category, img_location FROM gallery_img WHERE img_category NOT LIKE '%nude' order by img_time desc LIMIT $img_start, $img_limit;";
}
$img_result = mysqli_query($conn, $img_query);
if (mysqli_num_rows($img_result) > 0) {
// output data of each row
$img_count = 1;
while($img_row = mysqli_fetch_assoc($img_result)) {
$tags = $img_row["img_category"];
if ( $img_count%4 === 1 ) { echo '<div class="row">' . PHP_EOL; } // Create new row for every 4th image
echo '<div class="col-md-3">' . PHP_EOL;
echo '<div class="panel panel-default">' . PHP_EOL;
echo '<img src="http://lucycypher.com/h/400/w/300/a/c/thumb/'.$img_row["img_location"].'" class="img-responsive img-protected">' . PHP_EOL;
echo '<div class="panel-footer"><span class="glyphicon glyphicon-tag"></span> Tags: ' . $tags . '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
if ( $img_count%4 === 0 ) { echo '</div>' . PHP_EOL; } // Close row
$img_count++;
}
} else {
echo '<div class="col-md-3">' . PHP_EOL;
echo '<div class="panel panel-primary">' . PHP_EOL;
echo '<div class="panel-heading"><span class="glyphicon glyphicon-picture"></span> Sorry</div>' . PHP_EOL;
echo 'No recent uploads.' . PHP_EOL;
echo '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
}
?>
<div class="row">
<div class="col-md-12">
<?php
echo '<nav aria-label="Page navigation">
<ul class="pagination">' . PHP_EOL;
if($page>1) {
echo '<li><span aria-hidden="true">Previous</span></li>' . PHP_EOL;
}
for($i=1;$i<=$img_total_count;$i++) {
if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
}
if($page!=$img_total_count) {
if(!isset($page)) { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
else { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
}
echo '</ul>
</nav>' . PHP_EOL;
Once I figure this out I'm going to limit the images to 8 per page so on the last page if there are only 3 total images then it's missing the final </div> tag since there isn't a total of 4 which completely throws off the rest of the page layout.
Best way to explain it is a visual I assume:
When images total 4 per row with a limit of 8:
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
</div>
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
</div>
But if the final page only has 3 images:
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<!--Missing </div> Tag-->
The final </div> tag is set by this line: if ( $img_count%4 === 0 ) { echo '</div>' . PHP_EOL; } // Close row.
You may calculate the total count of images, which will be uploaded.
Then, use if ( $img_count%4 === 0 || $img_count==$total_count )
In my example $img_count is the number of added image (beginning from 1)

Showing unknown characters when excel forced to download in PhpExcel codeigniter

I am using PhpExcel to download reports as excel in my codeigniter application. When I try to download my report its showing some characters on the browser instead of downloading the file. This has worked fine in localhost but not working in the server. I dont know whats the problem. Here is my code:
View
<div class="box-content">
<div class="row">
<div class="col-lg-12">
<p class="introtext"><?= lang('customize_report'); ?></p>
<div id="form">
<?php echo form_open("reports/getSalesaccReport"); ?>
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="reference_no"><?= lang("reference_no"); ?></label>
<?php echo form_input('reference_no', (isset($_POST['reference_no']) ? $_POST['reference_no'] : ""), 'class="form-control tip" id="reference_no"'); ?>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="user"><?= lang("created_by"); ?></label>
<?php
$us[""] = "";
foreach ($users as $user) {
$us[$user->id] = $user->first_name . " " . $user->last_name;
}
echo form_dropdown('user', $us, (isset($_POST['user']) ? $_POST['user'] : ""), 'class="form-control" id="user" data-placeholder="' . $this->lang->line("select") . " " . $this->lang->line("user") . '"');
?>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="customer"><?= lang("customer"); ?></label>
<?php echo form_input('customer', (isset($_POST['customer']) ? $_POST['customer'] : ""), 'class="form-control" id="customer" data-placeholder="' . $this->lang->line("select") . " " . $this->lang->line("customer") . '"'); ?>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="biller"><?= lang("biller"); ?></label>
<?php
$bl[""] = "";
foreach ($billers as $biller) {
$bl[$biller->id] = $biller->company != '-' ? $biller->company : $biller->name;
}
echo form_dropdown('biller', $bl, (isset($_POST['biller']) ? $_POST['biller'] : ""), 'class="form-control" id="biller" data-placeholder="' . $this->lang->line("select") . " " . $this->lang->line("biller") . '"');
?>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="warehouse"><?= lang("warehouse"); ?></label>
<?php
$wh[""] = "";
foreach ($warehouses as $warehouse) {
$wh[$warehouse->id] = $warehouse->name;
}
echo form_dropdown('warehouse', $wh, (isset($_POST['warehouse']) ? $_POST['warehouse'] : ""), 'class="form-control" id="warehouse" data-placeholder="' . $this->lang->line("select") . " " . $this->lang->line("warehouse") . '"');
?>
</div>
</div>
<?php if($this->Settings->product_serial) { ?>
<div class="col-sm-4">
<div class="form-group">
<?= lang('serial_no', 'serial'); ?>
<?= form_input('serial', '', 'class="form-control tip" id="serial"'); ?>
</div>
</div>
<?php } ?>
<div class="col-sm-4">
<div class="form-group">
<?= lang("start_date", "start_date"); ?>
<?php echo form_input('start_date', (isset($_POST['start_date']) ? $_POST['start_date'] : ""), 'class="form-control datetime" id="start_date"'); ?>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<?= lang("end_date", "end_date"); ?>
<?php echo form_input('end_date', (isset($_POST['end_date']) ? $_POST['end_date'] : ""), 'class="form-control datetime" id="end_date"'); ?>
</div>
</div>
</div>
<div class="form-group">
<div
class="controls"> <?php echo form_submit('submit_report', $this->lang->line("submit"), 'class="btn btn-primary"'); ?> </div>
</div>
<?php echo form_close(); ?>
</div>
Controller
function getSalesaccReport()
{
if ($this->input->post('customer')) {
$customerid = $this->input->post('customer');
} else {
$customerid = NULL;
}
echo $customerid;
if ($this->input->post('biller')) {
$biller = $this->input->post('biller');
} else {
$biller = NULL;
}
if ($this->input->post('warehouse')) {
$warehouse = $this->input->post('warehouse');
} else {
$warehouse = NULL;
}
if ($this->input->post('start_date')) {
$start_date = $this->input->post('start_date');
} else {
$start_date = NULL;
} echo $start_date;
if ($this->input->post('end_date')) {
$end_date = $this->input->post('end_date');
} else {
$end_date = NULL;
} echo $end_date;
if ($start_date) {
$start_date = $this->sma->fld($start_date);
$end_date = $this->sma->fld($end_date);
}
if (!$this->Owner && !$this->Admin) {
$user = $this->session->userdata('user_id');
}
$dateTmp = "DATE_FORMAT(".$this->db->dbprefix('sales').".date,'%m/%Y')";
$condition = "if(".$this->db->dbprefix('sales').".biller_id = 5, 'A', if(".$this->db->dbprefix('sales').".biller_id = 6, 'B',if(".$this->db-> dbprefix('sales').".biller_id = 7,'C','D' )))";
$this->db
->select("date, ".$this->db->dbprefix('warehouses').".name,
CONCAT(".$this->db->dbprefix('warehouses').".name,'-',".$dateTmp.",'-', ".$condition.",".$this->db->dbprefix('sales').".invoice_no) as month," ." biller, customer,
scan_no,unit_price,tin,cin,cst,total_discount, item_tax, shipping,subtotal,payment_status,sales.id as saleid,total_items,total,biller_id,tax_rate_id,grand_total,quantity", FALSE)
->from('sales')
->join('sale_items', 'sale_items.sale_id=sales.id', 'left')
->join('companies','companies.id=sales.customer_id', 'left')
->join('warehouses', 'warehouses.id=sales.warehouse_id', 'left')
->group_by('sales.id')
->order_by('sales.date desc');
if ($biller) {
$this->db->where('sales.biller_id', $biller);
}
if ($customerid) {
$this->db->where('sales.customer_id', $customerid);
}
if ($warehouse) {
$this->db->where('sales.warehouse_id', $warehouse);
}
if (($start_date) && ($end_date)) {
$this->db->where($this->db->dbprefix('sales').'.date BETWEEN "' . $start_date . '" and "' . $end_date . '"');
}
/* if ($start_date) {
$this->db->where($this->db->dbprefix('sales').'.date LIKE "' . $start_date . '"');
}
if ($end_date) {
$this->db->where($this->db->dbprefix('sales').'.date LIKE "' . $end_date . '"');
} */
$q = $this->db->get();
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
} else {
$data = NULL;
}
if (!empty($data))
{
$this->load->library('excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle(lang('sales_report'));
$this->excel->getActiveSheet()->SetCellValue('A1', lang('date'));
$this->excel->getActiveSheet()->SetCellValue('B1', lang('branch'));
$this->excel->getActiveSheet()->SetCellValue('C1', lang('Vch_No.'));
$this->excel->getActiveSheet()->SetCellValue('D1', lang('Voucher_Type'));
$this->excel->getActiveSheet()->SetCellValue('E1', lang('Particulars'));
$this->excel->getActiveSheet()->SetCellValue('F1', lang('TIN_No.'));
$this->excel->getActiveSheet()->SetCellValue('G1', lang('CIN'));
$this->excel->getActiveSheet()->SetCellValue('H1', lang('CST_No.'));
$this->excel->getActiveSheet()->SetCellValue('I1', lang('Scan_No'));
$this->excel->getActiveSheet()->SetCellValue('J1', lang('Product'));
$this->excel->getActiveSheet()->SetCellValue('K1', lang('quantity'));
$this->excel->getActiveSheet()->SetCellValue('L1', lang('Rate'));
$this->excel->getActiveSheet()->SetCellValue('M1', lang('Sales_#__5%'));
$this->excel->getActiveSheet()->SetCellValue('N1', lang('Output_Vat_#_5%'));
$this->excel->getActiveSheet()->SetCellValue('O1', lang('Sales_#_14.5%'));
$this->excel->getActiveSheet()->SetCellValue('P1', lang('Output_Vat_#_14.5%'));
$this->excel->getActiveSheet()->SetCellValue('Q1', lang('Sales_#_0%'));
$this->excel->getActiveSheet()->SetCellValue('R1', lang('Scanning_charge'));
$this->excel->getActiveSheet()->SetCellValue('S1', lang('Scanning_Discount'));
$this->excel->getActiveSheet()->SetCellValue('T1', lang('Shipping'));
$this->excel->getActiveSheet()->SetCellValue('U1', lang('Gross_Total'));
$row = 2;
$total = 0;
$paid = 0;
$balance = 0;
$quantity= 0;
$prdt_price = 0;
$sale5=0.00;
$output5=0.00;
$sale145=0.00;
$output145=0.00;
$sale0=0.00;
$discount=0;
foreach ($data as $data_row)
{
$saleid=$data_row->saleid;
$billerid=$data_row->biller_id;
$gross_total= $data_row->grand_total + shipping;
if($billerid==7)
{
$sale0=0;
$discount=$data_row->total_discount;
/*$scanqty=$data_row->quantity;
if($scanqty==0.5)
{
$scanning_charge+=500;
}
else if($scanqty==1)
{
$scanning_charge+=1000;
}*/
$this->db->select("sum(`quantity`*1000) as scanning_charge", FALSE);
$this->db->where('sale_id',$saleid);
$queryres = $this->db->get('sale_items');
if($queryres->num_rows() > 0)
{
foreach($queryres->result() as $queryres1)
{
$scanning_charge=$queryres1->scanning_charge;
}
}
else
{
}
}
else
{
$scanning_charge=0;
$discount=0;
}
if(($billerid==5) || ($billerid==6))
{
$this->db->select("sum( case when tax_rate_id = 1 then `net_unit_price`*`quantity` else 0 end) as sale0", FALSE);
$this->db->where('sale_id',$saleid);
$res1=$this->db->get('sale_items');
if($res1->num_rows() > 0)
{
echo $billerid;
foreach ($res1->result() as $row31)
{
$sale0=$row31->sale0;
}
}
else
{
}
}
$total0=$data_row->total;
$this->db->select("GROUP_CONCAT(DISTINCT product_name) as products,SUM(quantity)as prdtqty")
->from('sale_items')
->where('sale_id',$saleid);
$q1 = $this->db->get();
if ($q1->num_rows() > 0)
{
foreach (($q1->result()) as $row1)
{
$products = $row1->products;
$prdtqty = $row1->prdtqty;
}
}
else
{
$products="";
}
$sql="select sum( case when tax_rate_id = 2 then net_unit_price*quantity else 0 end) as sale5,
sum( case when tax_rate_id = 3 then net_unit_price*quantity else 0 end) as sale145,
sum( case when tax_rate_id = 2 then item_tax else 0 end) as output5,
sum( case when tax_rate_id = 3 then item_tax else 0 end) as output145
from sma_sale_items
where sale_id=".$saleid;
$res=$this->db->query($sql);
if($res->num_rows() > 0)
{
foreach ($res->result() as $row3)
{
$sale5=$row3->sale5;
$output5=$row3->output5;
$sale145=$row3->sale145;
$output145=$row3->output145;
}
}
$this->excel->getActiveSheet()->SetCellValue('A' . $row, $this->sma->hrld($data_row->date));
$this->excel->getActiveSheet()->SetCellValue('B' . $row, $data_row->name);
$this->excel->getActiveSheet()->SetCellValue('C' . $row, $data_row->month);
$this->excel->getActiveSheet()->SetCellValue('D' . $row, $data_row->biller);
$this->excel->getActiveSheet()->SetCellValue('E' . $row, $data_row->customer);
$this->excel->getActiveSheet()->SetCellValue('F' . $row, $data_row->tin);
$this->excel->getActiveSheet()->SetCellValue('G' . $row, $data_row->cin);
$this->excel->getActiveSheet()->SetCellValue('H' . $row, $data_row->cst);
$this->excel->getActiveSheet()->SetCellValue('I' . $row, $data_row->scan_no);
$this->excel->getActiveSheet()->SetCellValue('J' . $row, $products);
$this->excel->getActiveSheet()->SetCellValue('K' . $row, $prdtqty);
$this->excel->getActiveSheet()->SetCellValue('L' . $row, $data_row->total);
$this->excel->getActiveSheet()->SetCellValue('M' . $row, $sale5);
$this->excel->getActiveSheet()->SetCellValue('N' . $row, $output5);
$this->excel->getActiveSheet()->SetCellValue('O' . $row, $sale145);
$this->excel->getActiveSheet()->SetCellValue('P' . $row, $output145);
$this->excel->getActiveSheet()->SetCellValue('Q' . $row, $sale0);
$this->excel->getActiveSheet()->SetCellValue('R' . $row, $scanning_charge);
$this->excel->getActiveSheet()->SetCellValue('S' . $row, $discount);
$this->excel->getActiveSheet()->SetCellValue('T' . $row, $data_row->shipping);
$this->excel->getActiveSheet()->SetCellValue('U' . $row, $gross_total);
$total += $data_row->subtotal;
$paid += $data_row->item_tax;
$row++;
}
$this->excel->getActiveSheet()->getColumnDimension('A')->setWidth(18);
$this->excel->getActiveSheet()->getColumnDimension('B')->setWidth(8);
$this->excel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
$this->excel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
$this->excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$this->excel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
$this->excel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
$this->excel->getActiveSheet()->getColumnDimension('I')->setWidth(12);
$this->excel->getActiveSheet()->getColumnDimension('J')->setWidth(30);
$this->excel->getActiveSheet()->getColumnDimension('K')->setWidth(10);
$this->excel->getActiveSheet()->getColumnDimension('L')->setWidth(11);
$this->excel->getActiveSheet()->getColumnDimension('M')->setWidth(13);
$this->excel->getActiveSheet()->getColumnDimension('N')->setWidth(17);
$this->excel->getActiveSheet()->getColumnDimension('O')->setWidth(14);
$this->excel->getActiveSheet()->getColumnDimension('P')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('Q')->setWidth(12);
$this->excel->getActiveSheet()->getColumnDimension('R')->setWidth(18);
$this->excel->getActiveSheet()->getColumnDimension('S')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('T')->setWidth(12);
$this->excel->getActiveSheet()->getColumnDimension('U')->setWidth(14);
$filename = 'sales_report';
$this->excel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$this->excel->getActiveSheet()->getStyle('E2:E' . $row)->getAlignment()->setWrapText(true);
ob_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
header('Cache-Control: max-age=0');
ob_clean();
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
exit();
}
else{
$this->session->set_flashdata('error', lang('nothing_found'));
redirect('reports/sales_accounts');
}
}
Here is my browser view:
Can anyone please help me to fix this.. I am really in need of this.. Thanks in advance.
Try this
ob_clean();
ob_start(); # added
header('Content-type: application/vnd.ms-excel; charset=UTF-8" ); # Improved with UTF charset
header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
header('Cache-Control: max-age=0');
ob_clean(); # remove this
if you want download this with 2003 and 2007 there are few changes to be done
2007 - file format xlsx
2003 - file format xls

html form with php issue

I have made a html form where the user can enter his name/email/phone etc. and also select quantity of the listed products.
Here is the product code and I'm wondering how do I do to include in the email message I get from the form to include how many of what product the user wants.
This is the form code:
<div class="col-sm-6 col-md-6 bottom-padding">
<?php
process_si_contact_form();
if (isset($_SESSION['ctform']['error']) && $_SESSION['ctform']['error'] == true):
?>
<div class="alert alert-danger">
<strong>Oops!</strong> Something went wrong.
</div>
<?php
elseif (isset($_SESSION['ctform']['success']) && $_SESSION['ctform']['success'] == true):
?>
<div class="alert alert-success">
<strong>Message sent!</strong> We'll get in touch asap.
</div>
<?php
endif;
?>
<form class="form-box register-form contact-form" method="POST" id="contact_form">
<input type="hidden" name="do" value="contact" />
<h3 class="title">Form</h3>
<label>Name: <span class="required">*</span></label>
<?php echo #$_SESSION['ctform']['f_name_error'] ?>
<input class="form-control" type="text" name="ct_f_name" value="<?php echo htmlspecialchars(#$_SESSION['ctform']['ct_f_name']) ?>">
<label>E-mail: <span class="required">*</span></label>
<?php echo #$_SESSION['ctform']['f_email_error'] ?>
<input class="form-control" type="email" name="ct_f_email" value="<?php echo htmlspecialchars(#$_SESSION['ctform']['ct_f_email']) ?>">
<label>Phone: <span class="required">*</span></label>
<?php echo #$_SESSION['ctform']['f_tel_error'] ?>
<input class="form-control" type="text" name="ct_f_tel" value="<?php echo htmlspecialchars(#$_SESSION['ctform']['ct_f_tel']) ?>">
<div class="panel-group" id="accordion">
<div class="panel panel-info">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" class="collapsed">
Hardware
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse" style="height: 0px;">
<div class="panel-body">
<ul class="list-group checked-list-box">
<?php
$my_products = array(
'1' => 'Product 1',
'2' => 'Product 2',
'3' => 'Product 3'
);
foreach ($my_products as $key => $value) {
echo "<div class=\"col-xs-6\" style=\"margin: 10px 0 5px 0;\">";
echo "<li class=\"list-group-item\" data-style=\"button\">";
echo $value;
echo "<select class=\"form-control\" name=\"quantity[$key]>\"";
for ($i = 0; $i <= 10; $i++) echo "<option value=\"$i\">$i</option>";
echo "</select>";
echo "</li>";
echo "</div>";
}
?>
</ul>
</div>
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" class="collapsed">
Software
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
Empty.
</div>
</div>
</div>
</div>
<label>Message: <span class="required">*</span></label>
<?php echo #$_SESSION['ctform']['message_error'] ?>
<textarea class="form-control" name="ct_message"><?php echo htmlspecialchars(#$_SESSION['ctform']['ct_message']) ?></textarea>
<div class="clearfix"></div>
<div class="buttons-box clearfix">
<input type="submit" class="btn btn-default" value="Send">
<span class="required"><b>*</b> Required fields</span>
</div>
</form>
</div>
Anything else I need to post? I don't know if this is the best way coded but it does the work. Now it won't let me save this question because it's much code included in this post so I'm writing to be able to save it lol.
EDIT: ADDING PHP FORM CODE.
<?php
function process_si_contact_form() {
$_SESSION['ctform'] = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST' && #$_POST['do'] == 'contact') {
foreach($_POST as $key => $value) {
if (!is_array($key)) {
if ($key != 'ct_message') $value = strip_tags($value);
$_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
}
}
$f_name = #$_POST['ct_f_name'];
$f_tel = #$_POST['ct_f_tel'];
$f_email = #$_POST['ct_f_email'];
$message = #$_POST['ct_message'];
$f_name = substr($f_name, 0, 64);
$errors = array();
if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
if (strlen($f_name) < 3) {
$errors['f_name_error'] = 'Your name please.';
}
if (strlen($f_tel) < 10) {
$errors['f_tel_error'] = 'Your phone please.';
} else if (!preg_match('/^([-+0-9()]+)$/', $f_tel)) {
$errors['f_tel_error'] = 'Thats not a phone number!';
}
if (strlen($f_email) == 0) {
$errors['f_email_error'] = 'Your e-mail please.';
} else if (!preg_match('/^(?:[\w\d]+\.?)+#(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $f_email)) {
$errors['f_email_error'] = 'Thats not an e-mail!';
}
if (strlen($message) < 10) {
$errors['message_error'] = 'Your message must contain atleast 10 characters.';
}
}
if (sizeof($errors) == 0) {
$time = date('r');
$message = "<strong>Name:</strong><br /><em>$f_name</em><br />"
. "<br />"
. "<strong>E-mail:</strong><br /><em>$f_email</em><br />"
. "<br />"
. "<strong>Phone:</strong><br /><em>$f_tel</em>"
. "<br /><br /><br />"
. "<strong>Message:</strong><br />"
. "<pre>$message</pre>"
. "<br /><br />"
. "<strong>IP:</strong><br /><em>{$_SERVER['REMOTE_ADDR']}</em><br />"
. "<br />"
. "<strong>Time:</strong><br /><em>$time</em><br />"
. "<br />"
. "<strong>Browser:</strong><br /><em>{$_SERVER['HTTP_USER_AGENT']}</em>";
$message = wordwrap($message, 70);
if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
mail($GLOBALS['ct_recipient'], $GLOBALS['ct_msg_subject'], $message, "From: {$GLOBALS['ct_recipient_no_reply']}\r\nReply-To: {$f_email}\r\nContent-type: text/html; charset=utf8\r\nMIME-Version: 1.0");
}
$_SESSION['ctform']['error'] = false;
$_SESSION['ctform']['success'] = true;
} else {
$_SESSION['ctform']['ct_f_name'] = $f_name;
$_SESSION['ctform']['ct_f_tel'] = $f_tel;
$_SESSION['ctform']['ct_f_email'] = $f_email;
$_SESSION['ctform']['ct_message'] = $message;
foreach($errors as $key => $error) {
$_SESSION['ctform'][$key] = "<span class=\"error\" style=\"float: right; color: 00ff00;\">$error</span>";
}
$_SESSION['ctform']['error'] = true;
}
}
}
$_SESSION['ctform']['success'] = false;
?>
EDIT 2: ADDING NEW PHP MAIL CODE.
<?php
function process_si_contact_form() {
$_SESSION['ctform'] = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST' && #$_POST['do'] == 'contact') {
foreach($_POST as $key => $value) {
if (!is_array($key)) {
if ($key != 'ct_message') $value = strip_tags($value);
$_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
}
}
$f_name = #$_POST['ct_f_name'];
$f_tel = #$_POST['ct_f_tel'];
$f_email = #$_POST['ct_f_email'];
$message = #$_POST['ct_message'];
$f_name = substr($f_name, 0, 64);
$products = array(
#$_POST['quantity[1]'],
#$_POST['quantity[2]'],
#$_POST['quantity[3]'],
#$_POST['quantity[4]'],
#$_POST['quantity[5]'],
#$_POST['quantity[6]'],
#$_POST['quantity[7]'],
#$_POST['quantity[8]'],
#$_POST['quantity[9]'],
#$_POST['quantity[10]']);
$errors = array();
if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
if (strlen($f_name) < 3) {
$errors['f_name_error'] = 'Fyll i ditt namn';
}
if (strlen($f_tel) < 10) {
$errors['f_tel_error'] = 'Fyll i ditt tel.nr';
} else if (!preg_match('/^([-+0-9()]+)$/', $f_tel)) {
$errors['f_tel_error'] = 'Felaktigt tel.nr';
}
if (strlen($f_email) == 0) {
$errors['f_email_error'] = 'Fyll i din e-postadress';
} else if (!preg_match('/^(?:[\w\d]+\.?)+#(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $f_email)) {
$errors['f_email_error'] = 'Felaktig e-postadress';
}
if (strlen($message) < 10) {
$errors['message_error'] = 'Ditt meddelande måste bestå av minst 10 tecken';
}
}
if (sizeof($errors) == 0) {
$time = date('r');
$message = "<strong>Namn:</strong><br /><em>$f_name</em><br />"
. "<br />"
. "<strong>E-postadress:</strong><br /><em>$f_email</em><br />"
. "<br />"
. "<strong>Telefon:</strong><br /><em>$f_tel</em>"
. "<br /><br /><br />"
. "<strong>Meddelande:</strong><br />"
. "<pre>$message</pre>"
. "<br />"
. "<strong>IP:</strong><br /><em>{$_SERVER['REMOTE_ADDR']}</em><br />"
. "<br /><strong>".$products[1]." - Ingenico IPP350</strong>"
. "<br /><strong>".$products[2]." - Ingenico ICT250</strong>"
. "<br /><strong>".$products[3]." - Yomani</strong>"
. "<br /><strong>".$products[4]." - Ingenico IWL250 GPRS</strong>"
. "<br /><strong>".$products[5]." - PosBank® AnyShop II</strong>"
. "<br /><strong>".$products[6]." - Ingenico IWL250 Wifi</strong>"
. "<br /><strong>".$products[7]." - Ingenico IWL250 BT</strong>"
. "<br /><strong>".$products[8]." - PosBank&reg AnyShop e2</strong>"
. "<br /><strong>".$products[9]." - Ingenico IWL285 3G</strong>"
. "<br /><strong>".$products[10]." - Ingenico iCMP</strong>"
. "<br /><br /><strong>Tid:</strong><br /><em>$time</em><br />"
. "<br />"
. "<strong>Webbläsare:</strong><br /><em>{$_SERVER['HTTP_USER_AGENT']}</em>";
$message = wordwrap($message, 70);
if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
mail($GLOBALS['ct_recipient'], $GLOBALS['ct_msg_subject'], $message, "From: {$GLOBALS['ct_recipient_no_reply']}\r\nReply-To: {$f_email}\r\nContent-type: text/html; charset=utf8\r\nMIME-Version: 1.0");
}
$_SESSION['ctform']['error'] = false;
$_SESSION['ctform']['success'] = true;
} else {
$_SESSION['ctform']['ct_f_name'] = $f_name;
$_SESSION['ctform']['ct_f_tel'] = $f_tel;
$_SESSION['ctform']['ct_f_email'] = $f_email;
$_SESSION['ctform']['ct_message'] = $message;
foreach($errors as $key => $error) {
$_SESSION['ctform'][$key] = "<span class=\"error\" style=\"float: right; color: 00ff00;\">$error</span>";
}
$_SESSION['ctform']['error'] = true;
}
}
}
$_SESSION['ctform']['success'] = false;
?>
The name of these elements would be: quantity[0], quantity[1], and quantity[2] which contains the value gave for each product. Notice if you want a quantity[0] you need to include that in the $my_products array. The value for Product 1 would be in quantity[1]. The information is from this code here:
$my_products = array(
'0' => 'Product 0', /* Added Product 0 */
'1' => 'Product 1',
'2' => 'Product 2'
);
foreach ($my_products as $key => $value) {
...
// $key is 0,1,2 through the loop so the name would be
// quantity[0] quantity[1], and quantity[2]
echo "<select class=\"form-control\" name=\"quantity[$key]>\"";
for ($i = 0; $i <= 10; $i++) echo "<option value=\"$i\">$i</option>";
echo "</select>";
...
}
So you would just get name in the same way you got your other POST data in your PHP form code:
$products = array(
#$_POST['quantity[0]'],
#$_POST['quantity[1]'],
#$_POST['quantity[2]'] );
Now just include those values in your $message:
$message =
/* Your normal message content here from the code */
. "<strong>Product0:</strong><br /><em>".$products[0]."</em><br />"
. "<br />"
. "<strong>Product1:</strong><br /><em>".$products[1]".</em><br />"
. "<br />"
. "<strong>Product2:</strong><br /><em>".$products[2]."</em><br />"
. "<br />";

multiple inputs - Problems with array

it is such that I need to use multiple inputs on my side,
I've tried to do like this but it does not work as it will not show up on the page at all in some manner.
HTML
<div id="onlinetestside">
<div id="onlinetestsidealt"><?php echo $ordet;?></div>
<input type="text" name="ord[]" maxlength="190">
<div style="clear:both;"></div>
</div>
PHP
//ordetalt its coming from database.
$alt = $_POST["ord"];
if($ordetalt == $alt)
{
echo $ordetalt . " og " . $alt;
}
else
{
echo "Error " . $ordetalt . " and " . $alt;
}
error appears like this:
Error Hej and Array
What I want to get to it shall be such that "$ordetalt == $alt" have the same content and it fits together.
EIDT
Here I show the entire code where I need to download some code to the side.
if ($stmt = $this->mysqli->prepare('SELECT id, ordet, ordetalt FROM test WHERE getid = ?')) {
$stmt->bind_param('i', $id);
$id = $_GET['id'];
$stmt->execute();
$stmt->bind_result($id, $ordet, $ordetalt);
while ($stmt->fetch()) {
?>
<div id="onlinetestside">
<div id="onlinetestsidealt"><?php echo $ordet;?></div>
<input type="text" name="ord[]" maxlength="190">
<div style="clear:both;"></div>
</div>
<?php
//ordetalt its coming from database
$alt = $_POST["ord"];
if($ordetalt == $alt)
{
echo $ordetalt . " og " . $alt;
}
else
{
echo "Error " . $ordetalt . " and " . $alt;
}
}
$stmt->close();
}
EIDT EIDT
$i = 0;
$a = $i++;
$alt = $_POST["ord"][$a];
if($ordetalt == $alt)
{
echo $ordetalt . " og " . $alt;
}
else
{
echo "Error " . $ordetalt . " and " . $alt;
}
Barmar is spot on, your line
$alt = $_POST["ord"];
Should read
$alt = $_POST["ord"][0];
OR
change your html like so:
<div id="onlinetestside">
<div id="onlinetestsidealt"><?php echo $ordet;?></div>
<input type="text" name="ord" maxlength="190">
<div style="clear:both;"></div>
</div>

Categories