问题描述
我在 PHP 中创建了一个带有字母过滤器的表格.它工作正常,没有任何问题.但是我正在为 wordpress 项目开发它,所以在 PHP 中成功开发后,我尝试使用 $wpdb
在 wordpress 中实现它.
我现在面临的问题是要进行过滤,我们需要单击任何字母表(假设为 A),然后单击所有从A"开始的输入应作为输出返回.但不是只返回A"条目,而是返回整个表.总之过滤器不起作用.
我在 wordpress 的模板文件中使用此代码.
全局$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">查看全部</a></form><?phpif(isset($_GET['letter'])){$char=$_GET['字母'];如果($字符){$sql = $wpdb->get_results( "SELECT * FROM Teacher_directory where Name LIKE '$char%'", ARRAY_A );$count = $sql->num_rows;如果($count >= 1){echo "";echo "";echo "";echo " S. No. ";echo "Name ";echo "<th>指定</th>";echo "<th>部门</th>";echo "<th>Tele/Mob.No.</th>";回声</tr>";foreach($sql 作为 $row){echo " ";回声<td>".$row['S.不.'] ."</td>";回声<td>".$row['Name'] ."</td>";回声<td>".$row['指定'] ."</td>";回声<td>".$row['部门'] ."</td>";回声<td>".$row['Tele/Mob.No.'] ."</td>";回声</tr>";}echo "</tbody>";echo "</table>";}}}更新 -:自从我得到答案后,我将向未来的观众展示我的最终完整代码(处于工作状态)
如果有人遇到有关按字母顺序过滤的表格的问题,那么下面的代码可能会解决您的问题.可以在模板文件中使用.
get_results( "SELECT * FROM Teacher_directory where Name LIKE '$char%'", ARRAY_A );$count = $results->num_rows;?><div class="contcolor"><?phpif(!empty($results)){echo "";echo "";echo "";echo " S. No. ";echo "Name ";echo "<th>指定</th>";echo "<th>部门</th>";echo "<th>Tele/Mob.No.</th>";回声</tr>";foreach($results as $row){echo " ";回声<td>".$n +=1 ."</td>";回声<td>".$row['Salutation'].''.$row['Name'] ."</td>";回声<td>".$row['指定'] ."</td>";回声<td>".$row['部门'] ."</td>";回声<td>".$row['Tele/Mob.No.'] ."</td>";回声</tr>";}echo "</tbody>";echo "</table>";}别的{echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";}?></div><?php}}别的 {$results = $wpdb->get_results('SELECT * FROM Teacher_directory', ARRAY_A);if(!empty($results)){?><div class="contcolor"><?phpecho "";echo "";echo "";echo " S. No. ";echo "Name ";echo "<th>指定</th>";echo "<th>部门</th>";echo "<th>Tele/Mob.No.</th>";回声</tr>";foreach($results as $row){echo " ";回声<td>".$row['S.不.'] ."</td>";回声<td>".$row['Salutation'].''.$row['Name'] ."</td>";回声<td>".$row['指定'] ."</td>";回声<td>".$row['部门'] ."</td>";回声<td>".$row['Tele/Mob.No.'] ."</td>";回声</tr>";}echo "</tbody>";echo "</table>";?></div><?php}}?></div></div></div></div><?phpget_footer();?></div> 解决方案 你只需要改变你的 IF 条件
$sql = $wpdb->get_results( "SELECT * FROM Teacher_directory where Name LIKE '$char%'", ARRAY_A );if(!empty($sql)) {/* 您的搜索结果与您打印的代码相同 */} 别的 {/* 一样 */}
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>";
}}
}
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>
解决方案 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 */
}
这篇关于表格中的字母过滤器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 05:23