Jump to content

Directory Structure


The Little Guy

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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-

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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