Solved

how to create alphabetic pagination in php

Posted on 2011-03-03
12
873 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
Industry Leaders: 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!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…

630 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