2016-01-13 82 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 */ 
}