Jump to content

export csv from mysql using phpexcel help


Go to solution Solved by bowen73,

Recommended Posts

Im trying to export a csv file from a mysql DB, which works, but im trying to put an if statement in, so basically....  for a cell of a product if prod_override is zero or empty the its uses cost +30% if the override price has a value then this value is to be used.

 

i get an error of 'unexpected if'.  can anyone point me in the right direction??

 

This is what i have so far.... everything works until i put the if else in.

<?php
require ("../connect.php"); 
//$sql ="select * from products";

$sql = mysqli_query($connect,"Select * FROM products");
while ($row = mysqli_fetch_array($sql))

    require_once '../Classes/PHPExcel/IOFactory.php';
    require_once '../Classes/PHPExcel.php';

        // Create new PHPExcel object
        $objPHPExcel = new PHPExcel();

        // Set document properties
        $objPHPExcel->getProperties()->setCreator("Mark Bowen")
                                                   ->setLastModifiedBy("Mark Bowen")
                                                   ->setTitle("Product Export")
                                                   ->setSubject("Product Export")
                                                   ->setDescription("Product export from database to import into Website")
                                                   ->setKeywords("office 2007 openxml php")
                                                   ->setCategory("CSV result file");
       
        // Add some data
        $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A1',"sku") 
						->setCellValue('B1',"name")
						->setCellValue('C1',"short_description")
						->setCellValue('D1',"description")
						->setCellValue('E1',"msrp")
						->setCellValue('F1',"price")
						->setCellValue('G1',"cost")
						->setCellValue('H1',"in_in_stock")
						->setCellValue('I1',"qty")
						->setCellValue('J1',"visibility")
						->setCellValue('K1',"status")
						->setCellValue('L1',"websites")
						->setCellValue('M1',"type")
						->setCellValue('N1',"image")
						->setCellValue('O1',"image_label")
						->setCellValue('P1',"small_image")
						->setCellValue('Q1',"small_image_label")
						->setCellValue('R1',"thumbnail")
						->setCellValue('S1',"thumbnail_label")
						->setCellValue('T1',"categories")
						->setCellValue('U1',"tax_class")
						->setCellValue('V1',"weight")
						->setCellValue('W1',"meta_title")
						->setCellValue('X1',"meta_description")
						->setCellValue('Y1',"meta_key")
						->setCellValue('Z1',"manufacturer")
						->setCellValue('AA1',"url_key");
			
			

	
				
        $i = 2;
        foreach($sql as $row){
              $objPHPExcel->setActiveSheetIndex(0)
						->setCellValue('A'.$i,$row['prod_sku'])
						->setCellValue('B'.$i,$row['prod_name'])
				//		->setCellValue('C'.$i,$row['short desc'])
						->setCellValue('D'.$i,$row['prod_long'])
						->setCellValue('E'.$i,$row['prod_rrp'])
						
						//start IF ELSE statement for Sale Price
						if ( ->setCellValue('F'.$i,$row['prod_sell_override']) == 0 ) {
						->setCellValue('F'.$i,$row['prod_trade']*100/70);
						} else {
						->setCellValue('F'.$i,$row['prod_sell_override'])
						}
						//End IF ELSE Statement
						
						->setCellValue('G'.$i,$row['prod_trade'])
			//			->setCellValue('H'.$i,$row['is in stock'])
			//			->setCellValue('I'.$i,$row['qty'])
			//			->setCellValue('J'.$i,$row['visibility'])
						->setCellValue('K'.$i,$row['prod_status'])
			//			->setCellValue('L'.$i,$row['websites'])
			//			->setCellValue('M'.$i,$row['type'])
			//			->setCellValue('N'.$i,$row['image'])
			//			->setCellValue('O'.$i,$row['image label'])
			//			->setCellValue('P'.$i,$row['small image'])
				//		->setCellValue('Q'.$i,$row['small image label'])
				//		->setCellValue('R'.$i,$row['thumbnail'])
			//			->setCellValue('S'.$i,$row['thumbnail label'])
			//			->setCellValue('T'.$i,$row['categories'])
			//			->setCellValue('U'.$i,$row['tax_class'])
						->setCellValue('V'.$i,$row['prod_weight'])
		//				->setCellValue('W'.$i,$row['meta_title'])
		//				->setCellValue('X'.$i,$row['meta_description'])
		//				->setCellValue('Y'.$i,$row['meta_key'])
		//				->setCellValue('Z'.$i,$row['manufacturer'])
		//				->setCellValue('AA'.$i,$row['url_key']);
          ;    $i++;
        }

        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle('export');


        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // Redirect output to a client’s web browser (Excel5)
//      header('Content-Type: application/vnd.ms-excel');
//      header('Content-Disposition: attachment;filename="export_DB_products.csv"');
//      header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
        $objWriter->save('../export/export_db_products.csv');

?>

Thanks in advance for any help

You cannot randomly put an if statement into your code. Like every language, PHP has a specific grammar.

 

You have to either create a separate statement with the if check and the setCellValue() call. Or you use the ternary operator, which yields a conditional expression:

...
->setCellValue('F', $i, $row['prod_sell_override'] ?: $row['prod_trade'] * 100 / 70)
...

Read: If $row['prod_sell_override'] is truthy (i. e. neither empty nor 0), use it as the cell value, otherwise use $row['prod_trade'].

Edited by Jacques1

Hi, Thanks for the input.  you may have guess put im still learning coding (and still a way to go haha)

 

ive changed the code to the line you provided, its not giving an error now (fantastic), but even though the DB is NULL or 0.00 it is putting the zero value into the export rather than the cost *100/70  

 

You cannot randomly put an if statement into your code. Like every language, PHP has a specific grammar.

 

You have to either create a separate statement with the if check and the setCellValue() call. Or you use the ternary operator, which yields a conditional expression:

...
->setCellValue('F', $i, $row['prod_sell_override'] ?: $row['prod_trade'] * 100 / 70)
...

Read: If $row['prod_sell_override'] is truthy (i. e. neither empty nor 0), use it as the cell value, otherwise use $row['prod_trade'].

  • Solution

Ive got it....  I changed the line to this and it seams to have worked.  just need to get phpexcel to export to 2 decimal places now.  thanks for pointing me in the right direction, very much appreciated :-)

->setCellValue('F'.$i,$row['prod_sell_override']>0 ? $row['prod_sell_override'] : $row['prod_trade']*100/70) 

 

You cannot randomly put an if statement into your code. Like every language, PHP has a specific grammar.

 

You have to either create a separate statement with the if check and the setCellValue() call. Or you use the ternary operator, which yields a conditional expression:

...
->setCellValue('F', $i, $row['prod_sell_override'] ?: $row['prod_trade'] * 100 / 70)
...

Read: If $row['prod_sell_override'] is truthy (i. e. neither empty nor 0), use it as the cell value, otherwise use $row['prod_trade'].

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.