Jump to content

can't wrap my brain around this one


futrose

Recommended Posts

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
Share on other sites

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
Share on other sites

$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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.