SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Hybrid View
-
Apr 10, 2009, 12:54 #1
- Join Date
- Mar 2008
- Location
- United Kingdom
- Posts
- 1,285
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Slightly complex WHILE Loop issue...
Hi,
I'm having some issues with a while loopIf anyone can help, I'd be over-the-moon.
I have a category table:
id | name
0 | home
1 | contact
and pages table:
id | name | file_name | cat
0 | home page | index.php | 0
1 | get in touch | get-in-touch.php | 1
2 | guestbook | guestbook.php | 1
3 | feedback | feedback.php | 1
Now my idea is to use a while loop to display all the pages in my database like the following:
HTML Code:<li id="home"> <!-- category name for the element id --> <a href="">home</a> <!-- category name for the anchor text --> <div class="callout" id="hometab"> <!-- category name before tab in id --> <ul> <li><a href="#">home page</a></li><!-- page name for anchor text --> </ul> </div> </li> <li id="contact"> <a href="">contact</a> <div class="callout" id="contacttab"> <ul> <li><a href="#">get in touch</a></li> <li><a href="#">guestbook</a></li> <li><a href="#">feedback</a></li> </ul> </div> </li> .........etc....................
Many thanks for any pointers, reeeeeally appreciate any help with this.
I've been racking my brains for hours now.
P.S.
Probably anything but helpful but I did try the following earlier, but didn't get the right result:
PHP Code:<?
require_once('connect.php');
$sql = "SELECT pages.name, pages.file_name, pages.cat, category.id, category.name FROM pages INNER JOIN category ON category.id = pages.cat";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res)) {
if (empty($last) || $last != $row['name']) {
echo '<li id="' . $row['4'] . '">
<a href="/' . $row['4'] . '">' . $row['4'] . '</a>
<a class="tab" href="/' . $row['4'] . '"></a>
<div class="callout" id="' . $row['4'] . 'tab">';
$last = $row['name'];
echo '<ul>';
}
echo '<li>' . $row['1'] . '</li>';
if (empty($last) || $last != $row['name']) {
echo '</ul>
</div>
</li>';
}
}
?>Last edited by invision2; Apr 10, 2009 at 13:55.
-
Apr 10, 2009, 13:50 #2
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Look in the syntax highlighting in your above.
See any problems?Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Apr 10, 2009, 13:53 #3
- Join Date
- Mar 2008
- Location
- United Kingdom
- Posts
- 1,285
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've now ammended the error you pointed out. Was a simple copynpaste issue when moving text to SitePoint
Can you think of any other reason why this may not display in the format I'd like it to?
I'm sure I've just got my while loop in the wrong place or summit.Last edited by invision2; Apr 10, 2009 at 14:35.
-
Apr 10, 2009, 15:13 #4
- Join Date
- Mar 2008
- Location
- United Kingdom
- Posts
- 1,285
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
OK, almost there I think
PHP Code:<?
require_once('connect.php');
$sql = "SELECT pages.name, pages.file_name, pages.cat, category.id, category.name FROM pages INNER JOIN category ON category.id = pages.cat";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res)) {
if ($row['3'] == '0') continue;
if (empty($last) || $last != $row['name']) {
echo '<li id="' . $row['4'] . '">
<a href="/' . $row['4'] . '">' . $row['4'] . '</a>
<a class="tab" href="/' . $row['4'] . '"></a>
<div class="callout" id="' . $row['4'] . 'tab">';
echo '<ul>';
$last = $row['name'];
}
if (empty($last2) || $last2 != $row['name']) {
echo '<li>' . $row['1'] . '</li>'; /////////////////////////////
$last2 = $row['name'];
echo '</ul>
</div>
</div>
</li>';
}
}
?>
The MAJOR issue is that it only displays one <li> instead of looping through (and displaying) all of them.
I've marked it above with ///////////////.
Really would appreciate help with this.
Thanks again.
Bookmarks