Solved

How to add pagination to html table generated from .csv file

Posted on 2011-03-10
10
735 Views
Last Modified: 2012-05-11
Hi experts,

I have an alphabetized table containing a lot of data to which I would like to add page numbers to (pagination). Here is the code as it stands:

<?php

     echo"<table border=\"0\"><tr>";
     foreach (range('A', 'Z') as $letter) {
        echo"<td><a href=\"rates.php?value=$letter\">$letter</a></td>";
     }    
     echo"</tr></table>";

      echo "<html><body><table>\n\n"; $f = fopen("rate.csv", "r");
      do{        

      if(strtoupper(substr($line[0],0,1)) == $_GET[value]){
          echo "<tr width=500>";        
          echo "<td width=200>" . $line[0]. "</td>";
          echo "<td width=100>" . $line[1]. "</td>";
          echo "<td width=100>" . $line[2]. "</td>";
          echo "<td width=100>" . $line[3]. "</td>";
          echo "<tr>";

        }
      }

      while (($line = fgetcsv($f)) !== false);
     
      fclose($f);

echo "\n</table></body></html>";

?>

Thanks
0
Comment
Question by:daz1234
  • 6
  • 3
10 Comments
 
LVL 19

Expert Comment

by:Bardobrave
ID: 35096644
Maybe this link provides some help to you

http://www.roscripts.com/PHP_pagination-70.html
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35096806
Can you post at least a piece of that csv file ?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35097058
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:daz1234
ID: 35097072
Yes, that is the csv!
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 35098449
Well............ It's alive ! This is what I've done, the code needs a little adjustments but tell us if this is what you want:

<?php

     // THE ALPHABET
     echo"<table border=\"0\"><tr>";
     foreach (range('A', 'Z') as $letter) {
        echo"<td><a href=\"rates.php?value=$letter\">$letter</a></td>";
     }    
     echo"</tr></table>";

    // for counting
    $f = fopen("rate.csv", "r"); 
    
    // counter
    $i = 0;
    while($line = fgetcsv($f)){
        $array[$i][0] = $line[0];
        $array[$i][1] = $line[1];
        $array[$i][2] = $line[2];
        $array[$i][3] = $line[3];
        $i++;
    }
    
    // substract the head line from csv
    //$rows = $i - 1;
    $ipp = 2; // items per page
    
    fclose($f); 
    
    echo"<table>";
    
    if(!empty($_GET[value])){
    
        // remove the description line
        array_shift($array);
    
        $j = 0;
        foreach($array as $key => $value){
            // let's get the new array with only a specific letter
            if(strtoupper(substr($array[$key][0],0,1)) == $_GET[value]){
                $country[$j][0] = $array[$key][0];
                $country[$j][1] = $array[$key][1];
                $country[$j][2] = $array[$key][2];
                $country[$j][3] = $array[$key][3];
                $j++;            
            }
        }
        
        // j is the number of rows
        // pages
        $pages = ceil($j / $ipp);
        

        echo"<tr><td colspan='5'>";
        echo"number of pages for letter $_GET[value] is $pages<br />page: ";
        
            for($i = 1;$i<=$pages;$i++){
                echo"<a href=\"rates.php?value=$_GET[value]&amp;page=$i\">$i</a> | ";
            }
        echo"</td></tr>";  
        
        // get the current page
        if(empty($_GET[page]) || !isset($_GET[page])) $page = 1;
        else $page = $_GET[page];	
        
        $startitem = ($page - 1)*$ipp;
        $enditem = $startitem + $ipp;
        
        echo"$startitem to $enditem";
        
            // output the pages from array
            for($i = $startitem;$i<$enditem;$i++){
                  echo "<tr width=500>";        
                  echo "<td width=200>" . $country[$i][0]. "</td>";
                  echo "<td width=100>" . $country[$i][1]. "</td>";
                  echo "<td width=100>" . $country[$i][2]. "</td>";
                  echo "<td width=100>" . $country[$i][3]. "</td>";
                  echo "<tr>";             
                //echo"<a href=\"rates.php?value=$_GET[value]&amp;page=$i\">$i</a> | ";
            }        
        
    
    }
    else{
        foreach($array as $key => $value){
              echo "<tr width=500>";        
              echo "<td width=200>" . $array[$key][0]. "</td>";
              echo "<td width=100>" . $array[$key][1]. "</td>";
              echo "<td width=100>" . $array[$key][2]. "</td>";
              echo "<td width=100>" . $array[$key][3]. "</td>";
              echo "<tr>"; 
              }
    }
    
    echo"</table>";    
    

?>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35098493
if you need more items just change
$ipp = 2; // items per page
0
 

Author Comment

by:daz1234
ID: 35100009
Thanks Roads, I will check it out in the morning and let you know. One other thing, the very first line in the .csv file is actually the headings for the columns in the table - any way that first line could be omitted altogether? It shows under D as the first heading is Destination. I can just add a <tr> with the headings instead. Thanks!
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35106037
In the solution - it is omitted.
0
 

Author Comment

by:daz1234
ID: 35107783
Roads, great coding - worked perfectly! Thanks for all your help with this.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35107801
No problem, cheers !
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to count occurrences of each item in an array.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question