Solved

how to create alphabetic pagination in php

Posted on 2011-03-03
12
829 Views
Last Modified: 2017-05-13
Hello experts,

I hope someone can help me. I have a .csv file that I am loading into a php table - a simple database of country names and corresponding numeric entries.
What I would like is to create alphabetic pagination so a user clicks on a letter for a top list A B C etc and the A countries load. See below for the basic code that I have used to fill a table with the entries which displays as one ling list.

Thanks!

<?php echo "<html><body><table>\n\n"; $f = fopen("includes/rate.csv", "r"); while (($line = fgetcsv($f)) !== false) {        
      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>\n"; } fclose($f); echo "\n</table></body></html>";
?>
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
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35025700
Can you include csv file for that ?
0
 
LVL 4

Expert Comment

by:mars-vie
ID: 35025709
Hello,

I don't know how many data is in the csv-file but you could load the csv-data in an array.
You could then use the PHP functions sort or array_filter
If it's alot of data a html-table would not be a good idea.
0
 

Author Comment

by:daz1234
ID: 35026552
Rates file included, I have cut a lot of the data out, just enough to show the layout, it is a very long file. Here are the first two lines to give an idea:

Destination,Dollar rate,Euro,Sterling
Afghanistan,0.2235,0.1624,0.1389

Thanks rates.csv
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 3

Expert Comment

by:pius_babbun
ID: 35026895
Check if this option suites your requirement
<?php
$sort_value = $_REQUEST['value'];

$query = "SELECT filed1,filed2,filed3,filed4,...filed n FROM table_name filed_name LIKE %$sort_value% ";

?>

Open in new window

<table width="100%" border="0">
  <tr>
    <td><a href="filename?value=a">A</a></td>
    <td><a href="filename?value=n">B</a></td>
    <td><a href="filename?value=c">C</a></td>
    <td><a href="filename?value=d">D</a></td>
    <td><a href="filename?value=e">E</a></td>
    <td><a href="filename?value=f">F</a></td>
    <td><a href="filename?value=g">G</a></td>
    <td><a href="filename?value=h">H</a></td>
    <td><a href="filename?value=i">I</a> </td>
    <td><a href="filename?value=j">J</a></td>
    <td><a href="filename?value=k">K</a></td>
    <td><a href="filename?value=l">L</a></td>
    <td><a href="filename?value=m">M</a></td>
    <td><a href="filename?value=n">N</a></td>
    <td><a href="filename?value=o">O</a></td>
    <td><a href="filename?value=p">P</a></td>
    <td><a href="filename?value=q">Q</a></td>
    <td><a href="filename?value=r">R</a></td>
    <td><a href="filename?value=s">S</a></td>
    <td><a href="filename?value=t">T</a></td>
    <td><a href="filename?value=u">U</a></td>
    <td><a href="filename?value=v">V</a></td>
    <td><a href="filename?value=w">W</a></td>
    <td><a href="filename?value=x">X</a></td>
    <td><a href="filename?value=y">Y</a></td>
    <td><a href="filename?value=z">Z</a></td>
  </tr>
</table>

Open in new window

0
 

Author Comment

by:daz1234
ID: 35027472
Pius, that didn't work.
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 35027667
Try this:

<table width="100%" border="0">
  <tr>
    <td><a href="index.php?value=a">A</a></td>
    <td><a href="index.php?value=b">B</a></td>
    <td><a href="index.php?value=c">C</a></td>
    <td><a href="index.php?value=d">D</a></td>
    <td><a href="index.php?value=e">E</a></td>
    <td><a href="index.php?value=f">F</a></td>
    <td><a href="index.php?value=g">G</a></td>
    <td><a href="index.php?value=h">H</a></td>
    <td><a href="index.php?value=i">I</a> </td>
    <td><a href="index.php?value=j">J</a></td>
    <td><a href="index.php?value=k">K</a></td>
    <td><a href="index.php?value=l">L</a></td>
    <td><a href="index.php?value=m">M</a></td>
    <td><a href="index.php?value=n">N</a></td>
    <td><a href="index.php?value=o">O</a></td>
    <td><a href="index.php?value=p">P</a></td>
    <td><a href="index.php?value=q">Q</a></td>
    <td><a href="index.php?value=r">R</a></td>
    <td><a href="index.php?value=s">S</a></td>
    <td><a href="index.php?value=t">T</a></td>
    <td><a href="index.php?value=u">U</a></td>
    <td><a href="index.php?value=v">V</a></td>
    <td><a href="index.php?value=w">W</a></td>
    <td><a href="index.php?value=x">X</a></td>
    <td><a href="index.php?value=y">Y</a></td>
    <td><a href="index.php?value=z">Z</a></td>
  </tr>
</table>


<?php

 
      echo "<html><body><table>\n\n"; $f = fopen("test.csv", "r"); 
      do{         
      if(strtolower(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>"; 
?>

Open in new window

0
 
LVL 3

Expert Comment

by:pius_babbun
ID: 35027710
Hope altered code from "Roads_Roads"  helps you much daz1234 check it ...
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35027784
Or even shorter:

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

 
      echo "<html><body><table>\n\n"; $f = fopen("test.csv", "r"); 
      do{         
      if(strtolower(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>"; 
?>

Open in new window

0
 

Author Closing Comment

by:daz1234
ID: 35027826
Thanks for your help! Works like a dream
0
 

Author Comment

by:daz1234
ID: 35027849
Thanks Pius for your help!
0
 
LVL 4

Expert Comment

by:mars-vie
ID: 35027866
<?php

$lines=file('rates.csv');
sort($lines);
$count=count($lines);

foreach(range('A', 'Z') as $letters)
{
echo "<h1><a name='$letters'>$letters</a></h1>";
echo "<blockquote>";
for($i=0; $i<$count; $i++) {if (substr($lines[$i], 0, 1)==$letters) {echo "$lines[$i]<br>";}}
echo "</blockquote>";
}
     
?>

This is a fast solution - you should have to format the output.
Using anchors for the links;
0
 
LVL 3

Expert Comment

by:pius_babbun
ID: 35028081
you are welcome daz1234 !
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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…

759 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