首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP While循环/Foreach循环

问PHP While循环/Foreach循环
EN

Stack Overflow用户
提问于 2014-11-18 06:09:13
回答 2查看 128关注 0票数 0

我有一个关于写这段代码的不同方式的问题,因为它没有做我想让它做的事情。

我正在创建一个论坛列表,将显示该类别下的部分。这就是问题所在,因为你可以看到我有一个If () {}Else {}语句,它被用来将信息放入我想要的列表中。问题是,我需要在该类别下的最后一个部分添加一个特殊的类,这个类将被称为last-tr。现在我的问题是,所有的信息都显示在屏幕上,但类只应用于最后一行,现在是所有类别的最后一行,这就是我要找的。

所以就像这样,

代码语言:javascript
复制
Welcome Center
   Introduce Yourself
   Say Hello To our Admins (last-tr)

Game
   COD
   BF4 (last-tr)

Code
   PHP
   Java
   PDO (last-tr)

但是我的代码带给我的是

代码语言:javascript
复制
Welcome Center
   Introduce Yourself
   Say Hello To our Admins 

Game
   COD
   BF4 

Code
   PHP
   Java
   PDO (last-tr) <- Just that one.

以下是代码

代码语言:javascript
复制
    <?php 
include '../../core/init.php'; 
include '../../includes/head.php';

//Getting Categories under Welcome

$db = DB::getInstance();
$user = New User();
$forum = New Forum();

$list = '';

$category = $db->query('SELECT * FROM forum_categories');
foreach ($category->results() as $category) {
    $list .= '<thead>';
    $list .= '<tr>';
    $list .= '<th class="titles">' . $category->title . '</th>';
    $list .= '<th>Last Post</th>';
    $list .= '<th>Topics</th>';
    $list .= '<th>Replies</th>';
    $list .= '</tr>';
    $list .= '</thead>';
    $list .= '<tbody>';
    $sections = $db->query("SELECT * FROM forum_section WHERE category_id = '$category->id'");
    foreach ($sections->results() as $section) {
        $x = 0;
        if ($x < $sections->count()) {
            $list .= '<tr>';
            $list .= '<td>';
            $list .= '<a href="/category.php?id='. $section->id .'">';
            $list .= '<img scr="/images/icons/iconmonstr/intro.png">';
            $list .= '</a>';
            $list .= '<a href="/category.php?id='. $section->id .'">' . $section->title . '</a>';
            $list .= '<span>' . $section->desc . '</span>';
            $list .= '</td>';
            $list .= '<td> Nasty </td>';
            $list .= '<td> 0 </td>';
            $list .= '<td> 0 </td>';
            $list .= '</tr>';
            $x++;
        }else {
            $list .= '<tr class="last-tr">';
            $list .= '<td>';
            $list .= '<a href="/category.php?id='. $section->id .'">' . $section->title . '</a>';
            $list .= '</td>';
            $list .= '<td> Nasty </td>';
            $list .= '<td> 0 </td>';
            $list .= '<td> 0 </td>';
            $list .= '</tr>';
        }
    }
    $list .= '</tbody>';
}
?>
<link rel="stylesheet" href="/css/fourms.css">
<title>Forums</title>
</head>
<body>
<?php include '../../includes/header.php'; ?>
    <section id="main_section">
        <section id="main_content">
            <div id="forum-section">
                <table class="forumlist">
                    <?php echo $list; ?>

                </table>
            </div>
        </section>
    </section>
<?php include('../../includes/footer.php'); ?>
EN

回答 2

Stack Overflow用户

发布于 2014-11-18 06:16:37

您的if语句应该是计数,因为数组的最后一个索引总是if ($x < $sections->count()-1) -1,因为它从0开始。

或

您也可以在这里通过从1启动$x来修复它,因为您使用的是一个计数器在旁边的foreach,所以您不会得到一个超出索引的错误。换句话说:

代码语言:javascript
复制
foreach ($sections->results() as $section) {
    $x = 1;
    if ($x < $sections->count()) {
        ...
        $x++;
    }else {
        ...
    }
}

但是使用第一个选项可能更直接。

票数 2
EN

Stack Overflow用户

发布于 2014-11-18 06:21:21

您测试的是整个类,而不是类中的数据。

$ sections ->count()计算所有的区段,而不是您正在处理的区段中的条目。

您需要测试单数部分的计数

所以如果你有

$sectionsAll =数组(

*sectionOne= array( 1,2,3),

*sectionTwo= array( 1,2,3),

*sectionThree= array( 1,2,3)

);

您当前正在$sectionsAll上进行测试,您需要检查sectionOne、sectionTwo、sectionThree

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26982922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档