Javascript with PHP Loop

Hi Chaps,

I have a PHP loop script that returns Job Information for each Project.

What I’ve got so far is an Export to Excel (2007) function that displays the Project information on 1st tab, and the Job information on the 2nd.

The problem I’ve got is that the Export to Excel function, gathers the information from the HTML table ID, but when looping through the data (resulting in more than 1 Job for a Project), the only way I can differentiate is to use some PHP code, something like:

id=“tbl_job_<?php echo $job_id; ?>”

What I basically want is to store the information for each Job on a different Excel tab.

But whart I have seems to break the javascript:

<script language="javascript" type="text/javascript">  
function ExportToExcel() {		
			var xlApp = new ActiveXObject("Excel.Application");
			// Silent-mode:
			xlApp.Visible = true;
			xlApp.DisplayAlerts = false;
			var xlBook = xlApp.Workbooks.Add();
			xlBook.worksheets("Sheet1").activate;
			var XlSheet = xlBook.activeSheet;
			XlSheet.Name="Project";
			
			// Store the sheet header names in an array
			var rows = tbl_project.getElementsByTagName("tr");
			var columns = tbl_project.getElementsByTagName("th");
			var data = tbl_project.getElementsByTagName("td");
			
  			// Set Excel Column Headers and formatting from array
			for(i=0;i<columns.length;i++){
	   			XlSheet.cells(3,i+1).value= columns[i].innerText; //XlSheetHeader[i];
			}
			
			//run over the dynamic result table and pull out the values and insert into corresponding Excel cells
			var d = 0;
			for (r=4;r<rows.length+3;r++) { // start at row 2 as we've added in headers - so also add in another row!
				for (c=1;c<columns.length+1;c++) {
					XlSheet.cells(r,c).value = data[d].innerText;
					d = d + 1;
				}
			}
			
			//autofit the columns
			XlSheet.columns.autofit;
									
			xlBook.worksheets("Sheet2").activate;
			var XlSheet2 = xlBook.activeSheet;
			XlSheet2.Name="Job";
			
			// Store the sheet header names in an array
			var rows = tbl_job.getElementsByTagName("tr");
			var columns = tbl_job.getElementsByTagName("th");
			var data = tbl_job.getElementsByTagName("td");
			
  			// Set Excel Column Headers and formatting from array
			for(i=0;i<columns.length;i++){
	   			XlSheet2.cells(3,i+1).value= columns[i].innerText; //XlSheetHeader[i];
			}
			
			//run over the dynamic result table and pull out the values and insert into corresponding Excel cells
			var d = 0;
			for (r=4;r<rows.length+3;r++) { // start at row 2 as we've added in headers - so also add in another row!
				for (c=1;c<columns.length+1;c++) {
					XlSheet2.cells(r,c).value = data[d].innerText;
					d = d + 1;
				}
			}
			
			//autofit the columns
			XlSheet2.columns.autofit;
			
			// Make visible:
			xlApp.visible = true;
			xlApp.DisplayAlerts = true;
			CollectGarbage();
			//xlApp.Quit();
			
}
</script>

This is the bit I think I need to change to get the JS to work, but I’m a bit out of my depth!

xlBook.worksheets(“Sheet2”).activate;
var XlSheet2 = xlBook.activeSheet;
XlSheet2.Name=“Job”;

// Store the sheet header names in an array
var rows = tbl_job.getElementsByTagName(“tr”);
var columns = tbl_job.getElementsByTagName(“th”);
var data = tbl_job.getElementsByTagName(“td”);

Any helpor guidence would be awesome!