Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logic Problem

Status
Not open for further replies.

ralphiooo

Programmer
Oct 23, 2005
64
GB
Hi, I have a problem trying to sort out the side navigation to a script i'm making. It's still in the planning phase at the moment so I've put together the following examples to make this easier to explain.

Say I have the following table:

Categories:
- id (primary key)
- depth (the depth of the category)
- lineage (lineage containing the category path)

With the following data:

1 0 /1/
2 0 /2/
3 0 /3/
4 1 /3/4/
5 2 /3/4/5/
6 2 /3/4/6
7 1 /3/7/
8 2 /3/7/8/
9 2 /3/7/9/
10 3 /3/7/9/10
11 3 /3/7/8/10

Now If I was on the page where id = 3 you would get:

1
2
3
__4
__7

If I was on the page where id = 4 you would get:

1
2
3
__4
____5
____6
__7

If I was on the page where id = 10 you would get:

1
2
3
__4
__7
____8
____9
______10

I hope you see the pattern now. The problem i'm having is constructing the loop. I only want to display the categories in the structure above so I loop through each category using foreach and then use an if statement to get the categories I wish to display. Here's what I have put together so far:

Code:
foreach cats as cat {
	if cat[depth] = 0 or (cat[depth] <= depth + 1 and ?????) {
		// display cat
	}
}

I know it needs something else. So far I have said if the depth = 0 (top level category) i always want to display it. Secondly I make sure that category gets displayed if it is within one depth of the current category. Finally if the category id is 4 (as shown in one of the examples above) it would give:

1
2
3
__4
____5
____6
__7
____8
____9

So the if statement needs something extra to restrict the display of categories it does not contain.

I've been trying to rack my brains around this for the last few hours and it has exhausted me.

Would appreciate it if someone could help. Thanks
 
I am also confused by your logic. If you are trying to setup a basic hierarchical tree for the navigation then I would suggest either using:

1. Recursion Tree - slow for large trees but easier to understand

or

2. Modified Preorder Tree Traversal - faster but more difficult to grasp.

I'm not sure these are the 'official' names for the methods, but these are the names I've learned. A good article explaining both methods can be found here. If by any chance you decide on using the recursion tree method, I have a standard class to handle the db manipulation and select queries, that your welcome to use.
 
The reason you are displaying 4 and 7 is a way of getting to the upper categories or the navigation would not work. Will take a read of that tutorial abit later so will report back on which method i prefer. Cheers
 
Just read the tutorial, and really looking to optimize so the first method would not be the preferred method. As for the second method it seems abit fiddly for what it is. The problem is that I have done all the backend bits getting the database navigation to work how I explained above. I thought the front end part would be alot simpler but I guess not. Appreciate the help so far though. Cheers

btw this is ralphiooo just on a different computer here.
 
i really think that you're on a hiding to nothing here. i'd go back and change the way you build your nav trees.

i've spent a few minutes looking at the problem and trying to guess what you intend by iteratively looking at your examples. the code i have is as follows:

Code:
<?
/*
id d hier
1  0 /1/
2  0 /2/
3  0 /3/
4  1 /3/4/
5  2 /3/4/5/
6  2 /3/4/6
7  1 /3/7/
8  2 /3/7/8/
9  2 /3/7/9/
10 3 /3/7/9/10
11 3 /3/7/8/10
*/

//into an array

$categories["1"] = array("depth"=> 0 , "hier"=>"/1");
$categories["2"] = array("depth"=> 0 , "hier"=>"/2");
$categories["3"] =array("depth"=> 0 , "hier"=>"/3");
$categories["4"] =array("depth"=> 1 , "hier"=>"/3/4");
$categories["5"] =array("depth"=> 2 , "hier"=>"/3/4/5");
$categories["6"] = array("depth"=> 2 , "hier"=>"/3/4/6");
$categories["7"] = array("depth"=> 1 , "hier"=>"/3/7");
$categories["8"] = array("depth"=> 2 , "hier"=>"/3/7/8");
$categories["9"] = array("depth"=> 2 , "hier"=>"/3/7/9");
$categories["10"] = array("depth"=> 3 , "hier"=>"/3/7/9/10");
$categories["11"] = array("depth"=> 3 , "hier"=>"/3/7/8/10");
$string = retrieve_category("4");
echo "<pre>";
echo nl2br("$string");
echo "</pre>";

function retrieve_category($cat)
{
	global $categories;
	$lineend = "\r\n";
	$string="";
	$masterdepth = $categories[$cat]['depth'] + 1;
	$masterhier = explode("/",$categories[$cat]['hier']);

	foreach ($categories as $catid=>$data):
		$depth = $data['depth'];
		$hier = explode("/",$data['hier']);
		if (
			((in_array($cat, $hier)) && $depth <= $masterdepth) 
			|| 
			$depth == 0
			||
			( in_array($cat, $masterhier) && $depth < $masterdepth)
			):
			$sep="";
			for ($i=0; $i<$depth;$i++): 
				$sep .="\t";
			endfor;
			$string .= $sep.$catid.$lineend;
		endif;
	endforeach;
	return $string;
}

it is so far working for depths 2 and below but is not working for category 10 (and thus depth 3 i guess). it should not be too difficult for you to take the code and adapt it to fit the bill perfectly. but as i said, this is the wrong approach. if things are this difficult then it's time to go back and examine your back-end code. menu trees are dead easy mainly.
 
Yeah I think i'm going to go back and make the changes while I have the chance. Cheers for your feedback. The Recursion Tree is starting to grow on me. If I store the categories in an array then I will only need one query. Itshim could you share how you've done it so that I can use it for reference. Thanks
 
np, the class it too long to post here. It is fully commented and documented using PHPdocumentor tags. I have placed my email address in my profile for now. Click on my name to see my profile. Drop me a line and I will send it to you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top