Alphabetical filter in table not working

2019-09-03 15:50发布

I have created a table with alphabet filter in PHP. It was working fine without any issue. BUT i was developing it for wordpress project, So after successfully development in PHP I tried to imliment it in wordpress using $wpdb.

The problem I am facing now is that to filter we need to click any alphabet (suppose A) And after clicking All enteries that started from 'A' should return as output. But instead of returning Only 'A' entries Its returning whole table. In short filter is not working.

I am using this code in template file in wordpress.

global $wpdb;
?><div class="wrap"><div class="main-top"><div class="main"><h1><div    class="titlepage"><?php the_title();?></div></h1><?php
?><form action="memberspage.php" method="POST" name='search'  onclick="submit">
<a href="?page_id=911?letter=A">A</a> |
<a href="?page_id=911?letter=B">B</a> |
<a href="?page_id=911?letter=C">C</a> |
<a href="?page_id=911?letter=D">D</a> |
<a href="?page_id=911?letter=E">E</a> |
<a href="?page_id=911?letter=F">F</a> |
<a href="?page_id=911?letter=G">G</a> |
<a href="?page_id=911?letter=H">H</a> |
<a href="?page_id=911?letter=I">I</a> |
<a href="?page_id=911?letter=J">J</a> |
<a href="?page_id=911?letter=K">K</a> |
<a href="?page_id=911?letter=L">L</a> |
<a href="?page_id=911?letter=M">M</a> |
<a href="?page_id=911?letter=N">N</a> |
<a href="?page_id=911?letter=O">O</a> |
<a href="?page_id=911?letter=P">P</a> |
<a href="?page_id=911?letter=Q">Q</a> |
<a href="?page_id=911?letter=R">R</a> |
<a href="?page_id=911?letter=S">S</a> |
<a href="?page_id=911?letter=T">T</a> |
<a href="?page_id=911?letter=U">U</a> |
<a href="?page_id=911?letter=V">V</a> |
<a href="?page_id=911?letter=W">W</a> |
<a href="?page_id=911?letter=X">X</a> |
<a href="?page_id=911?letter=Y">Y</a> |
<a href="?page_id=911?letter=Z">Z</a> |
<a href="?page_id=911">View All</a> 
</form><?php
if(isset($_GET['letter']))
{
  $char=$_GET['letter'];

  if($char)
{
$sql = $wpdb->get_results( "SELECT * FROM teacher_directory where Name LIKE     '$char%'", ARRAY_A  );
$count = $sql->num_rows;
if($count >= 1)
{
    echo "<table width='100%' border='1'>";
    echo "<tbody>";
    echo "<tr>";
    echo "<th>S. No.</th>";
    echo "<th>Name</th>";
    echo "<th>Designation</th>";
    echo "<th>Department</th>";
    echo "<th>Tele/Mob.No.</th>";
    echo "</tr>";
    foreach($sql as $row){
    echo "<tr>";
    echo "<td>" . $row['S. No.'] . "</td>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td>" . $row['Designation'] . "</td>";
    echo "<td>" . $row['Department'] . "</td>";
    echo "<td>" . $row['Tele/Mob.No.'] . "</td>";               
    echo "</tr>";
 }
    echo "</tbody>";
    echo "</table>"; 
}}
}

UPDATE -: Since I got my answer, I am showing my final complete code(in working condition) for future viewers

If anyone is facing issue regarding Alphabetical filtered table then below code may solve your problem. It can be use in template file.

