PHP - Moved: Phpexcel
This topic has been moved to Other Libraries and Frameworks.
http://www.phpfreaks.com/forums/index.php?topic=354411.0 Similar TutorialsI'm having an issue with a server I inherited. I have two files. The first file queries a mysql database and returns a table to the user. The second file uses phpexcel and php html dom parser to read the table and export the results to excel. I have it working with a small test page i created to test my code, but when I try to incorporate it into the inherited pages, it doesn't work. I think it's because of issues with validating the session or something of that nature, but I'm not sure, and I'm not sure how to fix it. The following are the two pages that I'm trying to get working together. View Tables Code: [Select] <?php session_start(); //Start the session. //if no session value is present, redirect the user: if(!isset($_SESSION['idUser'])) { require_once('includes/login_functions.inc.php');//Need the functions to create an absolute URL: $url = absolute_url(); header("Location: $url"); exit(); //Quit the script. } //******************************************************* $page_title = 'View Tickets'; include ('includes/header.html'); include ('includes/tdformatting_functions.inc.php'); require_once('mysqli_connect.php'); include ('includes/calstyle.css'); ... code to generate table via mysql/php ... //when clicked this link should open OutputToXL.php and create an Excel file with contents from current page. echo '<td><a href="../Test/OutputToXL.php?host='.$_SERVER['HTTP_HOST'].'&page='.$_SERVER['PHP_SELF'].'" target="_blank"><img src="images/exexcel.png" width="80" height="17" title="Export to Excel."></a></td>'; ?> Output to Excel Code: [Select] <?php session_start(); //Start the session. //if no session value is present, redirect the user: if(!isset($_SESSION['idUser'])) { require_once('includes/login_functions.inc.php');//Need the functions to create an absolute URL: $url = absolute_url(); header("Location: $url"); exit(); //Quit the script. } $page_title = 'New Excel'; include ('includes/header.html');//This Header comes out correctly, so I believe the authentication is working at this point include ('includes/tdformatting_functions.inc.php'); require_once('mysqli_connect.php'); include ('includes/calstyle.css'); /** Include path **/ ini_set('include_path', ini_get('include_path').'../Utility/File/Excel/Classes/'); /** PHP Simple HTML DOM Parser */ include ('../Test/simplehtmldom/simple_html_dom.php'); /** PHPExcel */ include ('../Utility/File/Excel/Classes/PHPExcel.php'); /** PHPExcel_Writer_Excel2003 */ include ('../Utility/File/Excel/Classes/PHPExcel/Writer/Excel5.php'); //Create new PHPExcel object echo date('H:i:s') . " Create new PHPExcel object <br>\n"; $objPHPExcel = new PHPExcel(); // Set properties echo date('H:i:s') . " Set properties <br>\n"; $objPHPExcel->getProperties()->setCreator("Roy Jacob"); $objPHPExcel->getProperties()->setLastModifiedBy("Roy Jacob"); $objPHPExcel->getProperties()->setTitle("Office 2003 XLS Test Document"); $objPHPExcel->getProperties()->setSubject("Office 2003 XLS Test Document"); $objPHPExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes."); //Thes variables are passed from the page I want to export $host = $_GET['host']; $page = $_GET['page']; $urlfile = 'http://' . $host . $page; echo $urlfile . '<br>'; $html = file_get_html($urlfile); //this is a test echo which I find a problem with. The page that is put into $html is not the same one that is being processed below. Below it's processing login.php instead of the passed page above. foreach($html->find('a') as $element) echo $element->href . '<br>'; // Add some data echo "<br>" . date('H:i:s') . " Adding Table data <br>\n"; $objPHPExcel->setActiveSheetIndex(0); //Parse Entire Table into Rows echo "<br>" . date('H:i:s') . " Parsing Table <br>\n"; //$tablematch = preg_match_all('/<tr(\s*?)(.*?)(\s*?)(.*?)(\s*?)<\/tr>/',$stringresults,$trs); $nextrow = 1; $nextcol = 0; foreach($html->find('table#results') as $resultstable) { foreach($resultstable->find('tr') as $row) { foreach($row->find('th') as $header) { $hcell = $header->plaintext; echo $hcell . ": " . $nextrow . ", " . $nextcol . "<br>\n"; $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($nextcol,$nextrow, ($hcell)); $nextcol++; } $nextcol = 0; foreach($row->find('td') as $Cells) { $dcell = $Cells->plaintext; echo $dcell . ": " . $nextrow . ", " . $nextcol . "<br>\n"; $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($nextcol,$nextrow, ($dcell)); $nextcol++; } $nextrow++; } } // Rename sheet echo date('H:i:s') . " Rename sheet <br>\n"; $objPHPExcel->getActiveSheet()->setTitle('HTMLTableOutput'); // redirect output to client browser echo date('H:i:s') . " Write to Excel2003 format <br>\n"; header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="Output.xls"'); header('Cache-Control: max-age=0'); $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $objWriter->save(str_replace('.php', '.xls', __FILE__)); //$objWriter->save('php://output'); // Echo done echo date('H:i:s') . " Done writing file. <br>\r\n"; //} ?> I don't know where the problem is originating, but I'm assuming the function is being redirected to the log in page of the site instead of the referring page. Anyone know how to fix this? I'm currently looping on an array with this structu Categories{ CategoryName CategoryCode CategoryDescription Products{ product_info{ product_code product_type{ CountNumber ProductDescription } } prices{ "wholesale":"250", "retail":"400" } } }
I'm looping on the above array at each level, and I've declared an array so that I can put all my needed values into it $priceResult = array(); foreach($prices->categories as $category){ $categoryName = $category->category_name; $categoryCode = $category->category_code; $categoryDescription = $category->category_desc; foreach($category->products as $product){ foreach($product->product_info as $info){ $product_code = $info->product_code; foreach($info->product_type as $type){ $CountNumber = $type->CountNumber; $ProductDescription = $type->ProductDescription; } } foreach ($product->prices as $price => $amount) { $price_amount = $amount; } } } The problem I'm having is I don't know how to properly push into that new ```$priceResult``` array so that I can use PHPExcel to put it into a format with a subheader. The format I would want from the above example would be something like this
Test Category 1 | 123 | Category for Testing
Test Category 2 | 321 | New Category for Testing So basically I want to call PHPExcel on my $priceResult array in order to get that format where I can list my category info, and for each product within that category, I'd have a product row. Everything would be grouped by the category main header $build = Excel::create($name, function ($excel) use ($priceResult) { $excel->setTitle('Test Products'); UPDATE:
CategoryCode : 123 CategoryName : TestCategory CategoryDescription: For Testing Products{ 0{ Product_code : 123, CountNumber : 12, ProductDescription: Test Product, price_amount : 150.00 }, 1{ Product_code : 112, CountNumber : 32, ProductDescription: Test Product 2, price_amount : 250.00 } }
This topic has been moved to PHP Freelancing. http://www.phpfreaks.com/forums/index.php?topic=331097.0 This topic has been moved to PHP Freelancing. http://www.phpfreaks.com/forums/index.php?topic=345722.0 This topic has been moved to PHP Freelancing. http://www.phpfreaks.com/forums/index.php?topic=349322.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=309960.0 This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=356314.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=318465.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=319767.0 This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=305825.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=342919.0 This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=353027.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=343318.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=313579.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=346829.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=316254.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=356760.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=342987.0 This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=327250.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=317014.0 |