Solved

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

Posted on 2011-03-10
10
758 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
[X]
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
  • 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
Independent Software Vendors: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

724 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