<?php
/*
Template Name: Table
*/
get_header();
?><div class="banner-box"><?php
global $wpdb;
?><div class="wrap"><div class="main-top"><div class="main"><h1><div  class="titlepage"><?php the_title();?></div></h1><div class="contcolor"><?php
?><form action="teacher.php" method="POST" name='search' onclick="submit"><a  href="?page_id=916&letter=A">A</a> |
<a href="?page_id=916&letter=B">B</a> |
<a href="?page_id=916&letter=C">C</a> |
<a href="?page_id=916&letter=D">D</a> |
<a href="?page_id=916&letter=E">E</a> |
<a href="?page_id=916&letter=F">F</a> |
<a href="?page_id=916&letter=G">G</a> |
<a href="?page_id=916&letter=H">H</a> |
<a href="?page_id=916&letter=I">I</a> |
<a href="?page_id=916&letter=J">J</a> |
<a href="?page_id=916&letter=K">K</a> |
<a href="?page_id=916&letter=L">L</a> |
<a href="?page_id=916&letter=M">M</a> |
<a href="?page_id=916&letter=N">N</a> |
<a href="?page_id=916&letter=O">O</a> |
<a href="?page_id=916&letter=P">P</a> |
<a href="?page_id=916&letter=Q">Q</a> |
<a href="?page_id=916&letter=R">R</a> |
<a href="?page_id=916&letter=S">S</a> |
<a href="?page_id=916&letter=T">T</a> |
<a href="?page_id=916&letter=U">U</a> |
<a href="?page_id=916&letter=V">V</a> |
<a href="?page_id=916&letter=W">W</a> |
<a href="?page_id=916&letter=X">X</a> |
<a href="?page_id=916&letter=Y">Y</a> |
<a href="?page_id=916&letter=Z">Z</a> |
<a href="?page_id=916">View All</a> 
</form><?php
if(isset($_GET['letter']))
{
  $char=$_GET['letter'];
  if($char)
{
$results = $wpdb->get_results( "SELECT * FROM teacher_directory where Name     LIKE '$char%'", ARRAY_A  );
$count = $results->num_rows;
?><div class="contcolor"><?php
if(!empty($results))
{   
    echo "<table width='100%' border='1' cellspacing='1'>";
    echo "<tbody>";
    echo "<tr>";
    echo "<th>S. No.</th>";
    echo "<th>Name</th>";
    echo "<th>Designation</th>";
    echo "<th>Department</th>";
    echo "<th>Tele/Mob.No.</th>";
    echo "</tr>";
    foreach($results as $row){
    echo "<tr>";
    echo "<td>" . $n +=1 . "</td>";
    echo "<td>" . $row['Salutation'].' '.$row['Name'] . "</td>";
    echo "<td>" . $row['Designation'] . "</td>";
    echo "<td>" . $row['Department'] . "</td>";
    echo "<td>" . $row['Tele/Mob.No.'] . "</td>";               
    echo "</tr>";
 }
  echo "</tbody>";
  echo "</table>"; 
}else
{
  echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records    Found</td></tr></table>";
}?></div><?php
}}

else {

       $results = $wpdb->get_results( 'SELECT * FROM teacher_directory', ARRAY_A  );
       if(!empty($results)){
       ?><div class="contcolor"><?php
        echo "<table width='100%' border='1' cellspacing='1'>";
       echo "<tbody>";
       echo "<tr>";
       echo "<th>S. No.</th>";
       echo "<th>Name</th>";
       echo "<th>Designation</th>";
       echo "<th>Department</th>";
       echo "<th>Tele/Mob.No.</th>";
       echo "</tr>";
       foreach($results as $row){
       echo "<tr>";
       echo "<td>" . $row['S. No.'] . "</td>";
       echo "<td>" . $row['Salutation'].' '.$row['Name'] . "</td>";
       echo "<td>" . $row['Designation'] . "</td>";
       echo "<td>" . $row['Department'] . "</td>";
       echo "<td>" . $row['Tele/Mob.No.'] . "</td>";               
       echo "</tr>";
 }
 echo "</tbody>";
 echo "</table>"; 
 ?></div><?php
}}
?></div></div></div></div><?php
get_footer();?></div>

标签: php wordpress
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-03 16:33

You have to just change your IF condition

$sql = $wpdb->get_results( "SELECT * FROM teacher_directory where Name LIKE '$char%'", ARRAY_A  );
if(!empty($sql)) { 
   /* your search result same code that you have print */ 
} else {
  /* same it is */
}
查看更多
登录 后发表回答