Solved

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

Posted on 2011-03-10
10
702 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
Comment Utility
Maybe this link provides some help to you

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

Expert Comment

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

Expert Comment

by:Lukasz Chmielewski
Comment Utility
0
 

Author Comment

by:daz1234
Comment Utility
Yes, that is the csv!
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 27

Expert Comment

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

Author Comment

by:daz1234
Comment Utility
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
Comment Utility
In the solution - it is omitted.
0
 

Author Comment

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

Expert Comment

by:Lukasz Chmielewski
Comment Utility
No problem, cheers !
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now