草庐IT

php - 试图让论坛显示类别的所有子类别

coder 2024-04-21 原文

这是我的论坛目前的样子: http://prntscr.com/73oicl

但是,如您所见,这些类别都表示“社区”,这是不应该发生的。 “测试”子类别应位于“公告和更新”子类别下。

我知道问题出在哪里,但我不知道如何解决。

    function getForums($id) {
    $currentHost = "http://$_SERVER[HTTP_HOST]";
    $query = "SELECT * FROM forums, categories";
    try {
        global $db;
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $result = $stmt->fetchAll();
        foreach($result as $row) {
            $category_title = $row['category_title'];
            $forum_id = $row['forum_id'];
            $forum_title = $row['forum_title'];
            $forum_topic_count = $row['forum_topic_count'];
            $forum_post_count = $row['forum_post_count'];
            $forum_last_topic_id = $row['forum_last_topic_id'];
            $forum_last_topic = $row['forum_last_topic'];
            $forum_last_date = $row['forum_last_date'];
            $forum_last_user = $row['forum_last_user'];
            $fixed_last_topic = substr($forum_last_topic,0,25).'...';
            echo '<div class="forum pleft">
                    <div class="forum-header">
                        <span class="header-text">'.$category_title.'</span>
                    </div>
                    <table>
                        <tr>
                            <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td>
                            <td class="topics">'.$forum_topic_count.'</td>
                            <td class="posts">'.$forum_post_count.'</td>
                            <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td>
                        </tr>
                    </table>
                </div>';
        }
    }
    catch(PDOException $ex) {
        die("error");
    }
}

如您所见,对于每个结果,它都会创建一个全新的论坛 div,这意味着为该类别提供的所有子类别不会放在一起,并且每个子类别都将创建一个新论坛,我不想要。 有没有一种方法可以让 echo explode ,这样如果 2 个或更多子类别在 1 个类别中,它就不会推出新的 div?

最佳答案

我在没有测试的情况下写了这篇文章,因此您可能需要检查错误。这可能不是最有效的方法,但它应该有效。

function getForums($id) {
    $currentHost = "http://$_SERVER[HTTP_HOST]";
    $query = "SELECT * FROM forums, categories ORDER BY categories ASC"; /* Order by categories to arrange same categories together */
    try {
        global $db;
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $result = $stmt->fetchAll();
        $numrows = $stmt->fetch(PDO::FETCH_NUM); /* Check for total rows of records */

        $count = 0;
        $currentCategory = ""; // Declare container

        foreach($result as $row) {
            $category_title = $row['category_title'];
            $forum_id = $row['forum_id'];
            $forum_title = $row['forum_title'];
            $forum_topic_count = $row['forum_topic_count'];
            $forum_post_count = $row['forum_post_count'];
            $forum_last_topic_id = $row['forum_last_topic_id'];
            $forum_last_topic = $row['forum_last_topic'];
            $forum_last_date = $row['forum_last_date'];
            $forum_last_user = $row['forum_last_user'];
            $fixed_last_topic = substr($forum_last_topic,0,25).'...';

            /* If currentCat is empty OR is not similar to previous */
            if($currentCategory == "" || $currentCategory != $category_title){
                /* If currentcat is not empty AND not similar to previous, close */
                if($currentCategory != "" && $currentCategory != $category_title){
                    echo '</table></div>';
                }

                echo '<div class="forum pleft">
                    <div class="forum-header">
                        <span class="header-text">'.$category_title.'</span>
                    </div>
                    <table>
                        <tr>
                            <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td>
                            <td class="topics">'.$forum_topic_count.'</td>
                            <td class="posts">'.$forum_post_count.'</td>
                            <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td>
                        </tr>';
                $count++;

                /* Check if its last row, If yes, end */
                if($count == $numrows){
                    echo '</table></div>';
                }

                $currentCategory = $category_title; /*Set category title */
             }
             /* If next row belong to the same category, If yes, continue */
             else if($currentCategory == $category_title){
                echo '<tr>
                        <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$category_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td>
                        <td class="topics">'.$forum_topic_count.'</td>
                        <td class="posts">'.$forum_post_count.'</td>
                        <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td>
                    </tr>';
                $count++;

                /* Check if its last row, If yes, end */
                if($count == $numrows){
                    echo '</table></div>';
                }
            }
        }
    }
    catch(PDOException $ex) {
        die("error");
    }
}

关于php - 试图让论坛显示类别的所有子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30153582/

有关php - 试图让论坛显示类别的所有子类别的更多相关文章

  1. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  2. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  3. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  6. ruby-on-rails - 跳过状态机方法的所有验证 - 2

    当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested

  7. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  8. ruby - Nokogiri 剥离所有属性 - 2

    我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog

  9. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  10. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

随机推荐