Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2011-03-10
10
722 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

808 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