Location: PHPKode > scripts > CTable > ctable/GenerateTable_Example.php
<?
/*
This is a sample function I created for one of my site layouts which uses CTable.  I pass an MS-SQL result resource to the function, and it generates three different table layouts ... one is a header which displays the database field names ... the second is the data contained within the result set ... and the third contains some form objects ... to work with the data displayed.

Some of the additional information contained within this table output will be added to the class in the future - the class thus far is in developmental stages ... I am working on creating a rollOver section for the class.  I have the JS written for both Mozzila and IE, but have not yet found a good way of adding it to the class itself.  

I will update the class when these functions are added.

If you have any questions, please feel free to email me at hide@address.com ... thank you -- Dave Higgins
*/

function GenerateTableLayout($result) {
	if(mssql_num_rows($result) > 0) {
		echo "<form name=\"search_output\" method=\"post\" action=\"results.php?mode=documents\"\n";
		$table = new CTable();
		$r = 1;
		$table->setClass('tStyle');
		$table->setCellpadding(2);
		$table->setCellspacing(0);
		$table->setBorder(1);
		$table->setWidth("100%");
		$table->Rows(mssql_num_rows($result));
		$row = 0;
		$cell=0;
		while($rs = mssql_fetch_array($result)) {
			if($table->GetCells() < 1) {
				$table->Cells((sizeof($rs)/2)+1); // field count, divided by two (remove integer indexs from equation, plus one for the Check cell
			}
			$row++;
			$cell=1;
			if($r == 1) {
				$header = new CTable();
				$header->setClass('tStyle');
				$header->setCellpadding(0);
				$header->setCellspacing(0);
				$header->setBorder(0);
				$header->setWidth("100%");				
				$header->Rows(1);
				$header->Cells((sizeof($rs)/2)+1);
				$header->SetHeader('class="tStyle"', 1);
				$header->SetHeader('class="tStyle" width="20px"', 1,1);
				$header->SetData("<b>Run</b>", 1,1);
				$hrow=1;
				$hcell=1;
				foreach($rs as $k=>$a) {
					if(!is_numeric($k)) {
						$hcell++;
						//echo "  <td class=\"tStyle\"><b>$k</b></td>\n";
						$header->SetHeader('class="tStyle" width="150px"', $hrow, $hcell);
						$header->SetData("<b>$k</b>", $hrow, $hcell);
					}
				}
				$header->Generate();
				unset($header);
			}
			//echo "<tr class=\"tUnchecked\" id=\"r$r\">\n";
			// onClick="document.getElementById(' . "'chk$r'" . ').checked = true;"  
			$table->SetHeader('class="tUnchecked" id="r' . $r . '" onMouseOver="FlipColor2(this, ' . "'tOver', 'tChecked'" . ')" onMouseOut="FlipColor2(this, ' . "'tUnchecked', 'tChecked'" . ')"', $row);
			//echo "  <td class=\"tStyle\"><input id=\"chk$r\" type=\"checkbox\" onClick=\"FlipColor(this, document.getElementById('r$r'))\" name=\"{$rs['escrow_no']}\">\n";
			$table->SetHeader('class="tStyle" width="20px"', $row, $cell);
			//echo "<!-- ADDING CHECKBOX -->\n";
			$table->SetData('<input id="chk'.$r.'" type="checkbox" onClick="FlipColor(this, document.getElementById(' . "'r$r')" . ')" name="'.$rs['escrow_no'].'">', $row, $cell);
			foreach($rs as $k=>$a) {
				if(!is_numeric($k)) {
					$cell++;
					//echo "<!-- ADDING CELL DATA: ROW($row) CELL($cell) -->\n";
					//if(@mssql_field_type($result, $cell) == 'datetime') {
					//  $a = @date('m/d/y', @strtotime($a));
					//}
					$table->SetHeader('class="tStyle" width="150px"', $row, $cell);
					$table->SetData($a==''?'&nbsp':$a, $row, $cell);
					echo "<!-- FIELD: $a -->\n";
				}
			}
			//echo "</tr>\n";
			$r++;
		}
		echo "<!-- ROWS: " . $table->GetRows() . "\tCELLS: " . $table->GetCells() . " -->";
		$table->Generate();
		unset($table);
		$footer = new CTable();
		$footer->setClass('tStyle');
		$footer->setCellpadding(5);
		$footer->setCellspacing(0);
		$footer->setBorder(0);
		$footer->setWidth("100%");				
		$footer->Rows(1);
		$footer->Cells(2);
		$footer->SetHeader('class="tStyle"', 1);
		$option = "<select class=\"Login\" size=1 style=\"width:100%\" name=\"reports\">\n";
		$option .= "  <option >Select a Document or Group</option>\n";
		$option .= "  <option >-----------------------</option>\n";
		$result = mssql_query("select document_group_id , document_group_name from document_groups order by document_group_name asc");
		while($rs = mssql_fetch_array($result)) {
		  $option .= "  <option >{$rs['document_group_name']}</option>\n";
		}
		$option .= "  <option >-----------------------</option>\n";
		$result = mssql_query("select document_id, document_name from documents order by document_name asc");
		while($rs = mssql_fetch_array($result)) {
			$option .= "  <option >{$rs['document_name']}</option>\n";
		}
		$option .= "</select>\n";
		$footer->SetHeader('width="100%"', 1,1);	
		$footer->SetData($option, 1, 1);
		$footer->SetData("<input class=\"menu-body\" name=\"result\" type=\"Submit\" value=\"Run Reports\">", 1, 2);
		$footer->Generate();
		unset($footer);
		echo "</form>\n";
		echo "<script>r=" . ($r-1) . ";</script>";
	} else {
		echo "There were no records in the result<br>\n";
	}
}
?>
Return current item: CTable