Jump to content

[SOLVED] Help with wrapping the ul tag around my folders in php


Jalz

Recommended Posts

Hi Guys,

 

I'm a php newbie and need some help to get a jquery folder tree (http://bassistance.de/jquery-plugins/jquery-plugin-treeview/) to work with my database navigation.

 

What I need to do to make this work is produce output similar to below. The UL tag needs to start at the beginning of when a folder starts but end when the last piece of content (generally before a new folder begins) ends within that folder - which is what I am having trouble trying to achieve.

 

So for example the code underneath, you can see the UL tag starts with Apple, but ends after iPod. Dont forget you can have a folder within a folder...although not demonstrated in this example.

 

<ul>
<li><span class="folder">Apple</span>
	<ul>
		<li><span class="file">Mac</span></li>
		<li><span class="file">iPod</span></li>
	</ul>
</li>
<li><span class="folder">Microsoft</span>
	<ul>
		<li><span class="file">Office 2007</span></li>
		<li><span class="file">Visual Studio</span></li>
	</ul>
</li>
<li><span class="file">File 4</span></li>
</ul>

 

Underneath is the code which I have written, basic and does work to a point. Notice I do not have the end </ul> tag defined anywhere. Thats because I am not sure where and how to put it so it ends at the end of the content.

 

  <?php foreach($rsFolders_result->getRecords() as $rsFolders_row){ ?>
  <?php 
  
  $type = $rsFolders_row->getField('TYPE',0);
  
  If($type=="FOLDER"){
  echo '<ul><span class="folder"><a href="#" rel="">'.$rsFolders_row->getField('Title',0).'</a></span></li>';
  	} else {
  echo '<li><span class="file"><a href="#" rel="">'.$rsFolders_row->getField('Title',0).'</a></span></li>';
  } ?>
  
  
    <?php } ?>

 

 

Underneath is roughly the type of data I am storing in my database, and it is sorted by PI_ID which is a text field.

 

 

   

Title

   

PI_ID

   

TYPE

 

 

   

Apple

   

1

   

Folder

 

 

   

Macbook

   

1/1

   

Folder

 

 

   

Pro

   

1/1/1

   

File

 

 

   

iPod

   

1/2

   

Folder

 

 

   

Nano

   

1/2/1

   

File

 

 

   

iPhone

   

1/3

   

File

 

 

   

Microsoft

   

2

   

Folder

 

 

   

Applications

   

2/1

   

Folder

 

 

   

Office

   

2/1/1

   

File

 

 

   

Hardware

   

2/2

   

Folder

 

 

   

XBox

   

2/2/1

   

File

 

 

Any help would be greatly appreciated, been racking my brains trying to get it to work.

 

Best

 

Jalz

 

try

<?php 
$test='Apple	1	Folder
Macbook	1/1	Folder
Pro	1/1/1	File
iPod	1/2	Folder
Nano	1/2/1	File
iPhone	1/3	File
Microsoft	2	Folder
Applications	2/1	Folder
Office	2/1/1	File
Hardware	2/2	Folder
XBox	2/2/1	File';

foreach (explode("\n", $test) as $r){
$r = explode("\t", $r);
$data[] =array('Title' => $r[0], 'PI_ID' => $r[1], 'TYPE' => $r[2]);
}
// $data simulate data from database
echo "<ul>\n";
$open_ul = 1;
foreach ($data as $rsFolders_row){
$deep = count(explode('/', $rsFolders_row['PI_ID']));
for ($i = $open_ul; $i > $deep; $i--) echo "</ul>\n";
$open_ul = $deep;
if ($rsFolders_row['TYPE'] == 'Folder'){
	echo '<li><span class="folder"><a href="#" rel="">'.$rsFolders_row['Title']."</a></span></li>\n";
	echo "<ul>\n";

	$open_ul++;
} else echo '<li><span class="file"><a href="#" rel="">'.$rsFolders_row['Title']."</a></span></li>\n";
}
for ($i=0; $i<$open_ul; $i++) echo "</ul>\n";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.