The Little Guy Posted April 13, 2010 Share Posted April 13, 2010 I need to build a directory, so a user first selects an item from the main page, then that page has files, or sub items, they can either click on the file to view its contents, or click on a sub item, and then the same thing happens, more items and directories (if any). so... http://mysite.com/item1 http://mysite.com/item1/subitem1 http://mystie.com/item1/subitem1/subitem1 http://mysite.com/item1/subitem1/subitem1/my_information.html the subitems can go 2+ subitems deep, and my_information.html is the actual content how can I do that? so a real example: http://mysite.com/Windows/Windows_7/Windows_32/Task_Bar.html http://mysite.com/Windows/Windows_7/Windows_64/Task_Bar.html http://mysite.com/Windows/Windows_Vista/Windows_32/Task_Bar.html http://mysite.com/Windows/Windows_Vista/Windows_64/Task_Bar.html http://mysite.com/Windows/Windows_XP/Windows_32/Task_Bar.html http://mysite.com/Windows/Windows_XP/Windows_64/Task_Bar.html Quote Link to comment Share on other sites More sharing options...
teamatomic Posted April 13, 2010 Share Posted April 13, 2010 scandir is_dir When you scan the dir if its a dir you make a folder link, if its a file you display its link. The easiest way would be to stuff them into an array for each then loop the arrays displaying the links. HTH Teamaotmic Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 13, 2010 Author Share Posted April 13, 2010 I actually am not really pulling from an actual directory, I am pulling from a database. Sorry I forgot to mention that. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted April 13, 2010 Share Posted April 13, 2010 That really depends on how your table is set up. If you have good parent/child relationships then its just a matter of displaying files that belong only to the requested parent set(s). HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 14, 2010 Author Share Posted April 14, 2010 Table 'categories': + id + parent + name + id = auto_inc parent = relates to the id in this table (0 means no parent) name = the name of the category (windows, zune, windows 7, imac, ibook, etc) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 14, 2010 Author Share Posted April 14, 2010 so... I got what I was looking for sorta. The new problem I am having is I can do this: http://tzfiles.com/Zune/Checking_For_Updates.html http://tzfiles.com/Zune/Gen_1/120/Checking_For_Updates.html http://tzfiles.com/Windows/Checking_For_Updates.html http://tzfiles.com/Checking_For_Updates.html I can go to any of those links, and they all show the same information. I don't know If I want that to happen or not. Any thoughts on this matter? The first link is the real link that I want, the others are variations of the link. If you go to any of the links (other than the first one), and click on one of the navigation items, you cant get back to the file. Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted April 14, 2010 Share Posted April 14, 2010 Could always have a back button. cb Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 14, 2010 Author Share Posted April 14, 2010 how would a back button help? Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted April 14, 2010 Share Posted April 14, 2010 From what i got from this: If you go to any of the links (other than the first one), and click on one of the navigation items, you cant get back to the file It would seem you need a back button to get back to your file. -cb- Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 14, 2010 Author Share Posted April 14, 2010 no no no.... say you go here: http://tzfiles.com/Zune/Gen_1/120/Checking_For_Updates.html then click on 120 in the nav, the link for "Checking For Updates.html" doesn't display, because it isn't a child of 120, it is a child of Zune, so you shouldn't even be able to access the above link. Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted April 14, 2010 Share Posted April 14, 2010 it happens because of the way your building the page, using mod rewrite i assume. I'm still confused as to wether you want a simple file browser or something more complicated? cb Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 15, 2010 Author Share Posted April 15, 2010 I need something complicated - $cPath = /Windows/Windows_XP/Open_Windows_Task_Manager.html - formatPath returns an array: Array ( [path] => Array ( [0] => Windows [1] => Windows_XP ) [file] => Open_Windows_Task_Manager.html [file_name] => Open_Windows_Task_Manager [file_title] => Open Windows Task Manager ) - deFormat replaces underscores with spaces, and removes the html extension here is what I have so far to check if the path is valid, but it isn't working.... public function validPath($cPath){ $aPath = array_reverse(explode("/", $cPath)); $path = $this->formatPath($cPath); if(empty($path['file'])){ $s = 1; }else{ $s = 0; } //print_r($path); //echo '<br><br>'; $new = array(); $rootset = false; for($i=$s;$i<count($path['path']);$i++){ $im = mysql_real_escape_string($aPath[$i]); $im = $this->deFormat($im); if(preg_match("~\.html~", $path['file']) && $i == 0){ $sql = $this->mysql->query("SELECT * FROM categories WHERE id = (SELECT category FROM data WHERE name = '$im')")or die(mysql_error()); }else{ $sql = $this->mysql->query("SELECT * FROM categories WHERE id = (SELECT parent FROM categories WHERE name = '$im')")or die(mysql_error()); } $row = mysql_fetch_assoc($sql); $tempPath = array_reverse($path['path']); //echo $tempPath[$i];print_r($row); //echo '<br><br>'; if($row['name'] == 'root'){ $rootset = true; } } if(!$rootset){ echo 'here'; } return true; } Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted April 15, 2010 Share Posted April 15, 2010 Ok, is little hard to undertand but i think i got your idea; The Idea Store the categories and paths in a mysql table called categories. Let users traverse the categories using mod_rewrite. So each category row has a id. This id is used to tell whos each parent is. This Means: You need to start with the child, and work to the parent to see if the directory is valid. It also helps as if something is wrong its more likely to get the result quicker. And you can just keep following parents until you get to oen that has no parent (a root category). You are a very lucky man, i just wrote a little class for you, all i ask is u keep the comments at the top. Code: // Please keep this intact here. // By ChemicalBliss, // http://www.phpfreaks.com/forums/ class URL_Wrapper_mw { private $category_array; private $Path; private $Path_Array; public function __construct($cPath){ $this->Path = $cPath; $this->FormatPath(); } public function FormatPath(){ $tempa = explode("/", $this->Path); $tempb = array(); foreach($tempa As $AName->$AValue){ if($tempa[count($tempa) - 1] != $AValue){ // Not last item $tempb[] = $AValue; }else{ $tempa['path'] = $tempb; $tempa['file'] = $AValue; $tempa['filename'] = preg_replace("/(\.[a-z0-9_]+)$/i","",$AValue); $tempa['file_title'] = ucfirst(str_replace("_"," ",$tempa['filename'])); } } $this->Path_Array = $tempa; } private function LoadCategories(){ // 1 single mysql call ever needed. $sql = "SELECT * FROM `categories`"; $execsql = mysql_query($sql); $this->category_array = array(); // For Measure // Store Cats in array. while($row = mysql_fetch_assoc($execsql)){ $this->category_array[] = $row; } } // Actually validate true or false is valid path. public function validate_path(){ $chkarray = array_reverse($this->Path_Array['path']); For($i=0;$i<count($chkarray);$i++){ // First check if last item (root category) if($i+1 == count($chkarray)){ // Get Parent Name/false $parent_name = $this->getParent($chkarray[$i]); if($parent_name !== false){ return false; // Too deep, url doesnt match parent path. } }else{ // get Parent Name/False $parent_name = $this->getParent($chkarray[$i]); // Check if false if($parent_name == false){ return false; // parent path doesnt match URL }else{ // check if names match if(strtolower($parent_name) != strtolower($chkarray[$i+1])){ return false; // URL doesnt match parent path. } } } } return true; // No problems. } // Just uses mysql. returns category row of parent to that id specified. public function getParent($name){ // Loop results until you get your target row, get parent id foreach($this->category_array As $row){ if(strtolower($row['name']) == strtolower($name)){ $pid = $row['parent']; } } // Get parent name from parent id found above. foreach($this->category_array As $row){ if($row['id'] == $pid){ return = $row['name']; // return parent id } } // No parent id? Return False to show it return false; } } // How to use // Instantiate the class so yo can use it. $Path_Engine = new URL_Wrapper_mw($cPath); // Call member method LoadCategories to load the mysql data. $Path_Engine->LoadCategories(); // check if the path is valid. if($Path_Engine->validate_path() === true){ echo "The path is valid!"; }else{ echo "Not valid "; } // *UNTESTED // by Chemicalbliss - you may remove these 2 comment lines. As it states it is untested, but the logic is there, if there are any errors then i'd make a new thread as it will be a new problem. once the code is fully working, if you still have problems then come back here and bump it. -cb- Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 15, 2010 Author Share Posted April 15, 2010 Sorry dude, the class doesn't work... I get "The path is valid!" even when it is not valid. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 15, 2010 Author Share Posted April 15, 2010 I take that back, I forgot to pass the path to the class... Now I am getting an error message, but I will fix it later I g2g now. Quote Link to comment 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.