Page 1 of 1

url list to js array

Posted: Sun Jul 10, 2005 10:37 am
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

Posted: Mon Jul 11, 2005 10:04 am
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.

Use explode

Posted: Mon Jul 11, 2005 2:15 pm
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... :)

Posted: Mon Jul 11, 2005 3:24 pm
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

Posted: Mon Jul 11, 2005 5:33 pm
by pickle
Well... ya. I meant BESIDES that method.

:)

Posted: Mon Jul 11, 2005 5:35 pm
by Burrito
pickle wrote:Well... ya. I meant BESIDES that method.

:)
lol :lol:

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