url list to js array

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
justquick
Forum Newbie
Posts: 5
Joined: Sun Jul 10, 2005 10:05 am

url list to js array

Post by justquick »

ok, i have a list of urls for music files that i want to turn into a javascript array for a tree menu. The urls look like this:

Code: Select all

http://192.168.1.101:8080/Del%20The%20Funky%20Homosapien/Both%20Sides%20of%20the%20Brain/01%20Time%20Is%20Too%20Expensive.mp3
These urls have a unknown number of subdirectories. I need the output js array to look like this:

Code: Select all

var TREE_NODES = ї
	ї'Del The Funky Homosapien', 'http://192.168.1.101:8080/Del%20The%20Funky%20Homosapien', null,
		ї'Both Sides of the Brain', 'http://192.168.1.101:8080/Del%20The%20Funky%20Homosapien/Both%20Sides%20of%20the%20Brain', null
			ї'01 Time Is Too Expensive.mp3', 'http://192.168.1.101:8080/Del%20The%20Funky%20Homosapien/Both%20Sides%20of%20the%20Brain/01%20Time%20Is%20Too%20Expensive.mp3', null]
		]		
	]
];
I need a function that will create this array where the parent directories go in the title. I have been able to preform this given a url, the problems arose when i needed to fill the js array with multiple parent directories and subdirectories. This has become a real headache and i would much appreciate some help
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Well, you're going to need PHP to parse through the URL (as far as I know). Open up the URL and get the artist, album, and song names from that. Then, use PHP to output Javascript and your array. Note that in Javascript, you can't just dynamically grow an array - you have to know how big you want the array, at the time of initialization.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mltsy
Forum Newbie
Posts: 9
Joined: Mon Jul 11, 2005 12:46 pm
Location: Minnesota, USA

Use explode

Post by mltsy »

I'd first take the URL and get the http:// out...

Code: Select all

$temp = explode("//", $url)
$protocol = $temp[0];
$url = $temp[1];
Then replace the %20s with spaces and split the url into parts, the last part being the filename:

Code: Select all

$url = str_replace("%20", " ", $url);
$url = explode("/", $url);
$url[0] = $protocol.$url[0]; //Add the "http://" back in
Then sort that out into an array of and array of an array etc.:

Code: Select all

$entry = $url[count($url) - 1];
for($i = count($url) - 1; $i > 0; $i--)
{
  $inner_part = $entry;
  $entry = array();
  $entry[$url[$i - 1]] = $inner_part;
}
Then merge that with your master array:

Code: Select all

$tree = array_merge_recursive($tree, $entry);
Then of course, you'd have to initialize $tree and put that whole thing in a loop like:

Code: Select all

$tree = array();
foreach($urlList as $url)
{ "all the previous stuff" }
Then you'd have a well structured array at least. Then you can use that array to somehow write a javascript array with what you want in it... :)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

pickle wrote:Note that in Javascript, you can't just dynamically grow an array - you have to know how big you want the array, at the time of initialization.
Array.push() :D
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Well... ya. I meant BESIDES that method.

:)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

pickle wrote:Well... ya. I meant BESIDES that method.

:)
lol :lol:

pickle pickle bo bickle banana fanna fo fickle....
Post Reply