futrose Posted October 28, 2010 Share Posted October 28, 2010 I am trying to create some navigation link with sublinks in a <ul> list. Here is the set up. Table = categories fields: id cattitle = title parentid = level of the link (0 = top level) cat_loc = the id value of the link that this link will be sublink of example: 1st link: id 1, title - home, parentid - 0, cat_loc - null; 2nd link: id - 5, title - info, parentid - 0, cat_loc - null; 3rd link: id - 6, title - test, parentid - 1, cat_loc - 5; so test should be a sublink of info. Here is what I have for code so far... <?php include $_SERVER['DOCUMENT_ROOT'] . './includes/magicquotes.inc.php'; include $_SERVER['DOCUMENT_ROOT'] . './includes/db.inc.php'; $result = mysqli_query($link, 'SELECT * From categories order by orderby asc'); if (!$result) { $error = 'Error getting categories: ' . mysqli_error($link); include 'error.php'; exit(); } while ($row = mysqli_fetch_array($result)) { $navlink[] = array('id' => $row['id'], 'cattitle' => $row['cattitle'], 'cat_loc' => $row['cat_loc'], 'parentid' => $row['parentid']); } echo '<ul>'; $count = 0; foreach ($navlink as $nav): { if ($nav['parentid'] == 0) { if ($count!=0){echo " | ";} echo '<li>' . '<a href="home.php?id=' . $nav['id'] . '"' . ' ' . 'title="' . $nav['cattitle'] . '">' . htmlspecialchars($nav['cattitle']) . '</a>'; $count++; } if ($nav['parentid'] != 0) { $result2 = mysqli_query($link, 'Select * From categories where "$nav[cat_loc]" = "$nav[id]"'); if (!$result2) { $error = 'Error getting categories: ' . mysqli_error($link); include 'error.php'; exit(); } echo '<ul>'; while ($row2 = mysqli_fetch_array($result2)) { $subnavlink[] = array('id' => $row2['id'], 'cattitle' => $row2['cattitle']); } foreach ($subnavlink as $subnav): { echo '<li>' . '<a href="home.php?id=' . $subnav['id'] . '"' . ' ' . 'title="' . $subnav['cattitle'] . '">' . htmlspecialchars($subnav['cattitle']) . '</a></li>'; } endforeach; } } endforeach; echo '</ul>'; yes I do have the php closed further down the page. Part of my problem is that the sublinks won't be in the database right after their parent links so I don't think the way I have my foreach loop set up is right. I know I don't have my <ul>'s and <li>'s closed correctly yet. I just want to get the subnavs to show up, I can fix those later. Any ideas? Link to comment https://forums.phpfreaks.com/topic/217092-cant-wrap-my-brain-around-this-one/ Share on other sites More sharing options...
futrose Posted October 29, 2010 Author Share Posted October 29, 2010 Ok so I made a bunch of changes that got me closer to where I need to be but I am getting this error now. Error getting subcategories: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[id]' at line 1 The line in question is $result2 = mysqli_query($link, 'Select * from categories where cat_loc = $nav[id]'); Here is the code <?php include $_SERVER['DOCUMENT_ROOT'] . './includes/magicquotes.inc.php'; include $_SERVER['DOCUMENT_ROOT'] . './includes/db.inc.php'; $result = mysqli_query($link, 'SELECT * From categories order by orderby asc'); if (!$result) { $error = 'Error getting categories: ' . mysqli_error($link); include 'error.php'; exit(); } while ($row = mysqli_fetch_array($result)) { $navlink[] = array('id' => $row['id'], 'cattitle' => $row['cattitle'], 'cat_loc' => $row['cat_loc'], 'parentid' => $row['parentid']); } echo '<ul>'; $count = 0; foreach ($navlink as $nav): { if ($nav['parentid'] == 0) { if ($count!=0){echo " | ";} echo '<li>' . '<a href="home.php?id=' . $nav['id'] . '"' . ' ' . 'title="' . $nav['cattitle'] . '">' . htmlspecialchars($nav['cattitle']) . '</a>'; $count++; if ($nav['cat_loc'] == 0) { $result2 = mysqli_query($link, 'Select * from categories where cat_loc = $nav[id]'); if (!$result2) { $error = 'Error getting subcategories: ' . mysqli_error($link); include 'error.php'; exit(); } if ($result2 ="") { echo '</li>'; } else { echo '<ul>'; while ($row2 = mysqli_fetch_array($result2)) { $subnavlink[] = array('id' => $row2['id'], 'cattitle' => $row2['cattitle']); } foreach ($subnavlink as $subnav): { echo '<li>' . '<a href="home.php?id=' . $subnav['id'] . '"' . ' ' . 'title="' . $subnav['cattitle'] . '">' . htmlspecialchars($subnav['cattitle']) . '</a></li>'; } endforeach; echo '</ul>'; } echo '</li>'; } } } endforeach; echo '</ul>'; ?> any ideas? Link to comment https://forums.phpfreaks.com/topic/217092-cant-wrap-my-brain-around-this-one/#findComment-1127822 Share on other sites More sharing options...
Anti-Moronic Posted October 29, 2010 Share Posted October 29, 2010 $result2 = mysqli_query($link, 'Select * from categories where cat_loc = $nav[id]'); Should be: $result2 = mysqli_query($link, 'Select * from categories where cat_loc = '.$nav['id']); Let me know if that works. EDIT: Also, I'd definitely advise wrapping this in a function so you can simply use this in your code: get_nav(); *or* if you use a class: $templateTools->get_nav(); Or something like that. Will make your life a lot easier. You can also use that to cache your query or retrieve already loaded data. Link to comment https://forums.phpfreaks.com/topic/217092-cant-wrap-my-brain-around-this-one/#findComment-1127826 Share on other sites More sharing options...
futrose Posted October 29, 2010 Author Share Posted October 29, 2010 well... that fixed that error but I have these 3 now. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\includes\topnav.inc.php on line 47 Line 47 is: while ($row2 = mysqli_fetch_array($result2)) Notice: Undefined variable: subnavlink in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\includes\topnav.inc.php on line 52 Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\includes\topnav.inc.php on line 52 Line 52 is: foreach ($subnavlink as $subnav): Link to comment https://forums.phpfreaks.com/topic/217092-cant-wrap-my-brain-around-this-one/#findComment-1127827 Share on other sites More sharing options...
futrose Posted October 29, 2010 Author Share Posted October 29, 2010 Got this working, thanks for your help. Link to comment https://forums.phpfreaks.com/topic/217092-cant-wrap-my-brain-around-this-one/#findComment-1127853 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.