paggination of imported csv file help
hi,
i am importing a csv file using fgetcsv() function
and i want to paginate my records
let say i have thhousend rows
but i want to show them 20 by 20 per page
my code looks like
PHP Code:
<?php
$row = 1 ;
$handle = fopen ( "test.csv" , "r" );
while (( $data = fgetcsv ( $handle , 1000 , "," )) !== FALSE ) {
$num = count ( $data );
echo "<p> $num fields in line $row : <br /></p>\n" ;
$row ++;
for ( $c = 0 ; $c < $num ; $c ++) {
echo $data [ $c ] . "<br />\n" ;
}
}
fclose ( $handle );
?>
please help me out
many thnx
Here's one way to do it, it's a bit basic but I didn't want to spend too long on it :
PHP Code:
<?php
$offset = isset( $_GET [ 'offset' ])? $_GET [ 'offset' ]: 0 ;
$LinesToDisplay = 20 ;
$row = ( $offset == 0 )? 1 : $offset ;
$handle = fopen ( "test.csv" , "r" );
for ( $i = 0 ; $i < $offset - 1 ; $i ++)
{
fgetcsv ( $handle , 1000 , "," );
}
while ((( $data = fgetcsv ( $handle , 1000 , "," )) !== FALSE ) && ( $LinesToDisplay -- > 0 )) {
$num = count ( $data );
echo "<p> $num fields in line $row : <br /></p>\n" ;
$row ++;
for ( $c = 0 ; $c < $num ; $c ++) {
echo $data [ $c ] . "<br />\n" ;
}
}
fclose ( $handle );
?>
<a href="test.php?offset=<?php echo $row ; ?> ">Next 20</a>
Thnx too much!
Originally Posted by
cridley
Here's one way to do it, it's a bit basic but I didn't want to spend too long on it :
PHP Code:
<?php
$offset = isset( $_GET [ 'offset' ])? $_GET [ 'offset' ]: 0 ;
$LinesToDisplay = 20 ;
$row = ( $offset == 0 )? 1 : $offset ;
$handle = fopen ( "test.csv" , "r" );
for ( $i = 0 ; $i < $offset - 1 ; $i ++)
{
fgetcsv ( $handle , 1000 , "," );
}
while ((( $data = fgetcsv ( $handle , 1000 , "," )) !== FALSE ) && ( $LinesToDisplay -- > 0 )) {
$num = count ( $data );
echo "<p> $num fields in line $row : <br /></p>\n" ;
$row ++;
for ( $c = 0 ; $c < $num ; $c ++) {
echo $data [ $c ] . "<br />\n" ;
}
}
fclose ( $handle );
?>
<a href="test.php?offset=<?php echo $row ; ?> ">Next 20</a>
thnx too much cridley
it really helps me
i do little change
i show the header row at every offset
i hope you wana see
PHP Code:
/*
* pagination stuff
* defining values
*/
$perPage = 20 ;
$totalRec = $_SESSION [ 'pagi' ][ 'total' ];
$totalPage = ceil ( $_SESSION [ 'pagi' ][ 'total' ] / $perPage );
$currentPage = !(isset ( $_REQUEST [ 'nav' ])) || $_REQUEST [ 'nav' ] == "" ? 1 : $_REQUEST [ 'nav' ];
$startFrom = ( $currentPage - 1 ) * $perPage ;
$endAt = $startFrom + ( $perPage + 2 );
if ( $currentPage > 1 ) {
$startFrom += 2 ;
}
//csv data in tabular form
$row = 1 ;
$file = './temp/' . $_SESSION [ 'log' ][ 'csvFile' ];
if ( file_exists ( $file )) {
$handle = fopen ( $file , "r" );
} else {
echo '<p class="error">file not found</p>' ;
exit ();
}
echo '<div class="cp-div">' ;
echo '<table border="0" cellpadding="4" cellspacing="0" widht="100%">' ;
while (( $data = fgetcsv ( $handle , 1000 , "," )) !== FALSE ) {
$num = count ( $data );
$row ++;
if ( $row == 2 ) {
echo '<tr>' ;
for ( $c = 0 ; $c < $num ; $c ++) {
echo '<th align="left" nowrap="nowrap">' . $data [ $c ] . '</th>' ;
}
echo '</tr>' ;
}
elseif ( $row > $startFrom && $row <= $endAt ) {
echo '<tr>' ;
for ( $c = 0 ; $c < $num ; $c ++) {
if ( $c == 0 ) {
echo '<td class="label" style="border-bottom:1px solid #333333">' . $data [ $c ] . '</td>' ;
}
elseif ( $c == 1 && $row == 3 ) {
$keyBlogChk = $_SESSION [ 'log' ][ 'keyblog' ] == "yes" ? 'checked="checked"' : '' ;
echo '<td style="border-bottom:1px solid #333333;">' .
'<input type="checkbox" id=fld[$row][$c] name="fld[$row][$c]"' . $keyBlogChk . ' /></td>' ;
}
elseif ( $c == 1 && $row != 3 ) {
echo '<td style="border-bottom:1px solid #333333;"> </td>' ;
} else {
echo '<td style="border-bottom:1px solid #333333;">' .
'<input type="text" name="fld[]" value="' . $data [ $c ] . '" class="txt-fld-normal" size="15" />' .
'</td>' ;
}
}
echo '</tr>' ;
}
}
$totalPage = ceil ( $_SESSION [ 'pagi' ][ 'total' ] = $row / $perPage );
echo '</table>' ;
echo '</div>' ;
echo '<table border="0" cellpadding="4" cellspacing="0" widht="100%">' ;
echo '<tr>' .
'<td colspan="7"align="center" class="label">' ;
if( $currentPage == 1 ) {
echo '<<First <Previous' ;
}
else {
$prev = $currentPage - 1 ;
echo '<a href="./"> <<First</a> ' ;
echo '<a href="./?nav=' . $prev . '"> <Previous</a> ' ;
}
echo ' <small>Page ' . $currentPage . ' of ' . $totalPage . '</small> ' ;
if ( $currentPage == $totalPage ) {
echo 'Next>> ' ;
echo 'Last>> ' ;
} else {
$next = $currentPage + 1 ;
echo '<a href="./?nav=' . $next . '"> Next></a> ' ;
echo '<a href="./?nav=' . $totalPage . '"> Last>></a> ' ;
}
echo '</td>' .
'</tr>' .
'</table>' ;
fclose ( $handle );
?>
thnx any way
Last edited by haroon373; 10-11-2007 at 01:26 AM .
Reason: mistake
Since you are both the experts :-D
Can you help me with my script ?
I've tried to merge your scripts with mine, but all i get is a page that is unsorted, this is my original script
PHP Code:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body link=#812990 vlink=#812990 alink=#812990 text=black>
</html>
<?PHP
$file_handle = fopen ( "web.txt" , "rb" );
while (! feof ( $file_handle ) ) {
$parts = fgetcsv ( $file_handle , 4096 , "," );
if ( $parts [ 7 ] >= 9998 )
{
$parts [ 1 ] = ucwords ( strtolower ( $parts [ 1 ]));
$parts [ 2 ] = ucwords ( strtolower ( $parts [ 2 ]));
echo "<p><a class='popup4' href='#'><font face=Humanist777BT-ExtraBlackB><b> $parts [ 1 ] </b></font><table><tr><td><i><span class='comment'> $parts [ 2 ] </span></td></tr></table></i></a>" ;
echo "<TABLE BORDER='0' cellpadding='0' CELLSPACING='0'><TR><TD WIDTH='100' HEIGHT='20' BACKGROUND='pricebackground.jpg' VALIGN='bottom'><center>€" . $parts [ 4 ] . "</center></td></tr></table><br>" ;
echo "<b><font size=3 color=#D7182A>Op Voorraad In:<br></font>" ;
echo ( " Amsterdam " );
if ( $parts [ 5 ] >= 1 ) echo ( "<IMG SRC =green.gif>" ). "" ;
if ( $parts [ 5 ] == 0 ) echo ( "<IMG SRC =red.gif>" ). "" ;
echo ( " Utrecht " );
if ( $parts [ 6 ] >= 1 ) echo ( "<IMG SRC =green.gif>" ). "<BR><br></b>" ;
if ( $parts [ 6 ] == 0 ) echo ( "<IMG SRC =red.gif>" ). "<BR><br></b>" ;
}}
fclose ( $file_handle );
?>
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks