我在PHP中创建了一个带字母过滤器的表。它没有任何问题,工作正常。但是我正在为wordpress项目开发它,所以在用PHP成功开发之后,我尝试使用$wpdb
在wordpress中使用它。
我现在面临的问题是要过滤我们需要点击任何字母(假设A)并点击所有从'A' A'应该作为输出返回。但不是仅返回'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>
答案 0 :(得分: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 */
}