?
Solved

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

Posted on 2011-03-10
10
Medium Priority
?
773 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

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

Accepted Solution

by:
Lukasz Chmielewski earned 2000 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

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

770 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