Jalz Posted March 10, 2009 Share Posted March 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148864-solved-help-with-wrapping-the-ul-tag-around-my-folders-in-php/ Share on other sites More sharing options...
wildteen88 Posted March 10, 2009 Share Posted March 10, 2009 Simply echo the opening ul tag before the foreach loop. then echo the closing ul tag after your loop. Quote Link to comment https://forums.phpfreaks.com/topic/148864-solved-help-with-wrapping-the-ul-tag-around-my-folders-in-php/#findComment-781690 Share on other sites More sharing options...
sasa Posted March 11, 2009 Share Posted March 11, 2009 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/148864-solved-help-with-wrapping-the-ul-tag-around-my-folders-in-php/#findComment-781828 Share on other sites More sharing options...
Jalz Posted March 11, 2009 Author Share Posted March 11, 2009 Just wanted to say thank you guys for taking the time out to answer, especially sasa you guys rock! How do I mark it as solved Quote Link to comment https://forums.phpfreaks.com/topic/148864-solved-help-with-wrapping-the-ul-tag-around-my-folders-in-php/#findComment-782349 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.