adding database query in XLS - php
I have some difficulties getting the XLS function to add the query result in a xls file.
I have a html and a php file who is doing the job.
The query is working as it should, but i think the XLS function is not fetching the query result. I get "whitescreen" after pressing the button in the html file.
<div class="right" style="width:940px;">
Download as .XLS
</div>
i want the query to be saved in an xls file that can be saved localy.
The php is looking like this:
<?PHP
import( 'pages.admin' );
class AdminPortalStatistics extends AdminPage implements IView {
protected $template = 'order.stats';
public
function Execute() {
$kode = 'ST1';
$from = '2017-06-20';
$to = '2017-07-29';
if ($_GET['from'] && $_GET['to']) {
$from = $_GET['from'];
$to = $_GET['to'];
$query = "SELECT * from history_ordrer ho INNER JOIN history_cusomer hc ON ho.ordrenr = hc.ordrenr INNER JOIN history_mal hm ON ho.ordrenr = hm.ordrenr INNER JOIN histort_orderline hol ON ho.ordrenr = hol.ordrenr INNER JOIN kunde k ON ho.uid = k.uid WHERE ho.kode = ? AND ho.time BETWEEN ? AND ? AND ho.deleted is null order by ho.ordernr desc";
$orders = DB::query($query, $kode, $from, $to) - > fetchAll(DB::FETCH_ASSOC);
$this - > stats = $orders;
}
$this - > kode = $kode;
$this - > from = $from;
$this - > to = $to;
}
public function XLS( ) {
$this->Execute( );
$this->setTemplate( false );
$oldreporting = error_reporting( 0 );
require_once 'Spreadsheet/Excel/Writer.php';
// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();
// sending HTTP headers
$workbook->send('test.xls');
// Creating a worksheet
$worksheet =& $workbook->addWorksheet( 'Report' );
// write the data
$fields = array_merge( array( 'Period' ), $this->series );
$worksheet->writeRow( $row++, 0, $fields );
foreach( $this->values as $graph ) {
$line = array_merge( array( $graph['label'] ), $graph['data'] );
$worksheet->writeRow( $row++, 0, $line );
}
// Let's send the file
$workbook->close();
error_reporting( $oldreporting );
}
}
?>
Related
Can't display footer (wordpress)
Problem: run_overview() displays account_page-overview.php well. The problem is that it blocks footer(), so the footer part disappears on frontend. When I remove 'require $template;' of the run_overview(), it shows a footer part well, but doesn't show run_overview() part. Action I took: I tried changing the following code (and removed require $template;), but it doesn't display run_overview() part. include locate_template('public/views/account_page-overview.php', false, false); Need Help: Would you please let me know if there is another method to display both run_overview() and footer()? PHP Code: private function run_overview(){ /* * #param none * #return none (print html) */ global $indeed_db; $post_overview = get_user_meta($this->uid, 'uap_overview_post', true); if ($post_overview && $post_overview!=-1){ //print the post for user $post = get_post($post_overview); $data['message'] = $post->post_content; } else { //predifined message $data['message'] = uap_replace_constants($this->account_page_settings['uap_tab_overview_content'], $this->uid); $data['message'] = stripslashes($data['message']); $data['message'] = uap_correct_text($data['message']); } $data['title'] = $this->account_page_settings['uap_tab_overview_title']; $data['stats'] = $indeed_db->get_stats_for_payments($this->affiliate_id); $data['stats']['currency'] = get_option('uap_currency'); $data['help_url'] = $url = add_query_arg('uap_aff_subtab', 'help', $this->account_page_base_url); $data['statsForLast30'] = $indeed_db->getReferralsAmountForLastDays( $this->affiliate_id, 30 ); $data['referralsStats'] = $indeed_db->get_stats_for_reports( 'last_month', $this->affiliate_id ); $data['referralsExtraStats'] = $indeed_db->get_referral_report_by_date( $this->affiliate_id, date( 'Y-m-d h:i:s', time() - 30 * 24 * 60 * 60 ), date( 'Y-m-d h:i:s', time() ) ); $this->print_top_messages(); $fullPath = UAP_PATH . 'public/views/account_page-overview.php'; $searchFilename = 'account_page-overview.php'; $template = apply_filters('uap_filter_on_load_template', $fullPath, $searchFilename ); require $template; } private function footer(){ global $indeed_db; $data['footer_content'] = uap_replace_constants($this->account_page_settings['uap_ap_footer_msg'], $this->uid); $data['footer_content'] = stripslashes($data['footer_content']); $fullPath = UAP_PATH . 'public/views/account_page-footer.php'; $searchFilename = 'account_page-footer.php'; $template = apply_filters('uap_filter_on_load_template', $fullPath, $searchFilename ); require $template; } Thank you.
Get all messages from array - php
I want to get all messages and put it all into array , but there is another way i want sort all messages by id , for example: my json : {"msg_c":[{"message":"Hi","sender":"2","receve":"3","name":"Muhamad Bagzada"},{"message":"Hello","sender":"3","receve":"2","name":"Danyal Join"},{"message":"What's up , How are you ?","sender":"2","receve":"3","name":"Muhamad Bagzada"},{"message":"fine and you ?","sender":"3","receve":"2","name":"Danyal Join"},{"message":"fine","sender":"2","receve":"3","name":"Muhamad Bagzada"}]} Exactly i want : show messgaes in array by different value , ( message_u : my msg , messages_m : his msg) { "msg_c": [ {"message_u":"Hi","sender":"2","receve":"3","name":"Muhamad Bagzada"}, {"message_m":"Hello","sender":"3","receve":"2","name":"Danyal Join"}, {"message_u":"What's up , How are you ?","sender":"2","receve":"3","name":"Muhamad Bagzada"}, {"message_m":"fine and you ?","sender":"3","receve":"2","name":"Danyal Join"}, {"message_m":"fine","sender":"2","receve":"3","name":"Muhamad Bagzada"} ] } php function : public function MessageApp ($user_id,$sender){ $data =''; $Messg = mysqli_query($this->_join, "SELECT * FROM `messages` WHERE (`from` = '$sender' AND `to` = '$user_id') OR (`from` = '$user_id' AND `to` = '$sender') ORDER BY dates ASC LIMIT 12 "); while ($m = mysqli_fetch_assoc($Messg)) { $from = $m['from']; $to = $m['to']; $chat = $m['chat']; $dates = $m['dates']; $id = $m['id']; if ($user_id === $sender) { $select_id = $to; }else{ $select_id = $from; } $user = new user($from); $fullname = ($user->data()->fname)." ".($user->data()->lname); $gettype['msg_c'][] = array( 'message' => $chat, 'sender' => $from, 'receve' => $to, 'name'=>$fullname ); $data = json_encode($gettype); } return json_encode($gettype); } I called json data here : <?php include $_SERVER['DOCUMENT_ROOT']."/php/core/init.php"; include $_SERVER['DOCUMENT_ROOT']."/php/core/connect.php"; $my_id = input::get('user_id'); $user = new user($my_id); $sender = $user->data()->id; $db = db::getInstance(); $message = new message(); $id = input::get("id"); $user = new user($id); $user_id = $user->data()->id; echo $message->MessageApp($user_id,$sender); ?>
When you building the row in msg_c, you can separate the set key in different situation like this <?php $msg_row = array( 'sender' => $from, 'receve' => $to, 'name'=> $fullname ); if ($user_id === $sender) { $msg_row['message_m'] = $chat; } else { $msg_row['message_u'] = $chat; } $gettype['msg_c'][] = $msg_row; Or even build the array in the if else statement before to reuse the code
use variable from $get in query
Have a php scripts that holds 2 functions. In the first function the query is using the $get to fetch time and date. The second function is making im trying to use the variable from the first function. public function Execute( ) { if( $_GET['from'] && $_GET['to'] ){ $from = $_GET['from']; $to = $_GET['to']; $query = "SELECT * from historie_ordre where kode = ? AND time BETWEEN ? AND ? AND order by time desc"; $orders = DB::query( $query, $kode, $from, $to )->fetchAll( DB::FETCH_ASSOC ); Util::Debug( $orders ); $this->stats = $orders; } $this->kode = $kode; $this->from = $from; $this->to = $to; } second funstion: public function XLS( ) { //trying to use this: $from = intval($_GET['from']); $to = intval($_GET['to']); $kode = intval($_GET['kode']); $query = "SELECT * from historie_ordre where kode = $kode AND time BETWEEN $from AND $to AND order by time desc"; $orders = DB::query( $query, $kode, $from, $to )->fetchAll( DB::FETCH_ASSOC ); $this->setTemplate( false ); $oldreporting = error_reporting( 0 ); require_once 'Spreadsheet/Excel/Writer.php'; // Creating a workbook $workbook = new Spreadsheet_Excel_Writer(); // sending HTTP headers $workbook->send('stabburet_stats.xls'); // Creating a worksheet $worksheet =& $workbook->addWorksheet('My first worksheet'); // overskrift //$worksheet->write(0, 0, 'Antall'); $worksheet->write(0, 1, 'kode'); $worksheet->write(0, 2, 'name'); $worksheet->write(0, 3, 'adress'); $i=1; // for å få inn verdiane foreach( $orders as $order ){ $i++; //$worksheet->write($i, 0, $order['antall']); $worksheet->write($i, 1, $order['kode']); $worksheet->write($i, 2, utf8_decode( $order['name'] ) ); $worksheet->write($i, 3, utf8_decode ($order['adress']) ); } // Sender fila $workbook->close(); error_reporting( $oldreporting ); } } The first function is working. second is only working with "hardcoding" the variables.
It was pointed out to you in comments you need to be more careful and use the same techniques consistently. I also don't believe that the first function works, because your query syntax in each place is wrong. The problem at very least is that you have an incorrect SELECT syntax with a spurious AND at the end. You also are using a column named time, which is a mysql reserved word. That might also be a problem, that can be mitigated by using the `column name` syntax. $query = "SELECT * from historie_ordre WHERE kode = $kode AND time BETWEEN $from AND $to AND order by time desc"; Needs to be: $query = "SELECT * FROM historie_ordre WHERE kode = ? AND `time` BETWEEN ? AND ? ORDER BY `time` DESC"; Updated: Now that we have more information from you, the problem can also be traced to here: $from = intval($_GET['from']); $to = intval($_GET['to']); $kode = intval($_GET['kode']); The problem is that your from and to need to be strings in date format. You are forcing them to be zeros by turning them into integers. So again your query is not going to function. $from = $_GET['from']; $to = $_GET['to']; $kode = intval($_GET['kode']);
Generating PDF dynamically through fpdf
I am trying to generate a PDF dynamically using fpdf, but I am finding it very difficult. Only half of the result I could get. I want to generate my PDF like this: And now I get a result like this: Here is the code for PDF: <?php require('../fpdf/fpdf.php'); error_reporting(-1); $id = $_GET['order_id']; $db = new mysqli('localhost','root','','dbnme'); // use your credentials class INVPDF extends FPDF { var $id; var $today; var $widths; var $heads; var $aligns; var $formats; var $db; var $invTotal = 0; var $advancePaid = 0; //constructor function INVPDF($invno, $db) { parent::fpdf(); $this->id = $invno; $this->db = $db; $this->today = date('jS M Y'); $this->heads = array('Item', 'UOM', 'Price', 'Qty', 'Disc %', 'Tax', 'Frt', 'Total' ); $this->widths = array (45, 15, 35, 15, 15, 20, 25, 30); $this->aligns = array ('L','C','L','C','C','R','C', 'C'); $this->formats = array (0,0,0,0,0,1,0,0); } //Page header function Header() { //Arial bold 15 //Title include("../connect.php"); $ss1 = "select orders.sales_order_id, orders.company_id, lead_address.address, lead_address.address_category, lead_address.country, lead_address.state, lead_address.company_id, lead_address.city,lead_address.pin from orders INNER JOIN lead_address ON orders.company_id=lead_address.company_id where lead_address.address_category='Billing' AND orders.sales_order_id='".$_GET['order_id']."'"; $mq1 = mysql_query($ss1) or die(mysql_error()); $rr1 = mysql_fetch_array($mq1); $billing = $rr1['address']; list($line1, $line2, $line3) = explode(',',$billing); $country = $rr1['country']; $state = $rr1['state']; $city = $rr1['city']; $pin = $rr1['pin']; //list($line1, $line2, $country, $state, $city, $pin) = explode(',',$address); $ss2 = "select orders.sales_order_id, orders.company_id, lead_address.address, lead_address.address_category, lead_address.country, lead_address.state, lead_address.company_id, lead_address.city,lead_address.pin from orders INNER JOIN lead_address ON orders.company_id=lead_address.company_id where lead_address.address_category='Shipping' AND orders.sales_order_id='".$_GET['order_id']."'"; $mq2 = mysql_query($ss2) or die(mysql_error()); $rr2 = mysql_fetch_array($mq2); $shipping = $rr2['address']; $country1 = $rr2['country']; $state1 = $rr2['state']; $city1 = $rr2['city']; $pin1 = $rr2['pin']; //$email = $rr1['email']; // $phone = $rr1['phone']; // $mobile = $rr1['mobile']; // $this->SetFont('Arial','B',15); $this->setXY(10,20); // $this->Cell(0,10,'INVOICE '.$this->id,0,2,'L'); $this->Image('logo.png',20,6,15); $this->setXY(12,20); // $this->SetFont('OpenSans','',7); $this->Cell(0,10,'Company Name. ',0,2,'L'); $this->Cell(0,0,'Address1, address2',0,2,'L'); $this->Cell(0,8,'city, stte',0,2,'L'); $this->Cell(0,2,'sales#company.com',0,2,'L'); //$this->Image('images/logo.png',10,6,60); $this->SetFont('OpenSans','',7); $this->setXY(12,50); $this->Cell(0,-2,'Shipping Address',$this->id,0,2,0,'L'); $this->SetFont('OpenSans','',7); $this->setXY(12,52); $this ->MultiCell(57,22,'', 'LRTB', 'L', 1); //$this->Cell(0,8,$name,$this->id,0,2,0,'L'); $this->setXY(12,55); $this->Cell(0,-1,$shipping,$this->id,0,2,0,'L'); $this->setXY(12,53); $this->Cell(0,10,$country." , ".$state,$this->id,0,2,0,'L'); $this->setXY(12,52); $this->Cell(0,20,$city." , ".$pin,$this->id,0,2,0,'L'); $this->SetFont('OpenSans','',7); $this->setXY(140,52); $this ->MultiCell(57,22,'', 'LRTB', 'L', 1); $this->setXY(140,34); $this->Cell(0,30,'Billing Address',$this->id,0,2,0,'L'); $this->SetFont('OpenSans','',7); $this->setXY(140,35); $this->Cell(0,40,$line1." , ".$line2, $this->id,0,2,0,'L'); $this->setXY(140,35); $this->Cell(0,49,$line3, $this->id,0,2,0,'L'); $this->setXY(140,33); $this->Cell(0,62,$city1." , ".$pin1,$this->id,0,2,0,'L'); $this->setXY(140,33); $this->Cell(0,70,$country1." , ".$state1,$this->id,0,2,0,'L'); //$this->setXY(12,65); // $this->Cell(0,60,$phone,$this->id,0,2,0,'L'); //$this->setXY(12,65); //$this->Cell(0,70,$mobile,$this->id,0,2,0,'L'); $this->SetFont('OpenSans','',7); $this->setXY(10,5); $this->Cell(0,10,'QUOTATION: '.$this->id,0,2,'R'); $this->SetFont('OpenSans','',7); $until = date ('jS F Y', strtotime($this->today)); $this->Cell(0,0,'Date: '.$until,0,2,'R'); $this->SetFont('OpenSans','',7); $this->Cell(0,10,'Due Date: Due on receipt',0,2,'R'); //Line break $this->Ln(60); for ($i=0; $i<9; $i++) { $this->Cell ($this->widths[$i], 8, $this->heads[$i], 1, 0, 'C', 1); } $this->Ln(8); } //Page footer function Footer() { # $this->SetY(-50); // Uncomment to position at 5 cm from bottom //Arial italic 8 $this->SetFont('OpenSans','',8); $w = array_sum(array_slice($this->widths,0,3)); $this->Cell($w,12,'',1); $this->Cell($this->widths[3],12,'',1,0,'C'); $this->setLeftMargin(45 + array_sum(array_slice($this->widths,0,4))); $this->Cell($this->widths[5],4,'Sub Total',1); $this->Cell($this->widths[6],4,number_format($this->invTotal,2),1,0,'R'); $this->Cell($this->widths[7],4,'USD',1,1,'C'); $this->Cell($this->widths[5],4,'Tax',1); $this->Cell($this->widths[6],4,number_format($this->advancePaid,2),1,0,'R'); $this->Cell($this->widths[7],4,'USD',1,1,'C'); $this->Cell($this->widths[5],4,'Freight',1); $this->Cell($this->widths[6],4,number_format($this->freight,2),1,0,'R'); $this->Cell($this->widths[7],4,'USD',1,1,'C'); $this->setLeftMargin(10); $this->Cell($this->widths[5],4,'Total',1); $this->Cell($this->widths[6],4,number_format($this->invTotal+$this->advancePaid,2),1,0,'R'); $this->Cell($this->widths[7],4,'USD',1,1,'C'); $this->SetFont('OpenSans','',6); $this->Cell($w,10,'',0,0,'L'); $this->Cell(30,4,'Private Limited Company - TIN: 345sddd - PAN: sf43534',0,0,'C'); //$this->Cell($w,10,''); $this->Cell(-30,12,'SRVC TAX: gddddddddddd - CIN: sdgdgdgdfgfdgfg',0,0,'C'); $this->Cell(30,20,'This document has been electronically generated and requires no physical signature or stamp.',0,0,'C'); } function makeInvoice() { $sql = "select before_order_line_items.item, before_order_line_items.description, before_order_line_items.uom, before_order_line_items.selling_price, before_order_line_items.quantity, before_order_line_items.discount, before_order_line_items.tax, before_order_line_items.freight, before_order_line_items.tax_amount, before_order_line_items.total, items.name as iname, taxes.tax_id, taxes.name as tname, taxes.rate from before_order_line_items inner join items on before_order_line_items.item=items.item_id inner join taxes on before_order_line_items.tax=taxes.tax_id where before_order_line_items.sales_order_id = '".$_GET['order_id']."' "; //echo $sql; $res = $this->db->query($sql) or die($this->db->error); $this->SetFont('OpenSans','',8); if ($res->num_rows > 0) { while ($r = $res->fetch_row()) { $this->invTotal += $r[10]; foreach ($r as $c => $value) { //echo $value; if ($this->formats[$c]) { $value = number_format($value); // echo $value; } $this->Cell($this->widths[$c],10,$value,'LR',0, $this->aligns[$c]); } $this->Ln(); //$amount = number_format($amount+$value); } } } } # invpdf class $invno = $_GET['order_id']; //Instantiation of inherited class $pdf = new INVPDF($invno, $db); $pdf->Open(); $pdf->AliasNbPages(); $pdf->setLeftMargin(10); $pdf->setRightMargin(10); $pdf->AddFont('OpenSans','','Opensans-Regular.php'); $pdf->SetFont('OpenSans','',7); $pdf->SetDrawColor(102); $pdf->SetFillColor(220); $pdf->AddPage(); $pdf->makeInvoice(); $pdf->Output(); ?> Can somebody please help me how to do it? or is there any example code for something similar to this?
Looking at your actual pdf and expected pdf.. you are missing the no. column in table header. This line- $this->heads = array('Item', 'UOM', 'Price', 'Qty', 'Disc %', 'Tax', 'Frt', 'Total' ); should be- $this->heads = array('No.', 'Item', 'UOM', 'Price', 'Qty', 'Disc %', 'Tax', 'Frt', 'Total' );
Query array on model codeigninter
I am a student who learns CodeIgniter, I have a school assignment about the database, I create a project, and run query in my model just like this. class Send_model extends CI_Model{ function hello(){ $table = $this->db->query("SELECT * FROM (`tbl1`) LEFT JOIN `tbl2` ON `tbl2`.`id` = `tbl1`.`child_id` LEFT JOIN `tbl3` ON `tbl3`.`id` = `tbl1`.`child_id` "); foreach($table->result() as $row){ $modbus[]= $row->modbus; $data []= $row->data; $alert[]= $row->alert; }; $param0 = '&'.$modbus[0].'='.$data[0].':'.$alert[0]; $param1 = '&'.$modbus[1].'='.$data[1].':'.$alert[1]; $param2 = '&'.$modbus[2].'='.$data[2].':'.$alert[2]; $param3 = '&'.$modbus[3].'='.$data[3].':'.$alert[3]; $param4 = '&'.$modbus[4].'='.$data[4].':'.$alert[4]; $param5 = '&'.$modbus[5].'='.$data[5].':'.$alert[5]; $param6 = '&'.$modbus[6].'='.$data[6].':'.$alert[6]; $param7 = '&'.$modbus[7].'='.$data[7].':'.$alert[7]; $param8 = '&'.$modbus[8].'='.$data[8].':'.$alert[8]; $param9 = '&'.$modbus[9].'='.$data[9].':'.$alert[9]; $param10 = '&'.$modbus[10].'='.$data[10].':'.$alert[10]; $param11= '&'.$modbus[11].'='.$data[11].':'.$alert[11]; $param12 = '&'.$modbus[12].'='.$data[12].':'.$alert[12]; $param13 = '&'.$modbus[13].'='.$data[13].':'.$alert[13]; $param14 = '&'.$modbus[14].'='.$data[14].':'.$alert[14]; $param15 = '&'.$modbus[15].'='.$data[15].':'.$alert[15]; $param16 = '&'.$modbus[16].'='.$data[16].':'.$alert[16]; $param17 = '&'.$modbus[17].'='.$data[17].':'.$alert[17]; $param18 = '&'.$modbus[18].'='.$data[18].':'.$alert[18]; $sent = $param0.$param1.$param3.$param4.$param5.$param6.$param7.$param8.$param9.$param10.$param11.$param12.$param13.$param13.$param14.$param15.$param16.$param17; return $sent; } my controller just like this class Send extend CI_Controller{ function data () { $this->load->model('send_model'); $send = $this->model->send_model->hello(); echo $sent; } } i have a problem,if tbl1 add data then i have to write adding code to my script can anyone help me to simplify this code?
It is because you're hard-coding to send only 19 rows of data by creating 19 $param variables. In a dynamic condition, you may have an empty record-set or hundreds of records. Modify the structure of your foreach loop to the following: $param = array(); foreach($table->result() as $row){ $param[] = '&'.$row->modbus.'='.$row->data.':'.$row->alert; } return $param; Hope it answers your question.