Saturday, May 12, 2007

PHP DataTable Displaying and Paging



I have made a function to fetch data from database and show it in a HTML table. It's easy to use but hard to read. HTML text has been linked together.




function getResultAsTable ($actions, $result)
{
if ($rowcount =pg_num_rows($result)>0)
{
$resultHTML= "<table border="'1'" align="'center'">\n<tr bgcolor="'#e8eefa'">\n";

//Output the table header
$fieldCount = pg_num_fields($result);
for ($i=0; $i < $fieldCount; $i++)
{
$rowName = pg_field_name($result,$i);
$resultHTML .= "<th valign="'middle'">$rowName</th>\n";
}

$resultHTML .= "<th>Actions</th></tr>\n";

//output the table body
while ($row = pg_fetch_row($result))
{
$resultHTML .="\n";
for ($i=0; $i < $fieldCount; $i++)
{
$resultHTML .="<td>".htmlentities($row[$i])."</td>\n";
}

//Replace VALUE with proper primary key
$action = str_replace("VALUE", $row[0], $actions);
//Add action cell to the end of each row
$resultHTML .= "<td>$action</td>\n</tr>\n";

}

//Close table
$resultHTML .="</table>\n";
}
else
{
$resultHTML = "No Result Found";
}
return $resultHTML;
}


function pageLinks($totalpages, $currentpage, $pagesize, $parameter)
{
// Start at page one
$page = 1;
// Start at record 0
$recordstart = 0;

// Initialize page links
$pageLinks = '';
while ($page <= $totalpages) {
// Link the page if it isn't the current one
if ($page != $currentpage) {
$pageLinks .= "<a href="http://www.blogger.com/%5C"
parameter="$recordstart\">$page</a> ";
// If the current page, just list the number
}
else {
$pageLinks .= "$page ";
}
// Move to the next record delimiter
$recordstart += $pagesize;
$page++;
}

return $pageLinks;
}

No comments: