【问题标题】:PHP While Loop/Foreach LoopsPHP While 循环/Foreach 循环
【发布时间】:2014-11-17 22:09:12
【问题描述】:

我有一个关于编写此代码的不同方式的问题,因为它没有按照我的意愿执行..

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

像这样的,

Welcome Center
   Introduce Yourself
   Say Hello To our Admins (last-tr)

Game
   COD
   BF4 (last-tr)

Code
   PHP
   Java
   PDO (last-tr)

但是我的代码给我的是

Welcome Center
   Introduce Yourself
   Say Hello To our Admins 

Game
   COD
   BF4 

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

这里是代码

    <?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'); ?>

【问题讨论】:

  • 我强烈建议通过直接输出而不是连接字符串来呈现 HTML。
  • 您的菜单会保持不变还是会不时变化?
  • 会不时变化

标签: php oop pdo


【解决方案1】:

你的 if 语句应该是 if ($x &lt; $sections-&gt;count()-1),因为数组的最后一个索引总是 count-1,因为从 0 开始。

您也可以通过在 1 处开始 $x 来修复它,因为您使用的是带有侧面计数器的 foreach,因此不会出现索引不足错误。换句话说:

foreach ($sections->results() as $section) {
    $x = 1;
    if ($x < $sections->count()) {
        ...
        $x++;
    }else {
        ...
    }
}

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

【讨论】:

  • 它仍然只将 last-tr 类添加到最后一个循环中,我如何让它在每个部分之后添加它。明白我的意思
【解决方案2】:

您是针对整个班级而不是班级中的数据进行测试。
$sections->count() 正在计算所有部分,而不是您正在处理的部分中的条目。
您需要测试单数部分的计数
所以,如果你有
$sectionsAll = 数组(
sectionOne = 数组(1, 2, 3),
sectionTwo = 数组 (1, 2, 3),
第三节 = 数组(1、2、3)
);
您目前正在对 $sectionsAll 进行测试,您需要检查 sectionOne、sectionTwo、sectionThree

【讨论】:

  • 我需要将该类添加到最后一行,因为它具有不同的 css 样式
猜你喜欢
  • 2015-07-03
  • 2014-10-19
  • 2012-10-02
  • 2017-05-28
  • 2018-02-09
  • 2013-10-01
  • 2016-03-14
  • 2014-12-05
  • 1970-01-01
相关资源
最近更新 更多