dpalame Posted August 13, 2011 Share Posted August 13, 2011 I have been ripping my hair out for a while now trying to find out why the following code only outputs the last variable of the array $at1. foreach ($at1 as $var) { $query = "SELECT wfStoreList.* FROM wfStoreList LEFT JOIN wfData ON wfStoreList.store=wfData.wfstore AND wfData.wfupc LIKE '%$var%' WHERE wfData.wfstore IS NULL ORDER BY region,store ASC "; $result=mysql_query($query); $num=mysql_num_rows($result); $querySpec = "SELECT * FROM wfItemSpecs WHERE upc LIKE '%$var%'"; $resultSpec=mysql_query($querySpec); $rowSpec = mysql_fetch_array($resultSpec); $brand=$rowSpec['brand']; $description=$rowSpec['description']; $pack=$rowSpec['pack']; //Build Result String $display_string = "<div style=\"width:840px;overflow:auto;\"><table class=\"itemList\">"; $display_string .= "<tr>"; $display_string .= "<th>Brand</th>"; $display_string .= "<th>UPC</th>"; $display_string .= "<th>Description</th>"; $display_string .= "<th>Pack</th>"; $display_string .= "<th>Region</th>"; $display_string .= "<th>Store</th>"; $display_string .= "</tr>"; if ($num == 0) echo ""; else for($i = 0; $i<$num; $i++){ ($row = mysql_fetch_array($result)); if($i % 2) { //this means if there is a remainder $bgcolor='#ffffcc'; } else { //if there isn't a remainder we will do the else $bgcolor='#fff'; } $display_string .= "<tr style=\"background-color:$bgcolor;border: 1px solid #999;\">"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$brand</td>"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$var</td>"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$description</td>"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$pack</td>"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$row[region]</td>"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$row[store]</td>"; $display_string .= "</tr>"; } if ($num == 0){ echo ""; }else{ $display_string .= "</table></div>"; } } header("Content-type: application/msexcel"); header("Content-Disposition: attachment; filename=whole_foods_void_report.xls"); header("Pragma: no-cache"); header("Expires: 0"); print "$display_string\n$data"; } If I put the headers in the foreach loop then all the data for each variable in the array is output, but in between variable results in tries to reset the headers and prints out header error messages. I know this can't be too complicated, but I am a noob and can't find an answer. Any help would be appreciated. Thanks. Link to comment https://forums.phpfreaks.com/topic/244666-export-to-excel-php-with-foreach/ Share on other sites More sharing options...
dpalame Posted August 13, 2011 Author Share Posted August 13, 2011 Has anybody had a chance at this? Any ideas I am still scouring for an answer. Thank you for any help. Link to comment https://forums.phpfreaks.com/topic/244666-export-to-excel-php-with-foreach/#findComment-1256781 Share on other sites More sharing options...
dpalame Posted August 13, 2011 Author Share Posted August 13, 2011 I knew it was an easy fix! Here is the code for anybody trying to accomplish this: header("Content-type: application/msexcel"); header("Content-Disposition: attachment; filename=whole_foods_void_report.xls"); header("Pragma: no-cache"); header("Expires: 0"); foreach ($at1 as $var) { $query = "SELECT wfStoreList.* FROM wfStoreList LEFT JOIN wfData ON wfStoreList.store=wfData.wfstore AND wfData.wfupc LIKE '%$var%' WHERE wfData.wfstore IS NULL ORDER BY region,store ASC "; $result=mysql_query($query); $num=mysql_num_rows($result); $querySpec = "SELECT * FROM wfItemSpecs WHERE upc LIKE '%$var%'"; $resultSpec=mysql_query($querySpec); $rowSpec = mysql_fetch_array($resultSpec); $brand=$rowSpec['brand']; $description=$rowSpec['description']; $pack=$rowSpec['pack']; //Build Result String $display_string = "<div style=\"width:840px;overflow:auto;\"><table class=\"itemList\">"; $display_string .= "<tr>"; $display_string .= "<th>Brand</th>"; $display_string .= "<th>UPC</th>"; $display_string .= "<th>Description</th>"; $display_string .= "<th>Pack</th>"; $display_string .= "<th>Region</th>"; $display_string .= "<th>Store</th>"; $display_string .= "</tr>"; if ($num == 0) echo ""; else for($i = 0; $i<$num; $i++){ ($row = mysql_fetch_array($result)); if($i % 2) { //this means if there is a remainder $bgcolor='#ffffcc'; } else { //if there isn't a remainder we will do the else $bgcolor='#fff'; } $display_string .= "<tr style=\"background-color:$bgcolor;border: 1px solid #999;\">"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$brand</td>"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$var</td>"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$description</td>"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$pack</td>"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$row[region]</td>"; $display_string .= "<td style=\"white-space:normal;font-weight:bold;vertical-align:middle;\">$row[store]</td>"; $display_string .= "</tr>"; } if ($num == 0){ echo ""; }else{ $display_string .= "</table></div>"; } print "$display_string"; } Link to comment https://forums.phpfreaks.com/topic/244666-export-to-excel-php-with-foreach/#findComment-1256782 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.