Jump to content

Export to Excel PHP with foreach


dpalame

Recommended Posts

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
Share on other sites

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
Share on other sites

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.