2016-01-13 28 views
0

我已经创造了在PHP字母过滤器的表。它工作正常,没有任何问题。但我正在为wordpress项目开发它,所以在PHP中成功开发之后,我尝试使用$wpdb在wordpress中实现它。字母过滤器不工作

我现在面临的问题是,过滤我们需要点击任何字母(假设A),然后单击所有enteries,从“A”开始后应该返回作为输出。但不是返回只有'A'条目它返回整个表。总之过滤器不工作。

我使用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>"; 
}} 
} 

更新 - :因为我有我的回答,我显示我最后的完整代码(在工作状态下),以便将来观众

如果有人正面临着关于然后在下面的字母顺序过滤表中的问题代码可能会解决您的问题。它可以在模板文件中使用。

<?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> 
+5

'?PAGE_ID = 911?信='必须是** **后的第一个?像'?page_id = 911&letter =' – 2016-01-13 04:26:53

+0

我试过了但是列表空了。可能是我在行计数函数中犯了一些错误,我猜为什么。你能否也请检查一下。 – Rishabh

+0

你可以在分配给$ char后打印变量吗? if(isset($ _ GET ['letter'])){$ char = $ _ GET ['letter']; echo $ char;死(); –

回答

2

你只是改变你的IF条件

$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 */ 
}