Hi php user,
I know how to paginate a table if the file name is fixed. But If my file name is passed from another php page then how to deal with such problem.
The first page is shown correctly but when i click on the link for other page it shows error. This is because of the missing filename.
Any idea?
Thanks
<head>
<link href="tableGreenAlt.css" rel="stylesheet" type="text/css"/>
</head>
<script type="text/javascript" src="skinnytip.js">
</script>
<?php
if (!isset($_GET['filetoview']))
{
echo ("File can not be viewed!!!!!!!<br> ");
}
$filetoview = $_GET['filetoview'];
$lines = file($_SERVER['DOCUMENT_ROOT'] . $filetoview);
$line_amount = count($lines);
$headerline = $lines[0];
$headerdata = preg_split('/\t/', $headerline);
$ncolumns = count($headerdata);
$perpage = 10; #This number specified how many lines to show on a page.
echo"<div id=\"tiplayer\" style=\"position:absolute; visibility:hidden; z-index:10000;\"></div>";
echo"<a href= ".$filetoview. " target=\"_blank\">";
echo"<center><img src=\"download.png\" onMouseOver=\" return tooltip('Right click and Save Link As', 'To Download File' ,'width:200');\" onMouseOut=\"return hideTip();\"/></a></font></center>";
?>
<body>
<table id="tableGreenAlt" >
<thead>
<tr>
<?php
for ($i=0; $i< $ncolumns; $i++)
echo "<th>".$headerdata[$i]."</th>";
?>
</tr> </thead>
<tbody>
<?php
$color = '#cccccc'; //initial color
//print ($ncolumns);
$p = isset($_GET['p']) ? $_GET['p'] : 1;
for ($i = (($p * $perpage) - $perpage+1); $i <= (($perpage * $p)); $i++){
if($i >= $line_amount){
break;
}
else{
if($lines[$i] != ''){# This is the output loop.
//echo ''.$lines[$i].'<br />';
$data = preg_split('/\t/', $lines[$i]);
// set to second color if already initial color, otherwise set back to initial color
echo "<tr align =left bgcolor=".$color.">";
for ($j=0; $j < $ncolumns; $j++)
{
echo "<td>".$data[$j]."</td>";
}
echo"</tr>";
$color = ($color == '#cccccc')?'#EAF2D3':'#cccccc';
//$countRows = $countRows+1;
}
}
}
?>
</tbody>
</table>
<table summary="" cellpadding="20" cellspacing="10" border="0" >
<tr>
<?
$total_pages = ceil($line_amount/$perpage);
$dn = $p-5;
$up = $p+5;
if ($dn < 1 ) { $up = $up + (1 - $dn);}
if ($up > $total_pages) {$dn = $dn - ($up - $total_pages); $up = $total_pages;}
if ($dn < 1) { $dn = 1;}
echo "<td align=center width='30%' /td>";
if($p!=1)
{
$back_page=$p-1;
echo "<td ><a href='?p=1'><font face='Verdana' size='2' > First </font></a></td>";
echo "<td ><a href='?p=$back_page'><font face='Verdana' size='2' > Previous </font></a></td>";
}
else
{
echo "<td ><font face='Verdana' size='2' > First </font></a></td>";
echo "<td ><font face='Verdana' size='2' > Previous </font></a></td>";
}
for($j=$dn;$j<=$up;$j++)
{
if($j==$p) //the current page
{
echo "<td ><font face='Verdana' size='4' color =red>[$p]</font></td>";
}
else
{
echo "<td ><a href='?p=$j'> <font face='Verdana' size='2' > $j</font></a></td>";
}
}
if($p < $total_pages){
$next_page=$p+1;
echo "<td ><a href='?p=$next_page'> <font face='Verdana' size='2' > Next</font></a></td>";
echo "<td ><a href='?p=$total_pages'><font face='Verdana' size='2' > Last</font></a></td>";
}
else
{
echo "<td ><font face='Verdana' size='2' > Next</font></a></td>";
echo "<td ><font face='Verdana' size='2' > Last</font></a></td>";
}
?>
</tr>
</table>
Passing the filename in the <a href=.....&filetoview> will solve the problem