Iteration of grouped output creates an empty record
Posted: Sat Jan 06, 2007 4:19 pm
I have a MySQL table which describes the architecture of a Web site I'm developing. I'm attempting to use the table to create dynamic menus and the sitemap. Here is the table structure, abbreviated:
And so forth...
What I'm trying to do is iterate through the results of a query of this table, basically creating an outline format, which I can then leverage for navigation and for the sitemap.
Here's my query:
And here's my display code:
This is working well (for some reason I had to add the
because the display was dropping a single record if I didn't have that check... I'm not sure why. Actually, I have a sneaking suspicion this is related to the problem I'm describing below).
But there is a problem I can't resolve. The output has a single blank line in it, and it's not an empty record in the DB, as far as I can tell, because if I filter out a section, the blank line appears in a new place (always the 26th output line, I think). So, the source code of the output ends up looking like this (again, this is an abbreviated recreation):
I assume it's some mystery with the loop, loop interference, full moon?
Whatever it is, I can't figure it out. Any ideas?
Thanks,
max
Code: Select all
| id | parent | child1 | child2 | child3 |
--------------------------------------------------
1 | Home | | | |
2 | About | Foo1 | | |
3 | About | Foo2 | | |
4 | About | Foo3 | | |
5 | Resources | Bar1 | | |
6 | Resources | Bar2 | | |
7 | Resources | Bar3 | foo1 | |
8 | Resources | Bar4 | foo2 | |
9 | Services | Zot1 | | |
9 | Services | Zot2 | | |What I'm trying to do is iterate through the results of a query of this table, basically creating an outline format, which I can then leverage for navigation and for the sitemap.
Here's my query:
Code: Select all
$navQuery = sprintf("
SELECT DISTINCT parent, child1, child2, child3
FROM sitemap2
WHERE nav = 'y'
ORDER BY id,parent,child1,child2,child3;
");Code: Select all
<?php
$myParent = '';
$myChild1 = NULL;
$myChild2 = NULL;
$myChild3 = NULL;
while ($row = mysql_fetch_assoc($navResult)) {
if ($myParent != $row['parent']) {
echo '<b>' . $row['parent'] . "</b><br>\n";
$myParent = $row['parent'];
}
if ($myChild1 != $row['child1']) {
if ($myChild1 != " ") {
echo '<em>' . $row['child1'] . "</em><br>\n";
}
$myChild1 = $row['child1'];
}
if ($myChild2 != $row['child2']) {
if ($myChild2 != " ") {
echo ' ' . $row['child2'] . "<br>\n";
}
$myChild2 = $row['child2'];
}
}
?>Code: Select all
if ($myChild1 != " ") {But there is a problem I can't resolve. The output has a single blank line in it, and it's not an empty record in the DB, as far as I can tell, because if I filter out a section, the blank line appears in a new place (always the 26th output line, I think). So, the source code of the output ends up looking like this (again, this is an abbreviated recreation):
Code: Select all
<b>ABOUT US</b><br>
<em>Foo1</em><br>
<em>Foo2</em><br>
<em>Foo3</em><br>
<b>RESOURCES</b><br>
<em>Bar1</em><br>
<em>Bar2</em><br>
<em>Bar3</em><br>
foo1<br>
I'M NOT SURE WHY THERE'S A CARRIAGE RETURN HERE... ODD
<em>Bar4</em><br>
foo2<br>
<b>SERVICES</b><br>
<em>Zot1</em><br>
<br> THIS IS THE MYSTERY ROW
<em>Zot2</em><br>Whatever it is, I can't figure it out. Any ideas?
Thanks,
max