Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew McCombs
4,309 PointsPHP Code Help
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'); ?>
2 Answers
Nick Fuller
9,027 PointsThis is interesting.
Just reading through it, one thing I do see is that you're setting the variable $x to 0 inside your loop. So each time you iterate over your loop, $x is always 0.
So the line
if ($x < $sections->count()) {
Is always going to end up being true.
Maybe try
$sections = $db->query( "SELECT * FROM forum_section WHERE category_id = '$category->id'" );
$sections_count = $sections->count();
$i = 0;
foreach ( $sections->results() as $index=>$section ) {
if ( ++$i != $sections_count ) {
// not last item
} else {
// last item
}
}
thomascawthorn
22,986 PointsAre you comparing against the right sections? i.e. sub sections not all sections?
You might be able to get away with removing the else block which is always nice.
<?php
foreach ( $sections->results() as $index=>$section ) {
// Do rad non-last item stuff
if ( ++$i == $sections_count ) {
// last item only. Sorry you're not in the cool gang unless you're last.
}
}
If you're applying a class 'last' for design purposes, this should be done with CSS not html. You can use some cool selectors like ul.navigation li.link:last-of-type {}
But even better - why are you displaying list information inside tables?! If it's a list of stuff (which is exactly what this looks like), a more semantic html markup might be:
<ul>
<li>
<ul>
<li>Totally nested</li>
</ul>
</li>
<li></li>
<li></li>
<li></li>
</ul>