Need help changing code to create nested list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Need help changing code to create nested list

Post by Sinemacula »

I've got a csv file from which I'm creating a nested menu/list.

The file includes the following main columns that are used to make the list:
Program
Course
Section
Books

In each Program, there are several Courses. In each course, there are several Sections. In each section there are several books.

I've been able to put together the code that will create the nested list, but I want to change it slightly, and haven't been having much luck.

Here's the important part of what I've got:

Code: Select all

$currentGroup = '' ; 
$currentCourse = '' ; 
$currentSection = '' ;
$g = 0;
$c = 0;
$s = 0;

foreach ($filedata as $line_num => $line) {


$books = explode(';',$line);
				
	if ($currentGroup != $books[0] ) 
	{

	if($g > 0)
		{
			echo "</ul></li></ul></li></ul></li>";
		}
		
	$g++;	
	$c=0;
	$s=0;
				
	$currentGroup = $books[0] ; 
	echo "<li>".$currentGroup."<ul>" ; 
	} 

if ($currentCourse != $books[1]) 
	{ 
			
		 if($c > 0)
		{
			echo "</ul></li></ul></li>";
		
		}
		
	$c++;
	$s=0;
			
  	$currentCourse = $books[1] ; 
	echo "<li><a style=\"text-decoration:none; color:#333333;\" title=\"".$books[3]." ~ ".$books[4]."\">".$currentCourse."</a><ul>" ; 

	} 


if ($currentSection != $books[2]) 
	{ 
			
		 if($s > 0)
		{
			echo "</ul></li>";
		
		}
		
	$s++;
			
  	$currentSection = $books[2] ; 
	echo "<li><a style=\"text-decoration:none; color:#333333;\" title=\"".$books[3]." ~ ".$books[4]."\">Instructor: ".$books[4]."</a><ul>" ; 

	} 



	
	
$coursename = $books[3];

$instructor = $books[4];

$isbn = $books[5];
...the rest (not shown) deals with listing the books under the sections... which is fine.

What I want to change is to go from this:

Program
- Course 1
-- Section 1
--- Book 1
--- Book 2
-- Section 2
--- Book 1

to this:

Program
- Course 1 (Section 1)
-- Book 1
-- Book 2
- Course 1 (Section 2)
-- Book 1

in this case, I'd want to put the Section number or Course Instructor as a tool-tip on hovering over the Course.

What do I need to change in my code to make this work?

I've tried changing the nesting, but so far have ended up with incorrect results.

Thanks,

Scott

PS. You can see how it currently works here: http://store.multipleangles.com - the menu on the right
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

What is the problem?

Post by yacahuma »

It seems to be working. What is the problem?
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Post by Sinemacula »

Yes, it is working, but not quite the way I want it...

Right now, it creates the nested list fully:
  • Program 1
    • Course 1
      • Section 1
        • Book 1
          Book 2
        Section 2
        • Book 1
      Course 2
      • Section 1
        • Book 1
    Program 2
    • Course 1
      • Section 1
        • Book 2
But what I want is to reduce the hierarchy in the list by combining the Courses with their sections, so that I get this:
  • Program 1
    • Course 1 - Section 1
      • Book 1
        Book 2
      Course 1 - Section 2
      • Book 1
      Course 2 - Section 1
      • Book 1
    Program 2
    • Course 1 - Section 1
      • Book 2
Basically, each Course that has multiple Sections should be listed multiple times instead of having the Sections nested within the Course level, and then I can just use either the Section data or the Instructor data as the tool-tip info for each instance of Course.
Post Reply