根据表单提交结果在手风琴内显示单柱

时间:2014-05-08 作者:Robodashy

*注-有关完整背景,请参见前面的问题:http://bit.ly/1gaF673

好的,在解决了上面的问题和接下来的几个问题之后。我的表单提交返回我想要的结果。执行此操作的代码如下:

//THIS CODE CONNECTS TO THE NON-WP-TABLES WE MADE INSIDE THE WP DATABASE
global $wpdb;
//get list of localities
$localityRow = $wpdb->get_results( "SELECT Name, LocalityID FROM tblYBSLocality ORDER BY
Name ASC" );
//get list of schools
$schoolRow = $wpdb->get_results( "SELECT Name, SchoolID FROM tblYBSSchool ORDER BY Name 
ASC" );
//get school ID if form has been submitted
$SelectedSchoolID = $_GET[\'schools\'];
//get location ID if form has been submitted
$SelectedLocationID = $_GET[\'localities\'];
//get selected school name
$selectedSchoolName = $wpdb->get_var( "SELECT Name FROM tblYBSSchool WHERE 
SchoolID=".$SelectedSchoolID );
//get selected location name
$selectedLocationName = $wpdb->get_var( "SELECT Name FROM tblYBSLocality WHERE 
LocalityID=".$SelectedLocationID );
//get route ID
$getRouteID = $wpdb->get_results( "SELECT Name, RouteID, Description, RoutePageURI
FROM tblYBSRoute WHERE RouteID IN ( SELECT RouteID FROM tblYBSRouteSchool WHERE SchoolID = 
$SelectedSchoolID ) AND RouteID IN ( SELECT RouteID FROM tblYBSRouteLocality WHERE 
LocalityID = $SelectedLocationID ) " );

//THIS CODE DISPLAYS THE RESULTS BASED ON WHAT THE USER HAS SELECTED IN THE CODE OMITTED
if (isset($SelectedLocationID) && isset($SelectedSchoolID)) { ?>
<div id="resultsDiv">
<div id="resultsHead">
<h3>Search results for routes between:</h3>
<b><?php echo $selectedLocationName; ?></b> and <b><?php echo $selectedSchoolName; ?>
</b><br />
(Click on route to view timetable and map)
</div>
<div id="routeResult">
<?php
if ($getRouteID) {
foreach ($getRouteID as $route) {
$RouteName = $route->Name;
$RouteID = $route->RouteID;
$RouteDescription = $route->Description;
$RoutePageID = $route->RoutePageURI;
?>
<a href="../../<?php echo $RoutePageID;?>"><b>Route: </b><?php echo $RouteName;?></a> <br 
/> <?php echo $RouteDescription;?><br />
<?php
}
} else {
?>
<div id="routeRestult">
<h4>Results:</h4>
No Route found for your selection.<br />
</div><?php } ?> </div></div><?php } ?>
这是正常的,但我想改变发生了什么。目前,有一个指向个人帖子ID的链接,结果就在那里——这太棒了。但我想做些轻蔑的事,把那个帖子放在手风琴里。

我在这里找到了一个起点:http://bit.ly/1nqT6Pw - 但我需要弄清楚如何使它更具体地描述我要做的事情。上面链接中的代码是(正如该帖子中所示):

<?php if (have_posts()): ?>
<div id="accordion">
<?php while (have_posts()) : the_post();?>
<div class="accordion-header">
<h1><?php the_title();?></h1>
<?php the_excerpt();?>
</div>
<div><?php the_content();?></div>
<?php endwhile; ?>
</div>
<?php else:?>
<p><?php _e(\'No posts\'); ?></p>
<?php endif;?>
我知道你可以通过标题、摘录和内容来区分一篇文章,但我似乎无法将其正确地放到手风琴中。

我已将上述代码修改为以下内容:

<div id="routeResult"> <?php
if ($getRouteID) {
foreach ($getRouteID as $route) {
$RouteName = $route->Name;
$RouteID = $route->RouteID;
$RouteDescription = $route->Description;
$RoutePageID = $route->RoutePageURI; ?>
<div id="accordion"> <?php
while (is_single($RoutePageID));?>
<div class="accordion-header">
<h1><?php the_title();?></h1>
<?php the_excerpt();?>
</div>
<div><?php the_content();?></div>
</div><?php } } else { ?>
<div id="routeResult">
<h4>Results:</h4>
No Route found for your selection.<br />
</div> <?php } ?> </div>
这比你想象的要好。它创建div,但除了标题之外没有其他内容(等待它…)但标题不对。它给我的是页面的标题,而不是帖子的标题。

因此while语句检测到yes$RoutePageID有效,并且它是一篇文章,但随后返回当前页面的标题。

是因为我连接了其他表,需要告诉$wpdb返回到原始表吗?或者我应该用get\\u post做些什么(由于需要更多的声誉,无法链接到codex,但我知道该去哪里查找)。

一如既往,我们非常感谢您的任何帮助。谢谢你哟!

1 个回复
SO网友:Robodashy

问题解决后关闭。对于任何感兴趣的人,结果如下所示(请参阅上面详细说明的初始DB表连接代码,因为下面的代码仅指输出):

if (isset($SelectedLocationID) && isset($SelectedSchoolID)) { ?>
<div id="resultsDiv">
  <div id="resultsHead">
    <h3>Search results for routes between:</h3>
    <b><?php echo $selectedLocationName; ?></b>
    and <b><?php echo $selectedSchoolName; ?></b><br />

    (Click on route to view timetable and map)
  </div>

  <?php
  if ($getRouteID) { ?>
  <div id="routeResult">
    <div id="accordion">
    <?php
    foreach ($getRouteID as $route) {
      // Break the getRouteID results into separate variables
      $RouteName = $route->Name;
      $RouteID = $route->RouteID;
      $RouteDescription = $route->Description;
      $RoutePageID = $route->RoutePageURI;

      $getPost = $wpdb->get_results(
        $wpdb->prepare (
          "SELECT post_content, post_name
          FROM m6anby77we_posts
          WHERE post_name = \'%s\'", $RoutePageID
        )
      );

      foreach ($getPost as $post) {
        $pName = $post->post_name;
        $pContent = $post->post_content;

        ?>
        <div class="accordion-header">
          <h4>Route: <?php echo $RouteName;?> <?php echo $RouteDescription;?></h4>
        </div>
        <div><?php echo $pContent; ?></div>
        <?php
      }
    }
    ?>
    </div>
  </div>
<?php } else { ?>
  <div id="routeResult">
    <h4>Results:</h4>
    No Route found for your selection.<br />
  </div>
<?php } ?> </div> <?php } ?>
唯一的诀窍/痛苦之处在于,帖子内容是以字符串的形式引入的(这是意料之中的),因此您必须进入每篇帖子中的文本编辑器,以确保您在其中正确地格式化了内容,以便字符串可以打印为html。

祝你们玩得开心

结束

相关推荐

如何实现用于批量操作的自定义插件(或wp_list_table插件的任何替代)的wp_list_table功能

在WordPress中codex, 以下以红色突出显示WP List Table.“该类的访问被标记为私有。这意味着它不供插件和主题开发人员使用,因为它可能会在未来的WordPress发布中更改,而不会发出警告。”。有没有办法添加批量操作和排序功能,如(WP_List_Table 在自定义插件中,以及如何使用它?