Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1119
  • Last Modified:

how to create alphabetic pagination in php

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
daz1234
Asked:
daz1234
  • 4
  • 3
  • 3
  • +1
1 Solution
 
Lukasz ChmielewskiCommented:
Can you include csv file for that ?
0
 
mars-vieCommented:
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
 
daz1234Author Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
pius_babbunCommented:
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
 
daz1234Author Commented:
Pius, that didn't work.
0
 
Lukasz ChmielewskiCommented:
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
 
pius_babbunCommented:
Hope altered code from "Roads_Roads"  helps you much daz1234 check it ...
0
 
Lukasz ChmielewskiCommented:
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
 
daz1234Author Commented:
Thanks for your help! Works like a dream
0
 
daz1234Author Commented:
Thanks Pius for your help!
0
 
mars-vieCommented:
<?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
 
pius_babbunCommented:
you are welcome daz1234 !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now