0

I have a question on a different way to wright this code because its not doing what I want it to do..

I am creating a forum list that will display the section under that category. Well this is the issue as you can see that I have an If () {}Else {} statement that is being used to put the information in that list I want. The problem is that I need to add a special class to the last section under that category that will be called last-tr. Now my issue that I have is all the information is making it to the screen but the class is only applied to the last row and now the last row of all the category's which is what i'm looking for.

So something like this,

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

Game
   COD
   BF4 (last-tr)

Code
   PHP
   Java
   PDO (last-tr)

but what my code is giving me is

Welcome Center
   Introduce Yourself
   Say Hello To our Admins 

Game
   COD
   BF4 

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

Here is the code

    <?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'); ?>
3
  • 3
    I strongly suggest rendering HTML by outputting directly rather than concatenating a string. Commented Nov 17, 2014 at 22:13
  • Will your menu stay the same or it will vary from time to time? Commented Nov 17, 2014 at 22:13
  • It will vary from time to time Commented Nov 17, 2014 at 22:39

2 Answers 2

2

Your if-statement should be if ($x < $sections->count()-1) because the last index of an array is always count-1 due to starting at 0.

OR

You could also fix it here by starting $x at 1, since you are using a foreach with the counter on the side, so its not like you'll get an out of index error. In other words:

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

But going with the first option is probably more straight-forward.

Sign up to request clarification or add additional context in comments.

1 Comment

It still only adds the last-tr class to the last loop, how would I get it to add it after each section. See what I mean
1

You are testing against your overall class not the data within the class.
$sections->count() is counting all of the sections not the entries in teh one section you are dealing with.
You need to test the count of the singular section
So if you had
$sectionsAll = array(
    sectionOne = array( 1, 2, 3),
    sectionTwo = array( 1, 2, 3),
    sectionThree = array( 1, 2, 3)
);
You are currently testing on $sectionsAll, you need to check against sectionOne, sectionTwo, sectionThree

1 Comment

I need the add that class the that last row because it has different css styles

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.