Solved

how to create alphabetic pagination in php

Posted on 2011-03-03
12
714 Views
Last Modified: 2013-12-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
  • 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now